So, I am trying to create a program to read a file, merge sort, output to a file and time the sorting. I have the code written for loading a file, sorting algorithm (borrowed from online and altered), creating a file all good to go. Unfortunately, what is sorted is not output to the new file. I'm new to C++ and all of this has been difficult, but I'm slowly learning. I know I need to take the sorted elements, increment them, and output to the new file, but can't seem to get it right tonight. I've basically confused myself.
I have several files, problem1.cpp, merge_sort.cpp, and utilities.cpp. (header files and Makefile not included here)
Any help would be appreciated, thanks so much!
problem1.cpp:
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <chrono>
#include "merge_sort.hpp"
#include "utilities.hpp"
using namespace std;
int main(){
cout << "\n========================================";
cout << "\n\n Merge Sort Number Sorting Program...\n\n";
cout << "=======================================\n\n";
vector <double> input_data = getFile();
int low = 0, high = (int)input_data.size()-1;
cout <<"Sorting...\n";
auto start = chrono::steady_clock::now();
merge_sort(input_data, low, high);
auto end = chrono::steady_clock::now();
auto diff = end - start;
cout << "Sorting completed in " << chrono::duration <double, milli> (diff).count() << endl;
cout << "=======================================\n\n";
cout << "Creating output file, named sorted_numbers.dat\n\n";
createFile();
/*this is where I run into trouble. Can't seem to figure this out.
How to get the sorted data into the new file.
I did this in C, but can't seem to translate it to C++. */
vector<double> file_output;
for (i = 0; i < (int)sorted.size(); i++) {
new_file <<
}
=======================================
utilities.cpp: files for loading file and creating new file:
Code:
#include <iostream>
#include "utilities.hpp"
#include <vector>
#include <fstream>
//Get the file
vector <double> getFile() {
fstream loaded_file;
vector <double> input_from_file;
loaded_file.open("unsorted_numbers_short.dat", ios::in);
if (!loaded_file.is_open()) {
cout << "File not found!";
}
else {
double temp;
while (1){
loaded_file >> temp;
input_from_file.push_back(temp);
if(loaded_file.eof())
break;
}
loaded_file.close();
}
return input_from_file;
}
void createFile(){
fstream new_file;
new_file.open("sorted_numbers.dat", ios::out);
new_file << "TESTING \n\n";//just testing to see if it works. It does.
if (!new_file) {
cout << "File not created!";
}
else {
cout << "File created successfully! \n\n";
new_file.close();
}
}
And the final bit of code, the Merge Sort:
Code:
#include <iostream>
#include <vector>
using namespace std;
void merge(vector<double>, int, int , int );
void merge_sort(vector<double> arr, int low, int high)
//Helper sort for finding middle (from https://www.softwaretestinghelp.com/merge-sort/)
{
int mid;
if (low < high){
//divide the array at mid and sort independently using merge sort
mid=(low+high)/2;
merge_sort(arr,low,mid);
merge_sort(arr,mid+1,high);
//merge or conquer sorted arrays
merge(arr,low,high,mid);
}
}
// Merge sort
void merge(vector<double> arr, int low, int high, int mid)
{
int i, j;
vector<double> sorted;
i = low;
j = mid + 1;
while (i <= mid && j <= high) {
if (arr[i] < arr[j]) {
sorted.push_back(arr[i]);
i++;
}
else {
sorted.push_back(arr[j]);
j++;
}
}
while (i <= mid) {
sorted.push_back(arr[i]);
i++;
}
while (j <= high) {
sorted.push_back(arr[j]);
j++;
}
for (i = 0; i < (int)sorted.size(); i++) {
arr[i + low] = sorted[i];
}
}