-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathquickSort.cpp
More file actions
56 lines (46 loc) · 964 Bytes
/
Copy pathquickSort.cpp
File metadata and controls
56 lines (46 loc) · 964 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
56
#include <iostream>
using namespace std;
void swap(int* x, int* y);
void quickSort(int arr[], int low, int high, int dimArr);
int partition (int arr[], int low, int high);
int main(){
int array[] = {8676, 798, 5, 455, 222};
int dim = 5;
quickSort(array, 0, dim-1, dim);
for(int i=0; i<dim; i++)
{
cout << array[i] << " ";
}
cout << endl;
return 0;
}
void swap(int* x, int* y)
{
int w = *x;
*x = *y;
*y = w;
}
void quickSort(int arr[], int low, int high, int dimArr)
{
if(low<high)
{
int part = partition(arr, low, high);
quickSort(arr, low, part - 1, dimArr);
quickSort(arr, part + 1, high, dimArr);
}
}
int partition (int arr[], int low, int high)
{
int piv = arr[high];
int i = (low - 1);
for (int j = low; j <= high- 1; j++)
{
if (arr[j] <= piv)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}