forked from nayyyhaa/C-codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIterative_quick_sort.cpp
More file actions
55 lines (43 loc) · 982 Bytes
/
Iterative_quick_sort.cpp
File metadata and controls
55 lines (43 loc) · 982 Bytes
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
#include <iostream>
using namespace std;
// Function to swap numbers
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
/* This function takes last element as pivot, places the pivot element at its correct position in sorted array, and places
all smaller (smaller than pivot) to left of pivot and all greater elements to right of pivot */
int partition(int arr[], int l, int h)
{
int x = arr[h];
int i = (l - 1);
for (int j = l; j <= h - 1; j++) {
if (arr[j] <= x) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[h]);
return (i + 1);
}
void quickSort(int A[], int start, int end)
{
if (start < end) {
/* Partitioning index */
int p = partition(A, start, end);
quickSort(A, start, p - 1);
quickSort(A, p + 1, end);
}
}
int main()
{
int n = 5;
int arr[n] = { 4, 2, 6, 9, 2 };
quickSort(arr, 0, n - 1);
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}