-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.cpp
More file actions
65 lines (56 loc) · 1.24 KB
/
quickSort.cpp
File metadata and controls
65 lines (56 loc) · 1.24 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
/*********************
Name: Sai Sivakumar
Quick Sort With Arrays C++
Purpose: 1-3 sentences about your program.
**********************/
#include "quickSort.h"
//Public Methods
QuickSort::QuickSort()
{
arraySize = 0;
}
QuickSort::~QuickSort() {}
bool QuickSort::sorter(int size, int fixItUp[])
{
arraySize = size;
solvedArr = fixItUp;
quickSort(ARR_BEGIN, arraySize - 1);
return true;
}
void QuickSort::displaySortedArr()
{
for (int i = 0; i < arraySize; i++)
{
cout<<solvedArr[i]<<endl;
}
}
//Private Methods
void QuickSort::quickSort(int begin, int end)
{
if (begin < end)
{
int p = partition(begin, end);
quickSort(begin, p-1);
quickSort(p+1, end);
}
}
int QuickSort::partition(int start, int finish)
{
int pivot = solvedArr[finish];
int pivotIndex = start;
for(int i = start; i < finish; i++)
{
if(solvedArr[i] <= pivot) {
swap(i, pivotIndex);
pivotIndex++;
}
}
swap(finish, pivotIndex);
return pivotIndex;
}
void QuickSort::swap(int currentLocation, int pivotIndex)
{
int temp = solvedArr[currentLocation];
solvedArr[currentLocation] = solvedArr[pivotIndex];
solvedArr[pivotIndex] = temp;
}