-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorting.c
176 lines (147 loc) · 5.11 KB
/
sorting.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
// sorting.c
//
//
// Created by Bayram Muradov on 11/02/2017.
// Copyright © 2017 Bayram Muradov. All rights reserved.
//
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
//Selection Sort and its helper functions:
void selectionSort(int *arr, int size, int &compCount, int &moveCount) {
for (int last = size-1; last >= 1; --last) {
int smallest = indexOfSmallest(arr, last+1, compCount);
swap(arr[smallest], arr[last], moveCount);
}
}
void swap(int &x, int &y, int &z) {
int temp = x;
x = y;
y = temp;
z=z+3;
}
int indexOfSmallest(const int theArray[], int size, int &compCount) {
int indexSoFar = 0;
for (int currentIndex=1; currentIndex<size; ++currentIndex)
{
compCount++; // making comparison each time the for loop iterates
if (theArray[currentIndex] < theArray[indexSoFar]) {
indexSoFar = currentIndex;
}
}
return indexSoFar;
}
//Merge Sort Algorithm and its helper functions
void mergeSort(int *arr, int size, int &compCount, int &moveCount) {
int first = 0;
int last = size-1;
mergesortRec(arr, first, last, compCount, moveCount);
}
void mergesortRec (int *arr, int first, int last, int &compCount, int &moveCount) {
compCount++;
if (first < last) {
int mid = (first + last)/2; // index of midpoint
//cout<<"-----"<<endl;
mergesortRec(arr, first, mid, compCount, moveCount);
//cout<<"******"<<endl;
mergesortRec(arr, mid+1, last, compCount, moveCount);
// merge the two halves
merge(arr, first, mid, last, compCount, moveCount);
//cout<<"-----"<<endl;
}
} // end mergesort
void merge( int theArray[], int first, int mid, int last, int &cCount, int &mCount) {
int tempArray[MAX_SIZE]; // temporary array
int first1 = first; // beginning of first subarray
int last1 = mid; // end of first subarray
int first2 = mid + 1; // beginning of second subarray
int last2 = last; // end of second subarray
int index = first1; // next available location in tempArray
while((first1<=last1)&&(first2<=last2)) {
cCount++;
if(theArray[first1]>=theArray[first2]) {
tempArray[index] = theArray[first1];
mCount++;
first1++;
}
else
{
mCount++;
tempArray[index]=theArray[first2];
first2++;
}
index++;
}
while(first1<=last1)
{
cCount++;
tempArray[index]=theArray[first1];
mCount++;
first1++;
index++;
}
while(first2<=last2)
{
cCount++;
tempArray[index]=theArray[first2];
mCount++;
first2++;
index++;
}
for(index=first; index<=last; index++) {
cCount++;
theArray[index]=tempArray[index];
mCount++;
}
} // end merge
//Quick Sort algorithm and its helper functions
void quickSort(int *arr, int size, int &compCount, int &moveCount) {
int first=0;
int last=size-1;
quicksortRec(arr, first, last, compCount, moveCount);
}
void quicksortRec(int theArray[], int first, int last, int &compCount, int &mCount) {
// Precondition: theArray[first..last] is an array.
// Postcondition: theArray[first..last] is sorted.
int pivotIndex= theArray[0];
compCount++;
if (first < last) {
// create the partition: S1, pivot, S2
partition(theArray, first, last, pivotIndex, compCount, mCount);
// sort regions S1 and S2
quicksortRec(theArray, first, pivotIndex-1, compCount, mCount);
quicksortRec(theArray, pivotIndex+1, last, compCount, mCount);
}
}
void partition(int theArray[], int first, int last, int &pivotIndex, int &c, int &m) {
// Precondition: theArray[first..last] is an array; first <= last.
// Postcondition: Partitions theArray[first..last] such that:
// S1 = theArray[first..pivotIndex-1] < pivot
// theArray[pivotIndex] == pivot
// S2 = theArray[pivotIndex+1..last] >= pivot
//we will choose pivot as the first element of theArray
int pivot = theArray[first];
// initially, everything but pivot is in unknown
int lastS1 = first; // index of last item in S1
int firstUnknown = first + 1; // index of first item in unknown
// move one item at a time until unknown region is empty
for ( ; firstUnknown <= last; ++firstUnknown) {
// Invariant: theArray[first+1..lastS1] < pivot
// theArray[lastS1+1..firstUnknown-1] >= pivot
// move item from unknown to proper region
c+=2; //makes 2 comparisons @ each time the loop itaretes
if (theArray[firstUnknown] > pivot) { // belongs to S1
++lastS1;
swap(theArray[firstUnknown], theArray[lastS1], m);
} // else belongs to S2
}
// place pivot in proper position and mark its location
swap(theArray[first], theArray[lastS1], m);
pivotIndex = lastS1;
}