-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick-sort.cpp
More file actions
81 lines (78 loc) · 1.27 KB
/
Copy pathquick-sort.cpp
File metadata and controls
81 lines (78 loc) · 1.27 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
80
#include <iostream>
using namespace std;
void quick(int A[], int n,int beg, int end,int *loc)
{
int left,right,temp;
left=beg;
right=end;
*loc=beg;
step2:
while(A[*loc]<=A[right]&&*loc!=right)
right--;
if(*loc==right)
return;
if(A[*loc]>A[right])
{
temp=A[*loc];
A[*loc]=A[right];
A[right]=temp;
*loc=right;
}
goto step3;
step3:
while(A[left]<=A[*loc]&&left!=*loc)
left++;
if(*loc==left)
return;
if(A[left]>A[*loc])
{
temp=A[left];
A[left]=A[*loc];
A[*loc]=temp;
*loc=left;
}
goto step2;
}
void quick_sort(int A[], int n)
{
int beg, end, loc,top=-1;
int lower[10],upper[10];
if(n>1)
{
top++;
lower[top]=0;
upper[top]=n-1;
}
while(top!=-1)
{
beg=lower[top];
end=upper[top];
top--;
quick(A,n,beg,end,&loc);
if(beg<loc-1)
{
top++;
lower[top]=beg;
upper[top]=loc-1;
}
if(loc+1<end)
{
top++;
lower[top]=loc+1;
upper[top]=end;
}
}
}
int main()
{
int i,n;
cout<<"Enter the size of Array";
cin>>n;
int A[n];
cout<<"Enter the values in array";
for(i=0;i<=n-1;i++)
cin>>A[i];
quick_sort(A,n);
for(i=0;i<=n-1;i++)
cout <<A[i]<<" ";
}