-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicksort.cpp
67 lines (49 loc) · 1.64 KB
/
quicksort.cpp
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
// Quicksort -- in progress
#include <iostream>
using namespace std;
void quicksort(int array[], int startSize, int endSize);
//void partition()...
int main()
{
const int arraySize = 10;
int array[arraySize] = {37, 3, 12, 65, 1, 89, 42, 22, 9, 17};
// call quicksort function
quicksort(array, arraySize);
}
/*
sort the first element
sort the first element of the left subarray, sort the first element of the right subarray
sort the first element of the left subarray of the left subarray, sort the first element of the right subarray of the right subarray
* */
// 1. partitioning step -- sort the first element
void quicksort(int a[], int start, int end)
{
// 2. recursive step -- perform step 1 (partitioning step) on each subarray.
// partition will feed subarrays into quicksort f.
partition(int a[start], &start)
}
void partition(...)
{
// from right to left
for(int i=end; i < start; i--)
{
if(i < start) // when number less than start number is hit, swap them
{
// swap
int hold1 = array[i]; // int hold = 17;
array[i] = array[start]; // array[9] = 37;
array[start] = hold1; // array[start] = 17;
}
}
// from left to right
for(int j = start + number of numbers at the beginning you skip; j >= end; j++)
{
if(j > array[start]) // when number more than my number is hit, swap them
{
// swap
int hold2 = array[j];
array[j] = array[start];
array[start] = hold2;
}
}
}