-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathQuickSort.cpp
63 lines (61 loc) · 1.18 KB
/
QuickSort.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
60
61
62
63
#include<bits/stdc++.h>
using namespace std;
void swap(int *a,int *b)
{
int t=*a;
*a=*b;
*b=t;
}
int partition(int low,int high,int array[])
{
int i=low;
int j=high;
int pivot=low;
while(i<j)
{
do{
i++;
}while(array[i]<=array[pivot]);
while (array[j] >array[pivot])
{
j--;
}
if(i<j)
{
swap(&array[i],&array[j]);
}
}
swap(&array[low],&array[j]);
return j;
}
void quicksort(int low,int high ,int array[] )
{
if(low<high)
{
int j=partition(low,high,array);
quicksort(low,j,array);
quicksort(j+1,high,array);
}
}
int main()
{
int n,array[25];
cout<<"Enter Number of items in array:"<<endl;
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter Element"<<(i+1)<<":"<<endl;
cin>>array[i];
}
cout << "Before Sorting:" << endl;
for (int i = 0; i < n; i++)
{
cout << array[i] << "\t";
}
quicksort(0,n-1,array);
cout<<"\nAfter Sorting:"<<endl;
for(int i=0;i<n;i++)
{
cout<<array[i]<<"\t";
}
}