/
sorts.c
161 lines (139 loc) · 3.88 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
152
153
154
155
156
157
158
159
160
161
#include<stdio.h>
/*
qs:
compare i:1 ->N with pivot (5)
if arr[i] is less then pivot:
move content from i to holex
increment holex
copy contents from holex to i th location.
copy pivot to holex
call qs
return holex
*/
void qs( int array[], int left, int right ) {
int pivot; // pivot element.
int holex; // hole index.
int i;
holex = left + ( right - left ) / 2;
pivot = array[ holex ]; // get pivot from middle of array.
array[ holex ] = array[ left ]; // move "hole" to beginning of
holex = left; // range we are sorting.
for ( i = left + 1; i <= right; i++ ) {
if ( array[ i ] <= pivot ) {
/* we put the element in the hole,
* then we re-establish the hole immediately
* to the right of the moved element. */
array[ holex ] = array[ i ];
array[ i ] = array[ ++holex ];
}
}
/* At this point, all elements greater than the pivot are to
* the right of the hole; all elements other than the pivot
* itself which are less than or equal to the pivot are to
* the left of the hole. We sort the sub-arrays as
* necessary.
*/
if ( holex - left > 1 ) {
qs( array, left, holex - 1 );
}
if ( right - holex > 1 ) {
qs( array, holex + 1, right );
}
array[ holex ] = pivot;
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int partition (int arr[], int l, int h)
{
int pivot = arr[h];
int i = (l - 1), j;
for (j = l; j <= h- 1; j++) {
if (arr[j] <= pivot) {
i++;
//printf("Before swap %d %d\n", arr[i], arr[j]);
swap (&arr[i], &arr[j]);
//printf("After swap %d %d\n", arr[i], arr[j]);
}
//printf("Before swap %d %d\n", arr[i+1], arr[h]);
swap (&arr[i + 1], &arr[h]);
//printf("After swap %d %d\n", arr[i+1], arr[h]);
}
// Revisit adding following to remove warning
return (i + 1);
}
void quickSort(int A[], int l, int h)
{
int sorted = 0;
/*
printf("Quick sort\n");
for (sorted = 0; sorted < h+1; sorted++) {
printf("%d ", A[sorted]);
}
printf("\n");
*/
if (l < h) {
int p = partition(A, l, h);
quickSort(A, l, p - 1);
quickSort(A, p + 1, h);
}
}
void insertion_sort (int *i, int length)
{
int unsorted = 1, sorted;
int key = 0;
int changed = 0;
for (unsorted = 1; unsorted < length; unsorted++) {
key = i[unsorted];
sorted = unsorted - 1;
while((sorted >= 0) && (i[sorted] > key)) {
i[sorted + 1] = i[sorted];
//changed = 1;
sorted--;
}
//if (changed) {
i[sorted+1] = key;
//changed = 0;
//}
}
for (sorted = 0; sorted < length; sorted++) {
printf("%d ", i[sorted]);
}
printf("\n");
}
int main()
{
int unsorted = 1, length, sorted;
int i[] = {4, 6, 7, 9, 1, 2, 10};
int key = 0;
int changed = 0;
printf("size of arrray is %ld\n", sizeof(i)/sizeof(int));
length = sizeof(i)/sizeof(int);
for (sorted = 0; sorted < length; sorted++) {
printf("%d ", i[sorted]);
}
printf("\n");
#ifdef IS
insertion_sort(i, length);
printf("\nIS\n");
for (sorted = 0; sorted < length; sorted++) {
printf("%d ", i[sorted]);
}
printf("\n");
#endif
quickSort(i, 1, length);
for (sorted = 0; sorted < length; sorted++) {
printf("%d ", i[sorted]);
}
printf("\n");
qs(i, 0, length-1);
for (sorted = 0; sorted < length; sorted++) {
printf("%d ", i[sorted]);
}
printf("\n");
return 1;
}