forked from dharmanshu1921/Website-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
62 lines (59 loc) · 1.19 KB
/
QuickSort.cpp
File metadata and controls
62 lines (59 loc) · 1.19 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
#include <iostream>
using namespace std;
int partition(int a[], int low, int high)
{
int i, j, temp, key;
key = a[low];
i = low;
j = high + 1;
while (i <= j)
{
do
i++;
while (key >= a[i] && i <= high);
do
j--;
while (key < a[j]);
if (i < j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
temp = a[low];
a[low] = a[j];
a[j] = temp;
return j;
}
void qs(int a[], int low, int high)
{
int mid;
if (low < high)
{
mid = partition(a, low, high);
qs(a, low, mid - 1);
qs(a, mid + 1, high);
}
}
int main()
{
int n, i;
cout << "Quicksort Test\n";
// Input no.of Elements
cout << "\nEnter the number of elements\n";
cin >> n;
// Created array of n elements
int arr[n];
for (i = 0; i < n; i++)
cin >> arr[i];
cout << "\nThe elements are\n";
for (i = 0; i < n; i++)
cout << arr[i] << " ";
// Calling QuickSort
qs(arr, 0, n - 1);
// Printing Sorted Array
cout << "\nThe elements after sorting \n";
for (i = 0; i < n; i++)
cout << arr[i] << " ";
}