-
Notifications
You must be signed in to change notification settings - Fork 91
/
Sorting.cpp
97 lines (96 loc) · 2.23 KB
/
Sorting.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
#include <iostream>
using namespace std;
void display(int ar[],int n)
{
for(int i=0;i<n;i++)
{
cout<<ar[i]<<endl;
}
}
void Selection(int ar[], int n)
{ int min=0,temp=0;
for( int i=0;i<n;i++)
{
min=i;
for(int j=i+1;j<n;j++)
{
if(ar[j]<ar[min])
{
min=j;
}
}
temp=ar[i];
ar[i]=ar[min];
ar[min]=temp;
}
}
void Bubble(int ar[], int n)
{ int temp=0;
for( int i=1;i<n;i++)
{
for(int j=0;j<n-1;j++)
{
if(ar[j]>ar[j+1])
{
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}}}}
void Insertion(int ar[], int n)
{ int key=0;
for( int i=1;i<n;i++)
{
key=ar[i];
int j=i-1;
while(j>=0&& key<ar[j]){
ar[j+1]=ar[j];
j--;
}
ar[j+1]=key;
}}
int main()
{
char ch='y';
do{
cout<<"\n Hi! welcome do you want to continue (Y/N):"<<endl;
cin>>ch;
if(ch=='Y'||ch=='y')
{
int i,m,n;
cout<<"\n Enter your choice: \n 1.Selection sort\n 2.Bubble sort\n 3.Insertion sort"<<endl;
cin>>m;
cout<<"\n Enter the size of the array:"<<endl;
cin>>n;
int ar[n];
cout<<"\n Enter the elements:"<<endl;
for( i=0;i<n;i++)
{
cin>>ar[i];
}
switch(m)
{
case 1:
Selection(ar,n);
cout<<"\n Sorted array after Selection sort is:"<<endl;
display(ar,n);
break;
case 2:
Bubble(ar,n);
cout<<"\n Sorted array after Bubble sort is:"<<endl;
display(ar,n);
break;
case 3:
Insertion(ar,n);
cout<<"\n Sorted array after Insertion sort is:"<<endl;
display(ar,n);
break;
default:
cout<<"invalid choice";
}
}
else{
cout<<"\n Thank you program terminated";
}
}while(ch=='Y'||ch=='y');
return 0;
}