-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSortCode_zkile.cpp
More file actions
67 lines (51 loc) · 1.17 KB
/
Copy pathQuickSortCode_zkile.cpp
File metadata and controls
67 lines (51 loc) · 1.17 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
63
64
65
66
67
// QuickSortCode_zkile.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
using namespace std;
void swap(int& a, int& b)
{
int temp;
temp = a;
a = b;
b = temp;
}
int partition(int arr[], int begin, int end)
{
int pivot = arr[end];
int i = begin - 1;
for (int j = begin; j < (end); j++)
{
if (arr[j] >= pivot)
{
i += 1;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[end]);
return i + 1;
}
void quickSort(int arr[], int begin, int end)
{
int partIndex;
if (begin >= end)
return;
partIndex = partition(arr, begin, end);
quickSort(arr, begin, (partIndex - 1));
quickSort(arr, (partIndex + 1), end);
}
void output(int arr[], int length)
{
for (int i = 0; i < length; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
int main()
{
int array[10] = {9, 5, 6, 10, 34, 13, 20, 59, 21, 0};
int length = sizeof(array) / sizeof(array[0]);
quickSort(array, 0, length);
output(array, length);
return 0;
}