-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathquick_sort.cpp
More file actions
63 lines (62 loc) · 1.15 KB
/
quick_sort.cpp
File metadata and controls
63 lines (62 loc) · 1.15 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
#include<iostream>
#include "181B081_LAB4_1.h"
using namespace std;
int partition_p(int *a,int low,int high)
{
int left,right,temp;
left=low+1;
right=high;
int pivot=a[low];
while(left<=right)
{
while(a[left]<=pivot && left<=high)
{
left++;
}
while(a[right]>pivot && right>=low+1)
{
right--;
}
if(left<=right)
{
temp=a[right];
a[right]=a[left];
a[left]=temp;
left++;
right--;
}
}
temp=a[right];
a[right]=pivot;
a[low]=temp;
for(int k=0;k<=high;k++)
cout<<a[k]<<" ";
cout<<"\n";
return right;
}
void q_sort(int*a,int low,int high)
{
if(low<high)
{
int new_loc;
new_loc=partition_p(a,low,high);
q_sort(a,low,new_loc-1);
q_sort(a,new_loc+1,high);
}
}
int main()
{
int n;
cin>>n;
int *a;
a=ramu(n);
int i;
cout<<"Array without sorting:\n";
for(i=0;i<n;i++)
cout<<a[i]<<" "<<endl;
q_sort(a,0,n-1);
cout<<"final"<<"\n";
for(i=0;i<n;i++)
cout<<a[i]<<" "<<endl;
return 0;
}