This repository was archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_sort.cpp
More file actions
152 lines (126 loc) · 2.46 KB
/
merge_sort.cpp
File metadata and controls
152 lines (126 loc) · 2.46 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
#include "stdafx.h"
using namespace System;
#include <iostream>
#include <stdio.h>
#include<conio.h>
#include<time.h>
using namespace std;
#define MAX_SIZE 100000
class mrgsort
{
public:
int NofCompM; //Number of Comparisons
int NofSwpM; // Number of Swaps
int uSize; //Size of Array
int* pArray1; // For MergeSort
mrgsort()
{
cout << "How many elements for array? (MAX: " << MAX_SIZE << "): ";
cin >> uSize;
if (uSize > MAX_SIZE)
uSize = MAX_SIZE;
pArray1 = new int[uSize];
cout << "Memory Allocated" << endl;
}
~mrgsort()
{
delete[] pArray1;
cout << "Memory freed" << endl;
}
/* Function: Create a random Array */
void makeArray();
/* Function: Display Array */
void dispArray(int *pArray1);
/*Function: Sort two different parts */
void merge(int *pArray1, int low, int mid, int high);
/* Merge and sort all the parts */
void mergesort(int *pArray1, int low, int high);
};
void mrgsort::makeArray()
{
cout << "Generate random integer numbers to sort\n";
for (int i = 0; i < uSize; i++)
{
pArray1[i] = rand() % (uSize + 1000);
}
}
void mrgsort::dispArray(int *pArray1)
{
cout << "Contents of Array:";
for (int i = 0; i < uSize; i++)
{
if ((i % 10) == 0)
cout << endl;
cout << pArray1[i] << " ";
}
cout << endl;
}
void mrgsort:: mergesort(int *pArray1, int low, int high)
{
int mid;
NofCompM = NofSwpM = 0;
if (low < high)
{
NofCompM++;
mid = (low + high) / 2;
mergesort(pArray1, low, mid);
mergesort(pArray1, mid + 1, high);
merge(pArray1, low, high, mid);
}
return;
}
void mrgsort::merge(int *pArray1, int low, int high, int mid)
{
int i, j, k;
i = low;
k = low;
j = mid + 1;
int *c = new int[uSize]; //Temporary Array
while (i <= mid && j <= high)
{
if (pArray1[i] < pArray1[j])
{
c[k] = pArray1[i];
k++;
i++;
}
else
{
c[k] = pArray1[j];
k++;
j++;
NofSwpM++;
}
NofCompM++;
}
while (i <= mid)
{
c[k] = pArray1[i];
k++;
i++;
}
while (j <= high)
{
c[k] = pArray1[j];
k++;
j++;
}
for (i = low; i < k; i++)
{
pArray1[i] = c[i];
}
}
int main()
{
mrgsort m;
m.makeArray();
m.dispArray(m.pArray1);
clock_t begin_time = clock();
m.mergesort(m.pArray1, 0, (m.uSize-1));
cout << "\nTime Taken for Sorting: " << float(clock() - begin_time) / CLOCKS_PER_SEC << " seconds" << endl;
cout << "\nAfter Sorting: " << endl;
m.dispArray(m.pArray1);
cout << "\nNo of Comparisons: " << m.NofCompM << endl;
cout << "\nNo of Swaps: " << m.NofSwpM << endl;
getch();
}