-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort2.c
137 lines (116 loc) · 3.45 KB
/
sort2.c
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
/*
some useful unicode/utf8
▀ ▁ ▂ ▃ ▄ ▅ ▆ ▇ █ ▉ ▊ ▋ ▌ ▍ ▎ ▏ ▐ ░ ▒ ▓ ▔ ▕ ▖ ▗ ▘ ▙ ▚ ▛ ▜ ▝ ▞ ▟
the prototype
▐
▐
▐ ▐ ▐
▐ ▐ ▐ ▐
▐ ▐ ▐ ▐ ▐ ▐ ▐
▐ ▐ ▐ ▐ ▐ ▐ ▐ ▐ ▐ ▐
4 2 1 1 1 2 6 4 3 2
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
// COLORS
#define KNRM "\x1B[0m" // NORMAL
#define KRED "\x1B[31m"// RED
#define KGRN "\x1B[32m"// GREEN
#define KYEL "\x1B[33m"// YELLOW
#define KBLU "\x1B[34m"// BLUE
#define KMAG "\x1B[35m"// MAGENTA
#define KCYN "\x1B[36m"// CYAN
#define KWHT "\x1B[37m"// WHITE
/// Draws a Array of Integers as a Bargraph ( UTF-8 capable Console Required! )
/// takes (Array of Integers 'arr', int 'size' (Number of Elements in Array),
/// int 'hv' HighestValue in Array(0-99), int 'active' Row in Array (marked RED in Bargraph))
void drawGraph( int *arr, int size, int hv, int active){
// Lines
for (int i = hv; i > 0; i--){
// Rows
for (int j = 0; j < size; j++){
if ( arr[j] >= i ){
if (active == j){
printf("%s▐ %s", KRED, KNRM);
}else{
printf("▐ ");
}
}else{
printf(" ");
}
}
printf("\n");
}
// print array values
for (int i = 0; i < size; i++ ){
if ( arr[i] >= 10 ){
printf("%d ", arr[i]);
}else{
printf("%d ", arr[i]);
}
}
printf("\n");
}
// compare function of cocktailsort
int compare( const void* a, const void* b)
{
int int_a = * ( (int*) a );
int int_b = * ( (int*) b );
if ( int_a == int_b ) return 0;
else if ( int_a < int_b ) return -1;
else return 1;
}
// can be any swap function. This swap is optimized for numbers.
void swap(int *x, int *y) {
if(x == y)
return;
*x ^= *y;
*y ^= *x;
*x ^= *y;
}
// Cocktailsorts an Array of Integers
void cocktailsort(int *array, size_t n) {
// ivars for Graph drawing
char command[50];
strcpy( command, "clear" );
while(1) {
// packing two similar loops into one
char flag;
size_t start[2] = {1, n - 1},
end[2] = {n, 0},
inc[2] = {1, -1};
for(int it = 0; it < 2; ++it) {
flag = 1;
for(int i = start[it]; i != end[it]; i += inc[it]){
if(array[i - 1] > array[i]) {
swap(array + i - 1, array + i);
if (it == 0){// draw Graph with Active Colum sorting to the Right
drawGraph(array, 30, 30, i);
usleep(100000);
}else{// draw Graph with Active Colum sorting to the Left
drawGraph(array, 30, 30, i - 1);
usleep(100000);
}
if(array[i - 1] > array[i]) system(command); // clear the console
flag = 0;
}
}
if(flag) return;
}
}
}
int main(){
// create an array of values
int values[] = {25,4,8,11,17,13,6,20,10,29,12,23,2,15,3,7,1,19,18,16,14,9,5,30,22,27,21,24,28,26};
drawGraph(values, 30, 30, 0);// draw the unsorted graph first
// wait for user to start the sorting
printf("Press a Key to start Sorting\n");
getchar();
// start sorting
cocktailsort(values, 30);
//end
return 0;
}