forked from CDChaitanya/Sort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSort.cpp
125 lines (124 loc) · 2.22 KB
/
Sort.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
#include<iostream>
using namespace std;
void BubbleSort(int a[],int n)
{
int flag;
for(int i=0;i<n-1;i++)
{
flag=0;
for(int j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
int temp;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
flag=1;
}
}
if(flag==0)
{
return;
}
}
}
void InsertionSort(int a[],int n)
{
int i,j;
int temp;
for(i=1;i<n;i++)
{
temp=a[i];
for(j=i-1;j>=0;j--) // ADHICHE SAGALE COMPARE KARTA
{
if(a[j]>temp)
{
a[j+1]=a[j];
}
else
{
break;
}
}
}
a[j+1]=temp;
}
void ShellSort(int a[],int n)
{
int step;
int temp;
int i,j;
for(step=n/2;step>0;step=step/2)
{
for(i=step;i<n;i++) // HERE 1 IS REPLACED BY "STEP"
{
temp=a[i];
for(j=i-step;j>=0;j=j-step)
{
if(a[j]>temp)
{
a[j+step]=a[j];
}
else
{
break;
}
}
a[j+step]=temp;
}
}
}
int partition(int a[],int l,int h)
{
int pivot=a[l];
int i=l,j=h;
do
{
do{i++;}while(a[i]<=pivot);
do{j--;}while(a[j]>pivot);
if(i<j)
{
int t;
t=a[i];
a[i]=a[j];
a[j]=t;
}
}while(i<j);
int temp;
temp=a[j];
a[j]=a[l];
a[l]=temp;
return j;
}
void QuickSort(int a[],int l,int h)
{
int j;
if(l<h)
{
j=partition(a,l,h);
QuickSort(a,l,j);
QuickSort(a,j+1,h);
}
}
int main()
{
int a[100];
int n;
cout<<"ENTER TOTAL NUMBER OF ELEMENTS"<<endl;
cin>>n;
cout<<"NOW ENTER THE NUMBERS"<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
//int h=n+1;
//a[h]=INT32_MAX;
//QuickSort(a,0,h);
ShellSort(a,n);
cout<<"THE SORTED LIST IS ===>"<<endl;
for(int i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
}