-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.h
129 lines (110 loc) · 2.69 KB
/
sort.h
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
#ifndef SORT_H
#define SORT_H 1
/*
This is a library of sorting functions in C
Author: Roger Barker
Last Edit: 20150831
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
//------------------------------------------------------------
/*
Return size of array
*/
#define ARRLEN(x) (sizeof(x) / sizeof((x)[0]))
//------------------------------------------------------------
typedef enum{
SELECTION,
INSERTION,
MERGE,
QUICK,
BUBBLE,
HEAP
}sortMode;
typedef enum{
FALSE,
TRUE
}BOOL;
//------------------------------------------------------------
void printArray( int* array, int size, int mod );
void randomizeArray( int* array, int size, int mod );
//------------------------------------------------------------
/*
Selection Sort:
For i = 1 to n do
min <- i
For j = i +1 to n do
if A[j] < A[min] then
min <- j
temp <- A[i]
A[i] <- A[min]
A[min] <- temp
*/
void selectionSort( int* arrayToSort, int size );
/*
Insertion Sort:
For j = 2 to n do
key <- A[j]
i <- j - 1
while i > 0 and key < A[i]
A[i+1] <- A[i]
i <- i - 1
A[i+1] <- key
*/
void insertionSort( int* arrayToSort, int size );
/*
mergeSort(A,p,r):
if(p < r)
then q <- floor((p+r)/2)
mergeSort(A,p,q)
mergeSort(A,q+1,r)
merge(A,p,q,r)
*/
void mergeSort( int* arrayToSort, int lhv, int rhv );
/*
Quick(A,p,r):
if p < r
q = partition(A,p,r)
quicksort(A,p,q-1)
quicksort(A,q+1,r)
*/
void quickSort( int* arrayToSort,int lhv, int rhv);
/*
bubbleSort( A, n ):
repeat
swapped = false
for i = 1 to n-1 inclusive do
// if this pair is out of order
if A[i-1] > A[i] then
// swap them and remember something changed
swap( A[i-1], A[i] )
swapped = true
end if
end for
until not swapped
end procedure
*/
void bubbleSort( int* arrayToSort, int size );
/*
procedure heapsort(a, count) is
input: an unordered array a of length count
(Build the heap in array a so that largest value is at the root)
heapify(a, count)
(The following loop maintains the invariants that a[0:end] is a heap and every element
beyond end is greater than everything before it (so a[end:count] is in sorted order))
end ← count - 1
while end > 0 do
(a[0] is the root and largest value. The swap moves it in front of the sorted elements.)
swap(a[end], a[0])
(the heap size is reduced by one)
end ← end - 1
(the swap ruined the heap property, so restore it)
siftDown(a, 0, end)
*/
void heapSort( int* arrayToSort, int size );
#endif
/*******************************************************************\
EOF
\*******************************************************************/