-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprg24.cpp
More file actions
58 lines (58 loc) · 945 Bytes
/
Copy pathprg24.cpp
File metadata and controls
58 lines (58 loc) · 945 Bytes
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
//quicksort
#include<iostream>
using namespace std;
class quicksort{
public: void sort(int arr[20],int low,int high);
int partition(int arr[20],int low,int high);
};
void quicksort::sort(int arr[20],int low,int high)
{
int keypos;
if(low<high)
{
keypos=partition(arr,low,high);
sort(arr,low,keypos);
sort(arr,keypos+1,high);
}
}
int quicksort::partition(int arr[20],int low,int high)
{
int i,j,key,temp;
i=low+1;
j=high;
key=arr[low];
int flag=1;
while(flag){
while(key>arr[i] && i<high){
i++;
}
while(key<arr[j])
j--;
if(i<j){
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
else
flag=0;
}
temp=arr[j];
arr[j]=arr[low];
arr[low]=temp;
return j;
}
main()
{
quicksort q;
int i,n;
int arr[20];
cout<<"Enter the value of n: ";
cin>>n;
cout<<"Enter n array elements:\n";
for(i=0;i<n;i++)
cin>>arr[i];
q.sort(arr,0,n-1);
cout<<"\nAfter sorting\n";
for(i=0;i<n;i++)
cout<<arr[i]<<"\n";
}