The project directions are as follows:
USE INPUT.TXT FILE (#fstream)
Implement the Merge Sort algorithm in C++.
Requirement
Input of the program: Unsorted list of integers separated by spaces; your program must read input file containing the numbers to be sorted.
Output of the program: Sorted list of inputted integers separated by spaces in ascending order.
Pivot: you need to infuse randomness for choosing pivot; you come up with an idea and explain it as comments in your program.
I am kinda confused about why the program below is not running properly. I am getting several errors on my end. Any advice or help will be greatly appreciated. Thank you in advance!
I am getting these errors:
Expression must have a constant value Line 54
Expression did not evaluate to a constant Line 54
See usage of 'size' Line 54
Failure was caused by a read of variable outside its lifetime Line 54
>> result of expression not used Line 60
<< result of expression not used Line 69
What I have tried:
1 #include <iostream>
2 #include<fstream>
3 using namespace std;
4
5 int partition(int arr[], int start, int end)
6 {
7
8 int pivot = arr[start];
9
10 int count = 0;
11 for (int i = start + 1; i <= end; i++) {
12 if (arr[i] <= pivot)
13 count++;
14 }
15
16 int pivotIndex = start + count;
18 swap(arr[pivotIndex], arr[start]);
19
20 int i = start, j = end;
22
23 while (i < pivotIndex && j > pivotIndex) {
24
25 while (arr[i] <= pivot) {
26 i++;
27 }
28
29 while (arr[j] > pivot) {
30 j--;
31 }
32
33 if (i < pivotIndex && j > pivotIndex) {
34 swap(arr[i++], arr[j--]);
35 }
36 }
37
38 return pivotIndex;
39 }
40
41 void quickSort(int arr[], int start, int end)
42 {
43
44 if (start >= end)
46 return;
47
48 int p = partition(arr, start, end);
50
51 quickSort(arr, start, p - 1);
53
54 quickSort(arr, p + 1, end);
56 }
57
58 int main()
59 {
60 ifstream fin;
61
62 fin.open("input.txt");
63
64 int size;
65 int s = 0;
66
67 fin>>size;
68 int arr[size];
70
71 for(int i=0;i<size;i++)
72 {
73 ="" fin="">>arr[i];
74 }
75
76 fin.close();
77
78 quickSort(arr, s, size - 1);
79
80 for (int i = 0; i < size; i++) {
81
82 cout << arr[i] << " ";
83
84 }
85
86
87 return 0;
88 }