-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorts.c
152 lines (136 loc) · 2.16 KB
/
sorts.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#import <stdio.h>
#import <stdlib.h>
//#import <string.h>
static int SIZE = 15;
void unsort(int *p);
void print(int *p);
void bubble(int *p);
void insertion(int *p);
void selection(int *p);
void quick(int *p);
void merge(int *p, int n);
void mergeHelp(int *l1, int s1, int *l2, int s2, int *p);
int main() {
int list[15] = { 0, 5, 2, 1, 2, 3, 6, 8, 9, 5, 2, 1, 2, 3, 4};
int *p;
p = list;
bubble(p);
unsort(p);
insertion(p);
unsort(p);
selection(p);
unsort(p);
unsort(p);
merge(p, SIZE);
return 0;
}
void unsort(int *p) {
printf("UNSORT\n");
int i, j;
for(i = SIZE; i > 0; i--) {
j = rand() % SIZE;
p[j] = p[i];
}
}
void print(int *p) {
int i;
for(i = 0; i < SIZE; i++) {
printf("i[%d] = %d\n", i, p[i]);
}
}
void bubble(int *p) {
printf("BUBBLE SORT\n");
int i, j;
for(i = 0; i < SIZE; i++) {
for(j = 0; j < SIZE; j++) {
if(p[i] < p[j]) { //swapping with XOR for funsies
p[i] = p[i] ^ p[j];
p[j] = p[i] ^ p[j];
p[i] = p[i] ^ p[j];
}
}
}
}
void insertion(int *p) {
printf("INSERTION SORT\n");
int i, j;
int temp;
for(i=1;i<SIZE;i++) {
j=i;
while(j>0 && p[j-1] > p[j]) {
temp = p[j];
p[j] = p[j-1];
p[j-1] = temp;
j=j-1;
}
}
}
void selection(int *p) {
printf("SELECTION SORT\n");
int i, j;
int temp, tempMin;
for(i=0;i<SIZE-1;i++) {
tempMin = i;
for(j=i+1; j<SIZE;j++) {
if(p[tempMin] > p[j]) {
tempMin = j;
}
}
temp = p[i];
p[i] = p[tempMin];
p[tempMin] = temp;
}
}
void quick(int *p){}
void merge(int *p, int n) {
printf("n: %d\n", n);
int i;
int *l1, *l2;
int s1, s2;
if(n < 2) {
return;
}
s1 = n/2;
s2 = n-s1;
l1 = (int*)malloc(sizeof(int)* s1);
l2 = (int*)malloc(sizeof(int)* s2);
for(i =0; i<s1; i++){
l1[i] = p[i];
}
for(i=0; i<s2; i++){
l2[i] = p[i+s1];
}
merge(l1,s1);
merge(l2,s2);
mergeHelp(l1, s1, l2, s2, p);
free(l1);
free(l2);
}
void mergeHelp(int *l1, int s1, int *l2, int s2, int *p) {
int i,j,k;
i=0;
j=0;
k=0;
while(i < s1 && j < s2) {
if(l1[i] <= l2[j]) {
p[k] = l1[i];
i++;
k++;
}
else {
p[k] = l2[j];
j++;
k++;
}
}
while (i < s1) {
p[k] = l1[i];
i++;
k++;
}
while (j < s2) {
p[k] = l2[j];
j++;
k++;
}
}