-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortingtechniques.cpp
157 lines (143 loc) · 3.3 KB
/
sortingtechniques.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
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
#include <iostream>
using namespace std;
int partition(int arr[],int low,int uper){
int pivot= arr[low];
int start= low;
int end= uper;
while(start<end){
while(arr[start]<=pivot){
start++;
}
while(arr[end]>pivot){
end--;
}
if(start<end){
swap(arr[start],arr[end]);
}
swap(arr[low],arr[end]);
}
return end;
}
void quicksort(int arr[],int low,int uper){
if(low<uper){
int loc= partition(arr,low,uper);
quicksort(arr,low,loc-1);
quicksort(arr,loc+1,uper);
}
}
void merge(int arr[],int low, int mid,int uper){
int n1= mid-low+1;
int n2 = uper-mid;// kio k hme mid ko mid+1 se start krna tha pr hmne isko mid se is lye +1 nai kia
int a[n1];
int b[n2];
for(int i=0; i<n1;i++){
a[i]=arr[low+i];
}
for(int i = 0;i<n2;i++){
b[i]=arr[mid+1+i];
}
int i= low;
int j = mid+1;
int k= low;
while(i<=mid && j<=uper){
if(arr[i]<=arr[j]){
b[k]=a[i];
k++;
j++;
}
else{
b[k]=a[j];
j++;
k++;
}
if(i>mid){
while(j<=uper)
b[k]=a[j];
j++;
k++;
}
else{
while(i<=mid) {
b[k] = a[i];
i++;
k++;
}
}
}
}
void mergesort(int arr[],int low,int uper){
if(low<uper){
int mid = (low + uper)/2;
mergesort(arr,low,mid);
mergesort(arr,mid+1,uper);
merge(arr,low,mid,uper);
}
}
int main(){
int arr[5]={12,11,34,3,2};
/* int count = 1;
while(count<5){
for(int i =0; i<5-count;i++){
if(arr[i]>arr[i+1]){
swap(arr[i+1],arr[i]);
}
}
count++;
}*/
for(int i =0; i<4;i++){
for(int j =0 ;j<5;j++){
if(arr[j]>arr[j+1]){
swap(arr[j+1],arr[j]);
}
}
}
cout<<"the sorted array after bubble sort is : ";
for(int i= 0;i<5;i++){
cout<<arr[i];
cout<<" ";
}
cout<<endl;
for(int i = 0; i<4;i++){
int min=i;
for(int j=i+1; j<5;j++){
if(arr[j]<arr[min]){
min=j;
}
}
if(min!=i){
swap(arr[min],arr[i]);
}
}
cout<<"the sorted array after selection sort is : ";
for(int i= 0;i<5;i++){
cout<<arr[i];
cout<<" ";
}
for(int i = 1; i<5;i++){
int temp= arr[i];
int j =i-1;
while(j>=0&& arr[j]>temp){
arr[j+1]=arr[j];
j--;
}
arr[j+1]=temp;
}
cout<<"\nthe sorted array after insert sort is : ";
for(int i= 0;i<5;i++){
cout<<arr[i];
cout<<" ";
}
quicksort(arr,0,4);
cout<<"\nthe sorted array after quick sort is : ";
for(int i= 0;i<5;i++){
cout<<arr[i];
cout<<" ";
}
mergesort(arr,0,4);
cout<<"\nthe sorted array after merge sort is : ";
for(int i= 0;i<5;i++){
cout<<arr[i];
cout<<" ";
}
return 0;
}