-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflipflop.cpp
More file actions
81 lines (61 loc) · 1.32 KB
/
flipflop.cpp
File metadata and controls
81 lines (61 loc) · 1.32 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
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
#include <math.h>
#include <iostream>
#include <unistd.h>
using namespace std;
/*
reverse() was a function that I was going to use to sort the last indexes of the array.
I soon learned that this method kinda sucked. It remains because I may need it in the future.
*/
void reverse(uint32_t a[],uint32_t n) {
uint32_t temp = 0;
for(uint32_t i = 0; i < n/2 ; i++) {
temp = a[i];
a[i] = a[n-i -1];
a[n-i -1] = temp;
}
}
/*
swap() just swaps two given indexes of an array
*/
void swap(uint32_t &a,uint32_t &b) {
uint32_t c = a;
a = b;
b = c;
}
/*
flipFlopSort() takes a pointer to an array and the length of the array and sorts the
array in ~= O(n^2.7) time.
*/
void flipFlopSort(uint32_t *p, uint32_t length) {
if (length == 2){
if(*(p) > *(p+1)){
swap(*(p),*(p+1));
}
}
else if (length == 1) {
return;
}
else {
uint32_t nf = (2*length+3-1)/3;
flipFlopSort(p, nf);
flipFlopSort(p+length-nf, nf);
flipFlopSort(p, nf);
}
}
int main() {
uint32_t n;
cin >> n;
uint32_t array[n];
uint32_t* p = array;
// This part is to get the input and to initalize everything
for (uint32_t i = 0; i < n ; i++) {
cin >> array[i];
}
// Runs flipsort to sort the array
flipFlopSort(p, n);
// This outputs the array
for (uint32_t i = 0; i < n ; i++){
cout << array[i] << ' ';
}
return 0;
}