-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorts.c
122 lines (110 loc) · 1.81 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sorts.h"
int comparator (char *x, char *y){
int i = 0;
while((x[i]!= '\0')&&(y[i]!= '\0')){
if (x[i] > y[i]){
return 1;
}
if (x[i] < y[i]){
return 0;
}
i++;
}
if (x[i] == '\0'){
return 0;
}
if (y[i] == '\0'){
return 1;
}
}
void bubblesort(char **str, int size){
int x = 0,y = 0;
for (x = 0; x < size-1; x++){
for (y = 0; y < size-1; y++){
if (comparator(str[y], str[y+1]) > 0){
char *strN = str[y];
str[y] = str[y+1];
str[y+1] = strN;
}
}
}
}
void insertion(char **str, int size){
int x = 0,y = 0;
for(x = 1; x < size; x++){
for(y = x; y > 0 && comparator(str[y-1], str[y]) > 0; y--){
char *strN = str[y];
str[y] = str[y-1];
str[y-1] = strN;
}
}
}
void quicksort(char **str, int size){
char *elem;
elem = str[size / 2];
int x = 0, y = size;
while(x <= y) {
while(comparator(elem, str[x]))
x++;
while(comparator(str[y], elem))
y--;
if (x <= y) {
char *strN = str[x];
str[x] = str[y];
str[y] = strN;
x++;
y--;
}
}
if (y > 0)
quicksort(str, y);
if (size > x)
quicksort(str + x, size - x);
}
void mergesort(char **str, int n, int m){
if (n < m){
int q = (m+n)/2;
mergesort(str, n, q);
mergesort(str, q+1, m);
merge(str, n, q, m);
}
}
void merge(char **str, int n, int q, int m){
int i, j, k;
int n1 = q-n+1;
int n2 = m-q;
char *Left[n1], *Right[n2];
for (i = 0; i < n1; i++){
Left[i] = str[n+i];
}
for (j = 0; j < n2; j++){
Right[j] = str[q+1+j];
}
i = 0;
j = 0;
k = n;
while (i < n1 && j < n2){
if (comparator(Left[i], Right[j]) <= 0){
str[k] = Left[i];
i++;
}
else{
str[k] = Right[j];
j++;
}
k++;
}
while (i < n1){
str[k] = Left[i];
i++;
k++;
}
while (j < n2){
str[k] = Right[j];
j++;
k++;
}
}