-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert.c
97 lines (87 loc) · 1.96 KB
/
insert.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#define less(a,b) ((a)<(b))
#define swap(a,b) do{char t=a; a=b; b=t;}while(0)
void insert_sort(char *l,char *r) {
for(char *px=l+1; px<=r; px++)
if(less(*px,*l))
swap(*px,*l);
for(char *px=l+2; px<=r; px++) {
char h=*px;
char *p=px-1;
while(less(h,*p)) {
*(p+1)=*p;
p-=1;
}
*(p+1)=h;
for(int i=0;i<10;i++) {
usleep(50000);
printf("\033[4;%dH%c ",1+(2*i),l[i]);
fflush(stdout);
}
}
}
void select_sort(char *a,int len) {
register int i,j;
for(i=0;i<len;i++)
for(j=i+1;j<len;j++) {
if(*(a+i)>*(a+j))
swap(*(a+i),*(a+j));
for(int i=0;i<10;i++) {
usleep(50000);
printf("\033[7;%dH%c ",1+(2*i),a[i]);
fflush(stdout);
}
}
}
void cocktail_sort(char *a,int len) {
do{
char flag;
int start[2]={1,len-1},
end[2]={len,0},
inc[2]={1,-1};
for(int i=0; i<2; ++i) {
flag=1;
for(int j=start[i]; j!=end[i]; j+=inc[i])
if(a[j-1]>a[j]) {
swap(a[j-1],a[j]);
flag=0;
}
if(flag)
return;
for(int i=0;i<10;i++) {
usleep(50000);
printf("\033[10;%dH%c ",1+(2*i),a[i]);
fflush(stdout);
}
}
}while(1);
}
int main() {
FILE *in = fopen("data", "rb");
char *vals = (char*)mmap(0, 10*sizeof(int),
PROT_READ, MAP_FILE | MAP_PRIVATE, fileno(in),0);
fclose(in);
char *a = (char*)malloc(10*sizeof(char));
char *b = (char*)malloc(10*sizeof(char));
char *c = (char*)malloc(10*sizeof(char));
register int i;
for(i=0; i<10; i++) {
a[i] = vals[i];
b[i] = vals[i];
c[i] = vals[i];
printf("%c ",vals[i]);
}
printf("\n\n-INSERTION-SORT----\n");
insert_sort(a, a+10-1);
printf("\n\n-SELECTION-SORT----\n");
select_sort(b,10);
printf("\n\n-COCKTAIL-SORT-----\n");
cocktail_sort(c,10);
free(a);
free(b);
free(c);
return 0;
}