Merge Sort C++ Incorrectly Sorting - c++

I'm writing a program that will do 5 different sorting functions and compare times across them. I'm outputting the 1000th element of each array to see if it's sorted correctly, all of my other sorts except merge sort are producing the correct number. Merge sort is close but off, it is within an element or two of getting the correct answer (outputting 25011 rather than 25034 for the 1000th element.) Here is the code for my merge sort:
//Zach Heesaker. CS3. Project 4
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <stdio.h>
#include <ctime>
#include <cstdio>
#include <time.h>
#include <stdint.h>
#include<list>
#include<cmath>
using namespace std;
const int n = 10000;
int numswaps, numcompares;
void swap(int& a, int& b);
void print(int arr[], int n, ofstream& outf, double time);
void read(int arr[], int size);
void mergepass(int arr[], int y[], int& n, int& L);
void mergeSort(int arr[], int n);
void merge(int arr[], int y[], int L, int m, int n);
int main()
{
int numsorts = 5;
string whichsort;
ofstream outf("output.ot");
int arr[n + 1];
clock_t start, end;
double time;
outf << "Sort Name: " << setw(5) << " " << "1000th Element: " << setw(1) << " " << "Number of Moves: " << setw(1) << " " << "Time taken: " << endl;
read(arr, n);
numcompares = 0;
numswaps = 0;
start = clock();
mergeSort(arr, n);
end = clock();
time = double(end - start) / double(CLOCKS_PER_SEC);
}
void mergeSort(int arr[], int size)
{
int L = 1;
int y[n + 1];
while (L < n)
{
mergepass(arr, y, size, L);
L = 2 * L;
mergepass(y, arr, size, L);
L = 2 * L;
}
}
void merge(int arr[], int y[], int L, int m, int n)
{
int i, j, k, t;
i = L;
k = L;
j = m + 1;
while ((i <= m) && (j <= n))
{
numcompares++;
if (arr[i] <= arr[j])
{
numswaps++;
y[k++] = arr[i++];
}
else
{
numswaps++;
y[k++] = arr[j++];
}
}
if (i > m)
{
for (t = j; t <= n; t++)
{
numswaps++;
y[k + t - j] = arr[t];
}
}
else
{
for (t = i; t <= m; t++)
{
numswaps++;
y[k + t - i] = arr[t];
}
}
}
void mergepass(int arr[], int y[], int& n, int& L)
{
int i, t;
i = 1;
while (i <= n - 2 * L + 1)
{
merge(arr, y, i, i + L - 1, i + 2 * L - 1);
i = i + 2 * L;
}
if ((i + L - 1) < n)
{
merge(arr, y, i, i + L - 1, n);
}
else
{
for (t = i; t <= n; t++)
{
numswaps++;
y[t] = arr[t];
}
}
}
void swap(int& a, int& b)
{
int temp;
temp = a;
a = b;
b = temp;
numswaps += 3;
}
void print(int arr[], int n, ofstream& outf, double time)
{
outf << left << setw(6) << " " << left << arr[1000] << setw(12) << " " << left << numswaps << setw(10) << " " << left << "\t" << time << endl;
}
void read(int arr[], int size)
{
ifstream ifs("input.txt");
int i = 0;
while (!ifs.eof())
{
ifs >> arr[i];
i++;
}
}
int merge(int arr[], int left, int right)
{
int pivot = arr[right];
int k = (left - 1);
for (int j = left; j <= right - 1; j++)
{
numcompares++;
if (arr[j] < pivot)
{
k++;
swap(arr[k], arr[j]);
}
}
swap(arr[k + 1], arr[right]);
return (k + 1);
}
Any ideas as to what is going wrong in here? Thank you.

Related

C++ Divide and conquer algorithm problems

I have just learnt about Divide and Conquer algorithm and I'm a little bit confused about it. The question is my homework, I have tried many ways to fix my code but it did not run.
#include <iostream>
using namespace std;
void inputArray(int* a, int& n)
{
cout << "Input n:";
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "input a[" << i << "]= ";
cin >> a[i];
}
}
int sumeven(int* a, int l, int r)
{
if (l == r && a[l] % 2 == 0)
{
return a[l];
}
int mid = (l + r) / 2;
int s1 = sumeven(a, l, mid);
int s2 = sumeven(a, mid + 1, r);
return s1 + s2;
}
int main()
{
int n;
int a[20];
inputArray(a, n);
cout<<sumeven(a, 0,n-1);
return 0;
}
Try to test your programs without user input first:
#include <iostream>
using namespace std;
int sumeven(int* a, int l, int r)
{
if (r >= 6) return 0;
if (l > r ) return 0;
if (l >= r)
{
if (a[l] % 2 == 0)
{
return a[l];
}
else
{
return 0;
}
}
int mid = (l + r) / 2;
int s1 = sumeven(a, l, mid);
int s2 = sumeven(a, mid + 1, r);
return s1 + s2;
}
int main()
{
int n=6;
int a[6]={1,2,3,48,5,6};
cout<<sumeven(a,0,n-1);
return 0;
}

