-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquicksort.h
More file actions
44 lines (32 loc) · 1011 Bytes
/
Copy pathquicksort.h
File metadata and controls
44 lines (32 loc) · 1011 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
//
// Created by lenovo on 8.12.2022.
//
#ifndef DATABASES_PROJECT_QUICKSORT_H
#define DATABASES_PROJECT_QUICKSORT_H
#include <iostream>
#include <vector>
int partition(std::vector<int> &array, int startIndex, int endIndex) { //we need start and end index
int pivotIndex = (startIndex + endIndex) / 2;
int pivot = array[pivotIndex];
while (true) {
while (array[startIndex] < pivot) {
startIndex++;
}
while (array[endIndex] > pivot) {
--endIndex;
}
if (startIndex >= endIndex) {
return endIndex;
}
std::swap(array[startIndex],array[endIndex]);
}
}
void quickSort(std::vector<int> &array,int startIndex,int endIndex){
int pivot;
if(startIndex<endIndex){
pivot = partition(array,startIndex,endIndex);
quickSort(array,startIndex,pivot);
quickSort(array,pivot+1,endIndex);
}
}
#endif //DATABASES_PROJECT_QUICKSORT_H