forked from musa-1410/Stumps_Cricket_Simulation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorting.cpp
More file actions
84 lines (70 loc) · 2.08 KB
/
Sorting.cpp
File metadata and controls
84 lines (70 loc) · 2.08 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
#include <iostream>
using namespace std;
void countingSort(int arr[], string names[], int size) {
int maxVal = *max_element(arr, arr + size);
int* count = new int[maxVal + 1]();
int* output = new int[size];
string* outputNames = new string[size];
for (int i = 0; i < size; i++)
count[arr[i]]++;
for (int i = maxVal - 1; i >= 0; i--)
count[i] += count[i + 1]; // Descending order
for (int i = size - 1; i >= 0; i--) {
output[count[arr[i]] - 1] = arr[i];
outputNames[count[arr[i]] - 1] = names[i];
count[arr[i]]--;
}
for (int i = 0; i < size; i++) {
arr[i] = output[i];
names[i] = outputNames[i];
}
delete[] count;
delete[] output;
delete[] outputNames;
}
void merge(int arr[], string names[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int* L = new int[n1];
int* R = new int[n2];
string* Lnames = new string[n1];
string* Rnames = new string[n2];
for (int i = 0; i < n1; i++) {
L[i] = arr[left + i];
Lnames[i] = names[left + i];
}
for (int i = 0; i < n2; i++) {
R[i] = arr[mid + 1 + i];
Rnames[i] = names[mid + 1 + i];
}
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] >= R[j]) { // Descending order
arr[k] = L[i];
names[k++] = Lnames[i++];
} else {
arr[k] = R[j];
names[k++] = Rnames[j++];
}
}
while (i < n1) {
arr[k] = L[i];
names[k++] = Lnames[i++];
}
while (j < n2) {
arr[k] = R[j];
names[k++] = Rnames[j++];
}
delete[] L;
delete[] R;
delete[] Lnames;
delete[] Rnames;
}
void mergeSort(int arr[], string names[], int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, names, left, mid);
mergeSort(arr, names, mid + 1, right);
merge(arr, names, left, mid, right);
}
}