Quicksort does not work with large arrays

I'm trying to sort large arrays with Quicksort and Mergesort to evaluate performances.
I've a problem: if I impose a large number of elements in an array, the program does not start to generate values randomly. In the code below, if N=500000, it works very well. If N > 500000, for example 1000000, it does not work. With MergeSort the limit is 200000. I tried on multiple devices, C++ on Eclipse IDE.
Someone knows how to solve the problem?
#define N 800000
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <chrono>
using namespace std;
void Exchange(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int Partition(int A[], int p, int r) {
int x = A[r];
int i = p - 1;
for (int j = p; j <= r; j++) {
if (A[j] < x) {
i++;
Exchange(&A[i], &A[j]);
}
}
Exchange(&A[i + 1], &A[r]);
return i + 1;
}
int RPartition(int A[], int p, int r) {
srand(time(NULL));
int i = p + rand() % (p - r);
Exchange(&A[i], &A[r]);
return Partition(A, p, r);
}
void QuickSort(int A[], int p, int r) {
if (p < r) {
int q = RPartition(A, p, r);
QuickSort(A, p, q - 1);
QuickSort(A, q + 1, r);
}
}
void Stampa(int A[], int n) {
for (int i = 0; i < n; i++) {
cout << A[i] << "\n";
}
}
int main() {
srand(50000);
int A[N];
for (int i = 0; i < N; i++) {
A[i] = rand();
}
cout << "Array non ordinato\n";
Stampa(A, N);
auto start = std::chrono::system_clock::now();
QuickSort(A, 0, N - 1);
auto end = std::chrono::system_clock::now();
cout << "\nArray ordinato\n";
Stampa(A, N);
std::chrono::duration<double> elapsed = end - start;
cout << "Elapsed time: " << elapsed.count() << "s";
}
The explanation is very simple: you allocate the array as a local variable with automatic storage (aka on the stack), hence if the size is too large, you get a stack overflow.
You should either allocate the array from the heap or define it as static data.
Here is a modified version:
int main() {
srand(time(NULL));
int *A = new int[N];
for (int i = 0; i < N; i++) {
A[i] = rand();
}
cout << "Array non ordinato\n";
Stampa(A, N);
auto start = std::chrono::system_clock::now();
QuickSort(A, 0, N - 1);
auto end = std::chrono::system_clock::now();
cout << "\nArray ordinato\n";
Stampa(A, N);
std::chrono::duration<double> elapsed = end - start;
cout << "Elapsed time: " << elapsed.count() << "s";
delete[] A;
}

Calling function from one class in another in C++

So, I am writing a program in C++ that has a function in the Sort class that I wish to call in the Main class. The function has several data members used that are present in that class, that are not present in the Main class and I keep getting a C2660 error, that "Function does not take 0 arguments". Is there a way (short of writing a bunch of getters and setters) to resolve this?
#include "Sort.h"
#include "Timer.h"
using namespace std;
int main
{
Sort *sort = new Sort();
Timer ti;
sort->SetRandomSeed(12345);
sort->InitArray();
cout << "starting InsertionSort" << endl;
ti.Start();
sort->InsertionSort();
ti.End();
cout << "Insertion sort duration: " << ti.DurationInMilliSeconds() << "ms" << endl;
//sort->InitList();
//cout << "starting InsertionSortList()" << endl;
//ti.Start();
//sort->InsertionSortList();
//ti.End();
//cout << "Insertion sort list duration: " << ti.DurationInMilliSeconds() << "ms" << endl;
sort->InitArray();
cout << "starting SelectionSort" << endl;
ti.Start();
sort->SelectionSort();
ti.End();
cout << "SelectionSort duration: " << ti.DurationInMilliSeconds() << "ms" << endl;
sort->InitArray();
cout << "starting MergeSort" << endl;
ti.Start();
sort->MergeSort();
ti.End();
cout << "MergeSort duration: " << ti.DurationInMilliSeconds() << "ms" << endl;
sort->InitArray();
cout << "starting QuickSort" << endl;
ti.Start();
sort->QuickSort();
ti.End();
cout << "QuickSort duration: " << ti.DurationInMilliSeconds() << "ms" << endl;
sort->InitVector();
cout << "starting std::sort() of Vector<int>" << endl;
ti.Start();
sort->VectorSort();
ti.End();
cout << "std::sort() duration: " << ti.DurationInNanoSeconds() << "ns" << endl;
delete sort;
cout << endl <<"Press [Enter] key to exit";
getchar();
}
Sort.cpp
//const int for array
int num = 10000000;
int val = 10000;
//array
int *tmpArray, *qArr, *insArr, *selArr, *mergArr = NULL;
int low, high;
//duration for timer
int duration = 0;
Sort::Sort()
{
}
Sort::~Sort()
{
}
void Sort::InitArray()
{
//int for index
int i = 0;
tmpArray = new int[num];
qArr = new int[num];
insArr = new int[num];
selArr = new int[num];
mergArr = new int[num];
//fill temp array with sequential numbers
for (int i = 0; i < num; i++)
{
tmpArray[i] = 1 + rand() % val;
}
for (i = 0; i < num; i++)
{
qArr[i] = tmpArray[i];
insArr[i] = tmpArray[i];
selArr[i] = tmpArray[i];
mergArr[i] = tmpArray[i];
}
low = qArr[0];
high = qArr[num - 1];
int n = sizeof(tmpArray) / sizeof(tmpArray[0]);
}
void Sort::InitVector()
{
vector<int> v(num);
std::generate(v.begin(), v.end(), std::rand);
}
void Sort::InitList()
{
// A set to store values
std::list<int> l;
// Loop until we get 50 unique random values
while (l.size() < num)
{
l.push_back(1 + rand() % val);
}
for (int n : l) {
std::cout << n << '\n';
}
}
//setting seed
void Sort::SetRandomSeed(unsigned int seed)
{
seed = rand();
}
void Sort::InsertionSort()
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = insArr[i];
j = i - 1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && insArr[j] > key)
{
insArr[j + 1] = insArr[j];
j = j - 1;
}
insArr[j + 1] = key;
}
delete[] insArr;
insArr = NULL;
}
int Sort::partition(int qArr[], int low, int high)
{
int pivot = qArr[high]; // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high - 1; j++)
{
// If current element is smaller than or
// equal to pivot
if (qArr[j] <= pivot)
{
i++; // increment index of smaller element
swap(&qArr[i], &qArr[j]);
}
}
swap(&qArr[i + 1], &qArr[high]);
return (i + 1);
}
void Sort::QuickSort(int qArr[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(qArr, low, high);
// Separately sort elements before
// partition and after partition
QuickSort(qArr, low, pi - 1);
QuickSort(qArr, pi + 1, high);
}
delete[] qArr;
qArr = NULL;
}
void Sort::SelectionSort()
{
int i, j, min_idx;
// One by one move boundary of unsorted subarray
for (i = 0; i < n - 1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i + 1; j < n; j++)
if (selArr[j] < selArr[min_idx])
min_idx = j;
// Swap the found minimum element with the first element
swap(&selArr[min_idx], &selArr[i]);
}
delete[] selArr;
selArr = NULL;
}
void Sort::swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void Sort::VectorSort()
{
std::sort(v.begin(), v.end());
}
/* l is for left index and r is right index of the
sub-array of arr to be sorted */
void Sort::merge(int mergArr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int* L;
int* R;
/* create temp arrays */
L = new int[n1];
R = new int[n2];
/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++)
L[i] = mergArr[l + i];
for (j = 0; j < n2; j++)
R[j] = mergArr[m + 1 + j];
/* Merge the temp arrays back into arr[l..r]*/
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
mergArr[k] = L[i];
i++;
}
else
{
mergArr[k] = R[j];
j++;
}
k++;
}
/* Copy the remaining elements of L[], if there
are any */
while (i < n1)
{
mergArr[k] = L[i];
i++;
k++;
}
/* Copy the remaining elements of R[], if there
are any */
while (j < n2)
{
mergArr[k] = R[j];
j++;
k++;
}
}
void Sort::MergeSort(int mergArr[], int l, int r)
{
if (l < r)
{
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l + (r - l) / 2;
// Sort first and second halves
MergeSort(mergArr, l, m);
MergeSort(mergArr, m + 1, r);
merge(mergArr, l, m, r);
}
delete[] mergArr;
mergArr = NULL;
}
Sort.h
#include <iomanip>
#include <fstream>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include<iostream>
#include<cstdio>
#include<sstream>
#include<algorithm>
#include<list>
using namespace std;
#pragma once
class Sort
{
public:
Sort();
~Sort();
void InitArray();
void InitVector();
void InitList();
void SetRandomSeed(unsigned int seed);
int n, right, left, l, r, m;
vector<int> v;
void InsertionSort();
int partition(int qArr[], int low, int high);
void QuickSort(int qArr[], int low, int high);
void swap(int * xp, int * yp);
void VectorSort();
void MergeSort(int arr[], int l, int r);
void merge(int arr[], int l, int m, int r);
void SelectionSort();
};
Ignoring the Timer class (those are all good) here is the rest of the code. The C2660 errors are shown for the sort->MergeSort() and sort->QuickSort() calls in main.
I resolved the issues myself. I created helper functions in the Sort class that have no arguments and call the functions themselves to use in the main. Helper Functions shown below.
Sort.cpp
//method for Main to run to prevent C2660 errors
void Sort::mergeHelper()
{
MergeSort(mergArr, l, r);//call merge sort method
}
//method for Main to run to prevent C2660 errors
void Sort::quickHelper()
{
QuickSort(qArr, low, high);//call quick sort method
}
int Main
{
sort->quickHelper();
sort->mergeHelper();
}

