-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorts.cpp
More file actions
47 lines (41 loc) · 881 Bytes
/
sorts.cpp
File metadata and controls
47 lines (41 loc) · 881 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
45
46
47
#include "sorts.hpp"
void bubble_sort(int *ar, int size){
for (int j = 1; j < size; j++){
for(int i = 0; i < size - j; i++){
if (ar[i] > ar[i + 1])
swap(ar[i], ar[i + 1]);
}
}
}
void selection_sort(int* ar, int size){
for (int i = 0; i < size; i++){
swap(ar[min_ind(ar, i, size)], ar[i]);
}
}
void insertion_sort(int *ar, int size){
int key, k;
for (int i = 1; i < size; i++){
key = ar[i];
k = i;
for (int j = i - 1; ar[j] < key && j >= 0; j--, k--){
ar[k] = ar[j];
}
ar[k] = key;
}
}
void count_sort(int *ar, int size, int min, int max){
int null_ar[max];
for (int i = min; i <= max; i++){
null_ar[i] = 0;
}
for (int i = 0; i < size; i++){
null_ar[ar[i]]++;
}
int k = 0;
for (int i = min; i <= max; i++){
for (int j = k; j < k + null_ar[i]; j++){
ar[j] = i;
}
k += null_ar[i];
}
}