-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeSort.cpp
More file actions
80 lines (68 loc) · 1.65 KB
/
mergeSort.cpp
File metadata and controls
80 lines (68 loc) · 1.65 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
/*********************
Name: Sai Sivakumar
Merge Sort With Arrays C++
Purpose: 1-3 sentences about your program.
**********************/
#include "mergeSort.h"
//Public Methods
MergeSort::MergeSort()
{
arraySize = 0;
}
MergeSort::~MergeSort() {}
bool MergeSort::sorter(int size, int fixItUp[])
{
arraySize = size;
locOrigArr = fixItUp;
mergeSort(0, arraySize - 1);
return true;
}
void MergeSort::displaySortedArr()
{
for(int i = 0; i < arraySize; i++)
{
cout<<locOrigArr[i]<<endl;
}
}
//Private Methods
void MergeSort::mergeSort(int first, int last)
{
if (first < last) {
int middle = (first + last)/2;
mergeSort(first, middle);
mergeSort(middle+1, last);
merge(first, middle, last);
}
}
void MergeSort::merge(int first, int middle, int last)
{
int temp[arraySize];
int begin1, tempIndex, begin2;
begin1 = tempIndex = first;
begin2 = middle + 1;
while(begin1 <= middle && begin2 <= last) {
if(locOrigArr[begin1] <= locOrigArr[begin2]) {
temp[tempIndex] = locOrigArr[begin1];
begin1++;
} else {
temp[tempIndex] = locOrigArr[begin2];
begin2++;
}
tempIndex++;
}
finishFillingArray(&begin1, &middle, &tempIndex, temp);
finishFillingArray(&begin2, &last, &tempIndex, temp);
for (int i = first; i <= last; i ++)
{
locOrigArr[i] = temp[i];
}
}
void MergeSort::finishFillingArray(int *first, int *last, int *tempIndex, int temp[])
{
while(*first <= *last)
{
temp[*tempIndex] = locOrigArr[*first];
(*first)++;
(*tempIndex)++;
}
}