Quicksort 3 way partition too slow

I am using quicksort 3 way partition, but it is turning out to be too slow as and when the vector size is greater than 10000.
What am I doing wrong? Please guide me! Any help will be appreciated
The answer should be computed in less than 2.2 sec.
#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>
using std::vector;
using std::swap;
void print(vector<int> v)
{
for(int i = 0; i < v.size(); i++) std::cout << v[i] << " ";
std::cout << std::endl;
}
void partition2(vector<int> &a, int l, int r, int &i, int &j) {
int k;
int middle=(l+r)/2;
/*Selecting pivot as median of low, high and middle*/
if(((a[l]<=a[middle]) && (a[middle]<=a[r])) || ((a[r]<=a[middle]) && (a[middle]<=a[l])))
k=middle;
else if(((a[middle]<=a[l]) && (a[l]<=a[r])) || ((a[r]<=a[l]) && (a[l]<=a[middle])))
k=l;
else if(((a[middle]<=a[r]) && (a[r]<=a[l])) || ((a[l]<=a[r]) && (a[r]<=a[middle])))
k=r;
swap(a[l], a[k]);
//print(a);
int low_value = a[l];
int index_low = l;
int index_high = l;
int counter=l;
for (int i = l + 1; i <= r; i++) {
if (a[i] < low_value) {
swap(a[i], a[index_low]);
counter++;
low_value=a[l];
}
else if(a[i]==low_value)
{
index_high++;
swap(a[i], a[index_high]);
}
//print(a);
}
i=counter;
j=index_high;
//swap(a[l], a[j]);
//return j;
}
void randomized_quick_sort(vector<int> &a, int l, int r) {
if (l >= r) {
return;
}
int i,j;
partition2(a, l, r, i, j);
randomized_quick_sort(a, l, i-1);
randomized_quick_sort(a, j+1, r);
}
int main() {
int n;
std::cin >> n;
//while(1){
//n=100+rand()%99999;
//std::cout<<n<<std::endl;
vector<int> a(n);
for (size_t i = 0; i < a.size(); ++i) {
std::cin >> a[i];
//a[i]=1+rand()%99999999;
}
randomized_quick_sort(a, 0, a.size() - 1);
for (size_t i = 0; i < a.size(); ++i) {
std::cout << a[i] << ' ';
}
//std::cout<<"Pass\n";
//}
return 0;
}
Everything at first glance is correct. However, there are probably just too many comparison operations. Try this option - it works on my computer for 1.6 seconds on average.
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <ctime>
#include <random>
#include <chrono>
#include <iomanip>
using namespace std;
using namespace std::chrono;
//======= quick_sort =======//
template<typename T>
int partition(vector<T>& numbers, const int& left, const int& right)
{
swap(numbers[left], numbers[left + (right - left) / 2]);
T mid = numbers[left];
int i(left + 1), j(right);
while (i <= j)
{
while ( i <= j && numbers[i] <= mid ) i++;
while ( i <= j && numbers[j] > mid ) j--;
if ( i < j ) swap(numbers[i], numbers[j]);
}
swap(numbers[i - 1], numbers[left]);
return i - 1;
}
template<typename T>
void quick_sort_rec(vector<T>& numbers, const int& left, const int& right)
{
if (left >= right) return;
int p = partition(numbers, left, right);
quick_sort_rec(numbers, left , p - 1);
quick_sort_rec(numbers, p + 1 , right);
}
//=========================//
template<typename T>
T random_T(long min, long max)
{
return (T)min + static_cast<T>(rand()) / (static_cast<T>(RAND_MAX / ((T)(max - min))));
}
template<typename T>
float time_func(void (*f)(vector<T>&, const int&, const int&), vector<T>& a)
{
high_resolution_clock::time_point t1 = high_resolution_clock::now();
f(a, 0, a.size() - 1);
high_resolution_clock::time_point t2 = high_resolution_clock::now();
return 1000.0 * (duration_cast<microseconds>(t2 - t1).count()) / (float)(CLOCKS_PER_SEC); /// CLOCKS_PER_SEC;
}
int main()
{
srand((unsigned)(777));
vector<int> a;
for (int i = 0; i < 10000; i++)
{
a.push_back(random_T<int>(0, 1000));
}
cout << setprecision(10) << "quick sort rec = " << time_func(quick_sort_rec, a) << endl;
return 0;
}
I run the following code to test partition2
int main(){
vector<int> a = {2, 1, 1, 9, 5, 3, 4, 2, 7};
int i, j;
partition2(a, 0, a.size() - 1, i, j);
for (auto i : a)
cout << i << ' ';
cout << '\n';
return 0;
}
And the results are
1 1 5 9 2 3 4 2 7
If the partition2 selecting median of low, high and middle as pivot, then the pivot should be 5 and the results should be something like
2 1 1 3 4 2 5 9 7
Then I check the code
if (a[i] < low_value) {
swap(a[i], a[index_low]);
counter++;
low_value=a[l];
}
else if(a[i]==low_value)
{
index_high++;
swap(a[i], a[index_high]);
}
It seems to be that the code try to find the minimum value of array and then move them to beginning of array. It seems that it is doing selection sort instead of quicksort. It explains why it is slow when input size is large.

