Write a C++ program to implement Mergesort Algorithm discussed in class. Use the data file named random.data
1. Program description, Comments, Indentation and style etc.
2. Read data file, Load the array and print the array formatted 10 numbers in each line.
3. Implementation of Algorithm and correctness .
3. Print the sorted array formatted 10 numbers on each line.
random.data file
43.72
60.30
60.21
30.41
75.77
38.38
92.03
27.37
0.52
3.43
80.52
40.45
3.84
68.44
66.70
54.14
52.88
32.04
18.76
43.24
45.20
1.94
55.87
15.10
38.54
83.78
84.35
78.57
61.12
81.84
61.57
2.36
85.45
35.63
21.40
52.95
49.36
3.38
32.44
10.19
37.14
58.51
35.72
73.76
63.77
13.38
80.96
84.21
46.78
91.80
42.85
74.40
4.47
59.75
79.33
55.32
60.17
62.26
20.36
88.49
3.55
48.96
42.15
47.22
72.09
11.01
48.96
75.37
31.27
34.64
50.85
59.08
90.55
41.42
84.89
18.48
74.68
79.72
67.88
96.45
84.52
86.01
71.62
0.99
71.28
20.95
21.45
33.94
58.67
26.11
46.99
19.00
66.01
33.03
3.39
67.82
8.97
80.02
57.18
49.01
31.83
91.36
60.40
96.55
7.52
9.40
87.47
58.43
69.35
9.15
41.58
10.16
46.94
36.70
65.80
66.18
30.43
9.74
10.58
54.69
54.83
86.92
71.15
72.83
96.25
65.27
15.86
12.08
62.84
29.46
65.23
59.04
19.98
10.55
81.63
please answer. Thank you.
If you have any doubts, please give me comment...
#include <iostream>
#include <fstream>
using namespace std;
#define MAX_SIZE 150
void printArray(double arr[], int n);
void mergeSort(double arr[], int l, int r);
void merge(double arr[], int l, int m, int r);
int main()
{
ifstream inFile;
inFile.open("random.data");
if (inFile.fail())
{
cout << "Unable to open file" << endl;
return 0;
}
double arr[MAX_SIZE];
int n = 0;
while (inFile >> arr[n])
{
n++;
}
inFile.close();
cout << "Unsorted array: " << endl;
printArray(arr, n);
mergeSort(arr, 0, n - 1);
cout << "\nSorted array: " << endl;
printArray(arr, n);
return 0;
}
void printArray(double arr[], int n)
{
for (int i = 0; i < n; i++)
{
cout << arr[i];
if ((i + 1) % 10 == 0)
cout << endl;
else
cout << " ";
}
cout << endl;
}
void mergeSort(double arr[], int l, int r)
{
if (l < r)
{
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
void merge(double arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
double L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
Write a C++ program to implement Mergesort Algorithm discussed in class. Use the data file named...