-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathquick_sort.cpp
66 lines (55 loc) · 933 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
60
61
62
63
64
65
66
# include <iostream>
using namespace std;
int Partition(int arr[], int s, int e)
{
int pivot = arr[e];
int pIndex = s;
for(int i = s;i<e;i++)
{
if(arr[i]<pivot)
{
int temp = arr[i];
arr[i] = arr[pIndex];
arr[pIndex] = temp;
pIndex++;
}
}
int temp = arr[e];
arr[e] = arr[pIndex];
arr[pIndex] = temp;
return pIndex;
}
void QuickSort(int arr[], int s, int e)
{
if(s<e)
{
int p = Partition(arr,s, e);
QuickSort(arr, s, (p-1));
QuickSort(arr, (p+1), e);
}
}
int main()
{
int size=0;
cout<<"Enter Size of array: "<<endl;
cin>>size;
int myarray[size];
cout<<"Enter "<<size<<" integers in any order: "<<endl;
for(int i=0;i<size;i++)
{
cin>>myarray[i];
}
cout<<"Before Sorting"<<endl;
for(int i=0;i<size;i++)
{
cout<<myarray[i]<<" ";
}
cout<<endl;
QuickSort(myarray,0,(size-1));
cout<<"After Sorting"<<endl;
for(int i=0;i<size;i++)
{
cout<<myarray[i]<<" ";
}
return 0;
}