-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.cpp
More file actions
81 lines (69 loc) · 1.5 KB
/
Copy pathquickSort.cpp
File metadata and controls
81 lines (69 loc) · 1.5 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
81
#include<iostream>
using namespace std;
int Partition(int A[],int a ,int b)
{
int pivot = A[a];
int left = a + 1;
int right = b;
do{
while (( left <= right )&&(A[left] <= pivot ))
left ++;
while (( left <= right )&&(A[right]> pivot ))
right --;
if (left < right)
{
swap(A[left], A[right]);
left ++;
right --;
}
}
while (left <= right);
swap (A[a], A[right]) ;
return right;
}
void quickSort2(int A[],int a,int b)
{
if (a < b){
int k = Partition(A,a,b);
if (a <= k - 1)
quickSort2(A,a,k-1);
if (k + 1 <= b)
quickSort2(A,k+1,b);
}
}
void quickSort3(int A[],int a,int b)
{
if (a < b){
int k = Partition(A,a,b);
quickSort3(A,a,k-1);
quickSort3(A,k+1,b);
}
}
void quickSort4(int s[], int first, int last)
{
int mid,tmp,left,right;
left=first;
right=last;
mid=s[(first+last)/2];
do{
while(s[left]<mid)left++;
while(s[right]>mid)right--;
if(left<=right){
tmp=s[left];
s[left]=s[right];
s[right]=tmp;
left++;
right--;
}
}
while(left<=right);
if(left<last)quickSort4(s,left,last);
if(first<right)quickSort4(s,first,right);
}
int main()
{
int a[7] = {1,4,7,6,8,3,2};
quickSort2(a,0,6);
for(int i=0;i<7;i++)cout<<a[i]<<" ";
return 0;
}