-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQuick.cpp
More file actions
54 lines (44 loc) · 1.11 KB
/
Copy pathQuick.cpp
File metadata and controls
54 lines (44 loc) · 1.11 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
// Quicksort
#include <iostream>
#include <algorithm>
using namespace std;
template <class T>
T partition(T arr[], int start, int end)
{
T pivotValue = arr[start];
T pivotPosition = start;
for (int pos = start + 1; pos <= end; pos++)
{
if (arr[pos] < pivotValue)
{
swap(arr[pivotPosition + 1], arr[pos]);
swap(arr[pivotPosition], arr[pivotPosition + 1]);
pivotPosition ++;
}
}
return pivotPosition;
}
template <class T>
T quickSort(T arr[], int start, int end)
{
if (start < end) //test for base case start == end
{
int p = partition(arr, start, end);
quickSort(arr, start, p - 1);
quickSort(arr, p + 1, end);
}
return 0;
}
/*
int main()
{
int array[SIZE] = {100, 35, 7, 21, 89, 10, 148, 983, 33, 29};
for (int k = 0; k < SIZE; k++)
cout << array[k] << " ";
cout << endl;
quickSort(array, 0, SIZE-1);
for (int k = 0; k < SIZE; k++)
cout << array[k] << " ";
cout << endl;
return 0;
}*/