-
Notifications
You must be signed in to change notification settings - Fork 2
/
sorts.h
150 lines (126 loc) · 4.33 KB
/
sorts.h
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
// Created by ugur on 29.09.2019.
//
#ifndef ALGOI_SORTS_H
#define ALGOI_SORTS_H
#include <iostream>
#include <cmath>
template <class T>
void insertionSort(T *arr, size_t length, char mode = 'f'){
/*
* Uses the version in Skiena's book.
* Uses insertion sort algorithm to sort the given array. It works in place.
* First parameter is the array to sort.
* Second parameter is the length of the array.
* Third parameter is the mode for sorting. If user supplies a 'r' value, sorting is in descending order.
*/
for(size_t i = 1; i < length; i++){
if(mode == 'r'){
for(size_t j = i; j > 0; j--){
if(arr[j] >= arr[j - 1]){
std::swap(arr[j], arr[j - 1]);
} else break;
}
} else {
for(size_t j = i; j > 0; j--){
if(arr[j] <= arr[j - 1]){
std::swap(arr[j], arr[j - 1]);
} else break;
}
}
}
}
template <class T>
void insertionSortv2(T *arr, size_t length, char mode = 'f'){
/*
* Uses the version in CLRS.
* Uses insertion sort algorithm to sort the given array. It works in place.
* First parameter is the array to sort.
* Second parameter is the length of the array.
* Third parameter is the mode for sorting. If user supplies a 'r' value, sorting is in descending order.
*/
for(int j = 1; j < length; j++){
T key = arr[j];
int i = j - 1;
while(i >= 0 && (arr[i] > key)){
arr[i + 1] = arr[i];
i = i - 1;
}
arr[i + 1] = key;
}
}
template <class T>
void merge(T *arr, int start_point, int end_point1, int end_point2){
/*
* Merges two sorted arrays and creates a new sorted array. Process happens in place.
*/
int first_length = end_point1 - start_point + 1;
int second_length = end_point2 - end_point1;
T left_arr[first_length];
T right_arr[second_length];
for(int i = 0; i < first_length; i++){
left_arr[i] = arr[start_point + i];
}
for(int i = 0; i < second_length; i++){
right_arr[i] = arr[end_point1 + 1 + i];
}
int i = 0;
int j = 0;
for(int k = start_point; k <= end_point2; k++){
if(i == first_length){
arr[k] = right_arr[j];
j += 1;
continue;
}
if(j == second_length){
arr[k] = left_arr[i];
i += 1;
continue;
}
if(left_arr[i] <= right_arr[j]){
arr[k] = left_arr[i];
i += 1;
} else {
arr[k] = right_arr[j];
j += 1;
}
}
}
template <class T>
void mergeSort(T *arr, int start_point, int end_point){
/*
* Uses the version in Uğur Ali Kaplan's head which is based on CLRS. (Merging part)
* Recursively sorts the given array with merge sort.
* @start_point: Starting point of the part we focus on in the given large array
* @end_point: End point of the part we focus on in the given large array
*/
if(start_point < end_point){
int new_end_pt = int(floor(double(start_point + end_point) / 2.0));
mergeSort(arr, start_point, new_end_pt);
mergeSort(arr, new_end_pt + 1, end_point);
merge(arr, start_point, new_end_pt, end_point);
}
}
template <class T>
int partition(T *arr, int start_point, int end_point){
T pivot_element = arr[end_point]; // x holds the value in the highest given index
int i = start_point - 1; // i is before the beginning of array
for(int j = start_point; j < end_point; j++){ // go through the array one by one
if (arr[j] <= pivot_element){ // if a value is less than or equal to to the pivot element
i += 1; // increase counter
std::swap(arr[j], arr[i]); // change the values of the last element and the current element
}
}
std::swap(arr[i + 1], arr[end_point]); // swap values of pivot_element and the last element
return i + 1; // return pivot_point
}
template <class T>
void quickSort(T *arr, int start_point, int end_point){
if (start_point < end_point){
int pivot_point = partition(arr, start_point, end_point);
quickSort(arr, start_point, pivot_point - 1);
quickSort(arr, pivot_point + 1, end_point);
}
else return;
}
#endif //ALGOI_SORTS_H