-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsortings.cpp
70 lines (69 loc) · 1.07 KB
/
sortings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<iostream>
using namespace std;
void swap(int *a , int *b) //swap 2 numbers
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void duplicate(int a[],int b[],int n) //put the values on fulled array a into array b
{
for(int i=0;i<n;i++)
{
b[i]=a[i];
}
}
void show (int a[],int n) //display the array
{
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
void selection_sort(int a[],int l) //using selction sort which take O(n^2) time
{
for(int i=0;i<l-1;i++)
{
for(int j=i+1;j<l;j++)
{
if(a[i]>a[j])
{
swap(a[i],a[j]);
}
}
}
show(a,l);
}
void bubble_sort(int a[],int l) //using bubble sort
{
for(int i=0;i<l-1;i++)
{
for(int j=0;j<l-i-1;j++)
{
if(a[j]>a[j+1])
{
swap(a[j],a[j+1]);
}
}
}
show(a,l);
}
int main()
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int b[n],c[n];
duplicate(a,b,n);
duplicate(a,c,n);
selection_sort(b,n);
bubble_sort(c,n);
show(a,n);
return 0;
}