-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms.cpp
More file actions
50 lines (41 loc) · 1.02 KB
/
algorithms.cpp
File metadata and controls
50 lines (41 loc) · 1.02 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
#define SIZE 256
int* swap(int (&array)[SIZE], int value1, int value2) {
int tmp;
int lowest = array[value2];
tmp = array[value1];
array[value1] = lowest;
array[value2] = tmp;
return array;
}
int* selectionSort(int (&array)[SIZE], int n, int &lowest) {
int lowestValue = array[n];
int lowest_i = n;
lowest = n;
for (int i = n+1; i < SIZE; i++) {
if (array[i] < lowestValue) {
lowest = i;
lowestValue = array[i];
lowest_i = i;
}
}
swap(array, lowest_i, n);
return array;
}
int* bubbleSort(int (&array)[SIZE], int n) {
for (int i = 0; i < SIZE - n - 1; i++) {
if (array[i] > array[i+1]) {
swap(array, i, i+1);
}
}
return array;
}
int* insertionSort(int (&array)[SIZE], int n, int ¤t) {
for (int i = n; i > 0; i--) {
if (array[i] < array[i - 1]) {
current = i;
swap(array, i, i-1);
} else
return array;
}
return array;
}