-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path44-quick_sort.cpp
More file actions
79 lines (76 loc) · 1.02 KB
/
44-quick_sort.cpp
File metadata and controls
79 lines (76 loc) · 1.02 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
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
class quick
{
public:
int key,n;
int ar[100];
quick()
{
key=0;
}
int partition(int ar[],int low,int high)
{
int i,j,temp;
key=ar[low];
i=low+1;
j=high;
while(1)
{
while(i<high&&key>=ar[i])
i++;
while(key<ar[j])
j--;
if(i<j)
{
temp=ar[i];
ar[i]=ar[j];
ar[j]=temp;
}
else
{
temp=ar[low];
ar[low]=ar[j];
ar[j]=temp;
}
return j;
}
}
void sort(int a[],int low, int high)
{
int j;
if(low<high)
{
j=partition(a,low,high);
sort(a,low,j-1);
sort(a,j+1,high);
}
}
void getvalue()
{
cout<<"enter total elements\n";
cin>>n;
cout<<"enter elements\n";
for(int i=0;i<n;i++)
cin>>ar[i];
}
void display()
{
for(int i=0;i<n;i++)
cout<<ar[i]<<endl;
}
};
int main()
{
quick obj;
obj.getvalue();
cout<<"elements before sorting\n";
obj.display();
obj.sort(obj.ar,0,obj.n-1);
cout<<"elements after sorting\n";
obj.display();
getch();
return 0;
}