-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhybrid.h
More file actions
55 lines (43 loc) · 1.4 KB
/
hybrid.h
File metadata and controls
55 lines (43 loc) · 1.4 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
//
// Created by lenovo on 16.12.2022.
//
#ifndef DATABASES_PROJECT_HYBRID_H
#define DATABASES_PROJECT_HYBRID_H
#include "quicksort.h"
#include "insertion.h"
int div(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 hybridSort(std::vector<int> &array,int startIndex,int endIndex){
int pivot;
if(endIndex-startIndex < 50) {
for (int i = startIndex+1 ; i <= endIndex ; i++) {
int val = array[i];
int current = i;
while ( (current > 0) && array[current - 1] > val ) {
array[current] = array[current - 1];
current--;
}
array[current] = val;
}
}
else{
pivot = div(array,startIndex,endIndex);
hybridSort(array,startIndex,pivot);
hybridSort(array,pivot+1,endIndex);
}
}
#endif //DATABASES_PROJECT_HYBRID_H