forked from fnplus/Algorithms-Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuick_Sort.cpp
59 lines (53 loc) · 880 Bytes
/
Quick_Sort.cpp
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
//partitioning algorithm
int partition(int* A,int low, int high)
{
int pivot,temp,i;
pivot=A[high];
i=low-1;
for(int j=low;j<=high-1;j++)
{
if(A[j]<=pivot)
{
i++;
temp=A[j];
A[j]=A[i];
A[i]=temp;
}
}
temp=A[i+1];
A[i+1]=A[high];
A[high]=temp;
return i+1;
}
//recursive function quicksort
void quickSort(int* A, int low, int high)
{
int temp,pivot,i,j,mid;
if(low<high)
{
mid=partition(A,low,high);
quickSort(A,low,mid-1);
quickSort(A,mid+1,high);
}
};
//driver function
int main()
{
int n,i;
int* A;
cout << "enter number of elements in the array : ";
cin >> n;
A=new int(n);
cout << " enter space separated array elements : ";
for(i=0;i<n;i++)
{
cin >> A[i];
}
quickSort(A,0,n-1);
cout << "sorted array is : ";
for(i=0;i<n;i++)
{
cout << A[i] << " ";
}
cout << endl;
}