-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFuctionDefintions.cpp
More file actions
175 lines (151 loc) · 3.54 KB
/
FuctionDefintions.cpp
File metadata and controls
175 lines (151 loc) · 3.54 KB
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
#include<iostream>
#include<ctime>
#include"Header.h"
using namespace std;
//INSERTION SORT
void Sorting::insertionSort(int* arr, int num)
{
//Class method to implement the insertion sort
for (int i = 0; i < num; i++)
{
int a = arr[i];
int j = i - 1; /* Initialising the variable to be compared to the
current element, as the element to its immediate left */
while (j >= 0 && arr[j] > a)
{
arr[j + 1] = arr[j]; /* Moving the greater elements to the left until
a number less than 'a' is encountered */
j--;
}
arr[j + 1] = a;
}
}
//BUBBLE SORT
void Sorting::bubbleSort(int* arr, int num)
{
//Class method to implement the bubble sort algorithm
int temp = 0; //Varaible to temporarily store varibals while creating 'bubbles'
for (int i = 0; i < num; i++)
{
for (int j = i + 1; j < num; j++)
{
if (arr[j] < arr[i]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
//HEAP SORT
void Sorting::heapSortHeapify(int* arr, int num, int i)
{
//Class method to break the existing array into heaps upto individual elements
int parent = i;
int leftChild = (i * 2) + 1;
int rightChild = (i * 2) + 2;
if (leftChild < num && arr[leftChild] > arr[parent]) {
parent = leftChild;
}
if (rightChild < num && arr[rightChild] > arr[parent]) {
parent = rightChild;
}
if (parent != i) {
swap(arr[i], arr[parent]);
heapSortHeapify(arr, num, parent);
}
}
void Sorting::heapSort(int* arr, int num)
{
//Class method to group the heapified individual elements into a final array, to finalise the heap sort
for (int i = (num / 2 - 1); i >= 0; i--)
{
heapSortHeapify(arr, num, i);
}
for (int i = (num - 1); i > 0; i--)
{
swap(arr[0], arr[i]);
heapSortHeapify(arr, i, 0);
}
}
//QUICK SORT
int Sorting::seperate(int* arr, int l, int h)
{
int pivot = arr[h];
int i = (l - 1);
for (int j = l; j < h; j++)
{
if (arr[j] <= pivot) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[h]);
return (i + 1);
}
void Sorting::quickSort(int* arr, int l, int h)
{
if (l < h)
{
int q = seperate(arr, l, h);
quickSort(arr, l, q - 1);
quickSort(arr, q + 1, h);
}
}
//MERGE SORT
void Sorting::mergeSort(int* arr, int lowerBound, int upperBound)
{
//Class method to execute the merge sort algorithm
if (lowerBound <upperBound)
{
int midPoint = (lowerBound + upperBound) / 2;
mergeSort(arr, lowerBound, midPoint);
mergeSort(arr, midPoint+1, upperBound);
merge(arr, lowerBound, midPoint, upperBound);
}
}
void Sorting::merge(int* arr, int lowerBound, int midPoint, int upperBound)
{
int i = lowerBound; //Starting index for left subarray
int j = midPoint + 1; //starting index for right subarray
int k = lowerBound; //Starting index for temporary array
int* b = new int[upperBound + 1]; //Temporary array
while (i <= midPoint && j <= upperBound)
{
if (arr[i] < arr[j]) {
b[k] = arr[i]; //Where arr[i] is smaller than arr[j]
i++;
}
else
{
b[k] = arr[j]; //Where arr[j] is smaller than arr[i]
j++;
}
k++;
}
while (i <= midPoint)
{
b[k] = arr[i]; //Copying all elements from the left subarray to the temporary array
i++;
k++;
}
while (j <= upperBound)
{
b[k] = arr[j]; //Copying all elements from the right subarray to the temporary array
j++;
k++;
}
for (k = lowerBound; k <= upperBound; k++) {
arr[k] = b[k];
}
}
//DISPLAYING THE SORTED ARRAY
void Sorting::displaySortedArray(int* arr, int num)
{
cout << "\nThe sorted aray is:\n";
for (int i = 0; i < num; i++)
{
cout << arr[i] << " ";
}
cout << "\n";
}