forked from anmolrishi/ProgrammingHub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort.cpp
More file actions
206 lines (167 loc) · 3.2 KB
/
Copy pathSort.cpp
File metadata and controls
206 lines (167 loc) · 3.2 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include<iostream>
using namespace std;
//Quick Sort
// Avg Time Complexity O(nlogn)
// Worst Time Complexity O(n^2)
void swap(int *a, int *b){
int temp=*a;
*a=*b;
*b=temp;
}
int partition(int arr[], int low, int high)
{
int pivot = arr[low];
int i = low - 1, j = high + 1;
while (true)
{
do
{
i++;
} while (arr[i] < pivot);
do
{
j--;
} while (arr[j] > pivot);
if (i >= j)
return j;
swap(arr[i], arr[j]);
}
}
swap(&a[i+1],&a[e]);
return i+1;
}
void quicksort(int a[], int low, int high){
if(low<high){
int pi=partition(a,low,high);
quicksort(a,low,pi-1);
quicksort(a,pi+1,high);
}
}
//Insertion Sort
//Time Complexity O(n^2)
void isort(int a[],int n){
int j;
for(int i=1;i<n;i++){
int temp=a[i];
j=i-1;
while(a[j]>temp && j>=0){
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
}
//Selection Sort
//Time Complexity O(n^2)
void ssort(int a[],int n){
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
if(a[i]>a[j]){
swap(&a[i],&a[j]);
}
}
}
}
//Bubble Sort
//Time Complexity O(n^2)
void bsort(int a[],int n){
for(int i=0;i<n;i++){
for(int j=0;j<n-i;j++){
if(a[j]>a[j+1]){
swap(&a[j],&a[j+1]);
}
}
}
}
//Merge Sort
// Worst Time Complexity O(nlogn)
void merge(int a[], int start, int mid,int end){
int i=0,j=0,k=start;
int lsize=mid-start+1;
int rsize=end-mid;
int left[lsize], right[rsize];
for(int i=0;i<lsize;i++){
left[i]=a[start+i];
}
for(int i=0;i<rsize;i++){
right[i]=a[mid+1+i];
}
while(i<lsize && j<rsize){
if(left[i]<=right[j]){
a[k]=left[i];
i++;
}else{
a[k]=right[j];
j++;
}
k++;
}
while(i<lsize){
a[k]=left[i];
i++;k++;
}
while(j<rsize){
a[k]=right[j];
j++;k++;
}
}
void mergesort(int a[],int s, int e){
if(s<e){
int mid=s + (e-s)/2;
mergesort(a,s,mid);
mergesort(a,mid+1,e);
merge(a,s,mid,e);
}
}
int main(){
int n=0;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
cout<<"Quick Sort\n";
quicksort(a,0,n-1);
cout<<'\n';
for(int i=0;i<n;i++){
cout<<a[i]<<'\n';
}
cout<<'\n';
for(int i=0;i<n;i++){
cin>>a[i];
}
cout<<"Merge Sort\n";
mergesort(a,0,n-1);
cout<<'\n';
for(int i=0;i<n;i++){
cout<<a[i]<<'\n';
}
for(int i=0;i<n;i++){
cin>>a[i];
}
cout<<"Bubble Sort\n";
bsort(a,n);
cout<<'\n';
for(int i=0;i<n;i++){
cout<<a[i]<<'\n';
}
for(int i=0;i<n;i++){
cin>>a[i];
}
cout<<"Selection Sort\n";
ssort(a,n);
cout<<'\n';
for(int i=0;i<n;i++){
cout<<a[i]<<'\n';
}
for(int i=0;i<n;i++){
cin>>a[i];
}
cout<<"Insertion Sort\n";
isort(a,n);
cout<<'\n';
for(int i=0;i<n;i++){
cout<<a[i]<<'\n';
}
return 0;
}