forked from LawJarp-A/DemoI-O
-
Notifications
You must be signed in to change notification settings - Fork 0
/
a.c
146 lines (114 loc) · 2.46 KB
/
a.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
#include <stdlib.h>
#include <stdio.h>
#include "a.h"
//Swap function
void swap(long int *a, long int *b) {
long int t = *a;
*a = *b;
*b = t;
}
// Merge Sort
void mergeA(long int a[], long int p, long int q, long int r, long int *num_cmp)
{
long int n1 = q - p + 1;
long int n2 = r - q;
long int L[n1], M[n2];
for (long int i = 0; i < n1; i++)
L[i] = a[p + i];
for (long int j = 0; j < n2; j++)
M[j] = a[q + 1 + j];
long int i, j, k;
i = 0;
j = 0;
k = p;
while (i < n1 && j < n2)
{
*num_cmp = *num_cmp + 1;
if (L[i] <= M[j])
{
a[k] = L[i];
i++;
}
else
{
a[k] = M[j];
j++;
}
k++;
}
while (i < n1)
{
a[k] = L[i];
i++;
k++;
}
while (j < n2)
{
a[k] = M[j];
j++;
k++;
}
}
void mergeSort(long int a[], long int l, long int r, long int *num_cmp)
{
if (l < r)
{
long int m = l + (r - l) / 2;
mergeSort(a, l, m, num_cmp);
mergeSort(a, m + 1, r, num_cmp);
mergeA(a, l, m, r, num_cmp);
}
}
// Bubble Sort
void bubbleSort(long int a[], long int n, long int *num_cmp)
{
for (long int i = 0; i < n - 1; i++)
{
for (long int j = 0; j < n - 1 - i; j++)
{
// printf("%li %li\n", a[j], j);
*num_cmp = *num_cmp + 1;
if (a[j] > a[j + 1])
{
swap(&a[j], &a[j + 1]);
}
}
}
}
// Select Sort
void selectionSort(long int a[], long int n, long int *num_cmp)
{
long int i, j, min;
for (i = 0; i < n - 1; i++)
{
min = i;
for (j = i + 1; j < n; j++)
*num_cmp = *num_cmp + 1;
if (a[j] < a[min])
{
min = j;
}
swap(&a[min], &a[i]);
}
}
// Quick Sort
int partition(long int a[], long int l, long int h, long int *num_cmp) {
long int piv = a[h];
long int i = (l - 1);
for (long int j = l; j < h; j++) {
*num_cmp = *num_cmp + 1;
if (a[j] <= piv) {
i++;
swap(&a[i], &a[j]);
}
}
swap(&a[i + 1], &a[h]);
return (i + 1);
}
void quickSort(long int a[], long int l, long int h, long int *num_cmp) {
if (l < h) {
long int pi = partition(a, l, h, num_cmp);
quickSort(a, l, pi - 1, num_cmp);
quickSort(a, pi + 1, h, num_cmp);
}
}