-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quick_Sort.cpp
80 lines (71 loc) · 1.33 KB
/
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
///Time Complexity: O(n^2) for the worst case
///Time Complexity: O(n(log(n))) for the best case
/*
Auther : Abdullah Al Masum
*/
#include <bits/stdc++.h>
using namespace std;
#define MAXX 10000000
int Arr[MAXX];
void printArray(int A[], int N)
{
for (int i = 0; i < N; i++)
{
cout << A[i] << " ";
}
cout << endl;
}
void swapp(int *A, int i, int j)
{
int temp = A[j];
A[j] = A[i];
A[i] = temp;
}
int partition(int A[], int low, int high)
{
int pivot = A[low];
int i = low + 1;
int j = high;
do
{
while (A[i] <= pivot)
{
i++;
}
while (A[j] > pivot)
{
j--;
}
if (i < j)
{
swapp(A, i, j);
}
} while (i < j);
swapp(A, low, j);
return j;
}
void QuickSort(int A[], int low, int high)
{
if (low < high)
{
int partitionIndex = partition(A, low, high);
QuickSort(A, low, partitionIndex - 1);
QuickSort(A, partitionIndex + 1, high);
}
}
int main()
{
//takeInput();
int size;
cin >> size;
for (int i = 0; i < size; i++)
{
cin >> Arr[i];
}
cout << "Before Sorting: " << endl;
printArray(Arr, size);
QuickSort(Arr, 0, size - 1);
cout << "After Sorting:" << endl;
printArray(Arr, size);
return 0;
}