-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting_algorithms.cpp
More file actions
179 lines (166 loc) · 2.93 KB
/
Copy pathsorting_algorithms.cpp
File metadata and controls
179 lines (166 loc) · 2.93 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
#include<iostream>
using namespace std;
void BubbleSort(int n, int arr[])
{
int i,temp,j;
for(i=0;i<(n-1);i++)
{
for(j=0;j<(n-i-1);j++)
{
if(arr[j]>arr[j+1])
{
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
cout<<endl;
for(int k=0;k<n;k++)
{
cout<<arr[k]<<" ";
}
}
cout<<"\nSorted List is:\n";
for(i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
}
void InsertSort(int n, int arr[])
{
int i,j,temp;
for(i=1;i<n;i++)
{
temp=arr[i];
j=i-1;
while((temp<arr[j]) && (j>=0))
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=temp;
cout<<"Pass:"<<i<<endl;
for(int k =0;k<n;k++)
{
cout<<arr[k]<<" ";
}
cout<<endl;
}
cout<<"Array after sorting : \n";
for(i=0; i<n; i++)
{
cout<<arr[i]<<" ";
}
}
void SelectSort(int n, int arr[])
{ int i,minplace,temp,j;
for(int i=0;i<n-1;i++)
{
minplace=i;
for(j=i+1;j<n;j++)
{ if(arr[j]<arr[minplace])
minplace=j;
}
temp=arr[i];
arr[i]=arr[minplace];
arr[minplace]=temp;
cout<<endl;
for(int k=0;k<n;k++)
{
cout<<arr[k]<<" ";
}
}
}
void mer(int arr[],int low,int mid,int high,int n);
void mergesort(int arr[],int low,int high,int n)
{
if(low<high)
{
int mid;
mid=(low+high)/2;
mergesort(arr,low,mid,n);
mergesort(arr,mid+1,high,n);
mer(arr,low,mid,high,n);
}
}
void mer(int arr[20],int low,int mid,int high,int n)
{
int arr1[10];
int i,j,k=0;
i=low;
j=mid+1;
while(i<=mid&&j<=high)
{
if(arr[i]<arr[j])
{
arr1[k]=arr[i];
i++;
k++;
}
else
{
arr1[k]=arr[j];
j++;
k++;
}
}
while(i<=mid)
{
arr1[k]=arr[i];
i++;
k++;
}
while(j<=high)
{
arr1[k]=arr[j];
j++;
k++;
}
int cnt=0;
for(int i=low;i<=high;i++)
{
arr[i]=arr1[cnt];
cnt++;
}
for(int i=0;i<n;i++)
{
cout<<"\t "<<arr[i];
}
cout<<endl;
}
int main()
{
int arr[50],n,i,choice=1;
int low,high;
cout<<"1]Bubble Sort\n2]Insert Sort\n3]Select Sort\n4]Merge Sort";
cout<<"\nEnter your choice:";
cin>>choice;
cout<<"\nEnter total number of elements:";
cin>>n;
cout<<"Enter the elements of array:"<<endl;
for(i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"\n";
low=0;
high=n-1;
switch(choice)
{
case 1: BubbleSort(n,arr);
break;
case 2: InsertSort(n,arr);
break;
case 3: SelectSort(n,arr);
break;
case 4: mergesort(arr,low,high,n);
for(int i=low;i<=high;i++)
{
cout<<endl;
cout<<"\t "<<arr[i];
}
break;
default: cout<<"\nWrong choice";
break;
}
}