Merge and QuickSort - stack overloaded

Helo everyone. :)
I have to write a program where I have 4 types of sorting: bubble-, insertion-, merge- i quick-sort.
Program has to sort points x,y in array of structures (random) by amount of total of their coordinates //e.g.: (1,3) is less than (0,6) because 1+3<0+6//. It saves sorted ones in this same array and also in txt file with time of sorting by each B,I,M,Q-sort.
Code from Merge and MergeSort:
void Merge(Structure *tab, int A, int sr, int Z)
{
Structure *tmp = new Structure[Z];
int i;
for (i = A; i <= Z; ++i)
{
tmp[i] = tab[i];
}
i = A;
int j = sr + 1;
int q = A;
while (i <= sr && j <= Z)
{
if (Compare(tab, i, j)) //'Compare' tells if total of coordinates from tab[i] is bigger than total from tab[j]
{
tab[q] = tmp[i];
++j;
}
else
{
tab[q] = tmp[j];
++i;
}
++q;
}
if(i <= sr)
{
while (i <= sr)
{
tab[q] = tmp[i];
++i;
++q;
}
}
else
{
while(j <= Z)
{
tab[q] = tmp[j];
++j;
++q;
}
}
delete[] tmp;
}
void MergeSort(Structure *tab, int A, int Z)
{
int sr = (A + Z)/2;
if(A < Z)
{
MergeSort(tab, A, sr);
MergeSort(tab, sr + 1, Z);
Merge(tab, A, sr, Z);
}
}
And to QuickSort:
int DivideQS(Structure *tab, int A, int Z)
{
Structure tmp;
Structure pivot = tab[A]; // first el. for pivot (A=0)
int i = A, j = Z; //indexes in array
while (true)
{
while (Compare(tab, j, A))
j--;
while (!Compare(tab, i, A)) // until elements are lower than pivot, that's this '!' for
i++;
if (i < j) // swap when i < j
{
tmp = tab[i];
tab[i] = tab[j];
tab[j] = tmp;
i++;
j--;
}
else
return j;
}
}
void QuickSort(Structure *tab, int A, int Z)
{
int dziel;
if (A < Z)
{
dziel = DivideQS(tab, A, Z);
QuickSort(tab, A, dziel);
QuickSort(tab, dziel+1, Z);
}
}
My problem is a stack. No matter how big I make this, it still goes overloaded. I can't manage this problem. I don't know if that's because of mistake in code or it's somewhere else. Bubble and Insertion work impeccable.
I was looking for solving on many sites, in my language and foreign ones (I'm Pole), and modificating code in so many ways but still have no clue what to do.
Help me, please.
===================================
How could I be so blind! Thank you #user3187084. :D
But I think I went from bad to worse. Now I've received new error message:
Windows has triggered a breakpoint in Projekt_AiSD.exe.
This may be due to a corruption of the heap, which indicates a bug in Projekt_AiSD.exe or any >of the DLLs it has loaded.
This may also be due to the user pressing F12 while Projekt_AiSD.exe has focus.
The output window may have more diagnostic information.
And after that this showed to me: http://i40.tinypic.com/314qyl5.png
And this is code for checking:
#include <ctime>
#include <cstdlib>
#include <cstdio>
#include <Windows.h>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
struct Structure
{
int x;
int y;
};
bool Compare(Structure *tab, int i, int j)
{
if ( (tab[i].x + tab[i].y) > (tab[j].x + tab[j].y))
return true;
return false;
}
void LosujWspolrzedne(Structure* tab, int K)
{
srand(time(NULL));
for (int i = 0; i < K; i++)
{
tab[i].x = rand()%21;
tab[i].y = rand()%21;
}
//return;
}
void Save (Structure *tab, int K, string NazwaPliku)
{
ofstream zap(NazwaPliku);
for (int i = 0; i < K; i++)
{
zap << tab[i].x << ' ' << tab[i].y << endl;
}
zap.close();
}
void Load (Structure *tab, int K, string NazwaPliku)
{
ifstream wcz(NazwaPliku);
if (!wcz)
{
cout << "Can't open the file!!!" << endl;
exit(1);
}
for (int i = 0; i < K; ++i)
{
wcz >> tab[i].x >> tab[i].y;
}
wcz.close();
}
void Time(long long a, long long b, string NazwaPliku)
{
ofstream czas(NazwaPliku);
if (!czas)
{
cout << "Can't open the file!!!" << endl;
}
czas << (b - a) << " ms" << endl;
czas.close();
}
void CopyArray(Structure *tab, Structure *tab1, int K)
{
for (int i = 0 ; i < K ; i++)
{
tab1[i].x = tab[i].x;
tab1[i].y = tab[i].y;
}
}
void Call_MS(Structure *tab, int A, int K)
{
Load(tab, K, "1k.txt");
long long startTime = GetTickCount64();
MergeSort(tab, A, K-1);
long long endTime = GetTickCount64();
cout << (endTime - startTime) << "ms dla MergeSort" << endl;
Save(tab, K, "WartLos_MS_1k.txt");
Time(startTime, endTime, "WartLos_MS_1k_czas.txt");
}
void Call_QS(Structure *tab, int A, int K)
{
Load(tab, K, "1k.txt");
long long startTime = GetTickCount64();
QuickSort(tab, A, K-1);
long long endTime = GetTickCount64();
cout << (endTime - startTime) << "ms dla QuickSort" << endl;
Save(tab, K, "WartLos_QS_1k.txt");
Time(startTime, endTime, "WartLos_QS_1k_czas.txt");
}
const int MAX_EL = 30;
int _tmain(int argc, _TCHAR* argv[])
{
Structure *punkt = new Structure[MAX_EL];
void LosujWspolrzedne(Structure *punkt, int MAX_EL);
Structure *punkt1= new Structure[MAX_EL];
void CopyArray(Structure *punkt, Structure *punkt1, int MAX_EL);
delete[] punkt;
Save(punkt1, MAX_EL, "1k.txt");
cout<<"Start:"<<endl;
Call_MS(punkt1, 0, MAX_EL);
Call_QS(punkt1, 0, MAX_EL);
delete[] punkt1;
return 0;
}
You have MergeSort(tab, A, Z) inside MergeSort, so you always call itself with exact arguments
It should be MergeSort(tab, A, st)
You are using MergeSort(tab,A,Z) which call the same function again with same arguments which is will go one for infinite recursion hence error. You should only pass MergeSort(tab,A,sr) which will reduce your problem to half size.