FIXED – I included .cpp files too, cause of linker problems with template class – I had a memory leak in mergesort.cpp, which caused quicksort (included after) to not work too. Introsort was included before, so worked fine.
my qsort algorithm:
#include "quicksort.h"
template <class typ>
void quicksort(typ* tab, long int begin, long int end) {
long int i = begin;
long int j = end;
typ tmp;
typ pivot = tab[(begin + end) / 2];
/* podziel */
while (i <= j) {
while (tab[i] < pivot)
i++;
while (tab[j] > pivot)
j--;
if (i <= j) {
tmp = tab[i];
tab[i] = tab[j];
tab[j] = tmp;
i++;
j--;
}
};
/* rekurencja */
if (begin < j)
quicksort(tab, begin, j);
if (i < end)
quicksort(tab, i, end);
}
I’ve build this in 2 different computers with Visual Studio 11 (win 64) and with dev c++. Same happens to my mergesort. 32002 elements and no more.
mergesort:
#include "mergesort.h"
using namespace std;
template <class typ>
void merge(typ *tab, long int begin, long int mid, long int end){
typ *tab_pom = new typ[end+1];
long int i,j,k;
i=begin; //pierwsza czesc
j=mid+1; //druga czesc
k=0;
while ( (i<=mid) && (j<=end) ){ //pierwsze mniej niz srodek drugie mniej niz koniec
if(tab[i] < tab[j]){
tab_pom[k]=tab[i];
i++;
k++;
}else{
tab_pom[k]=tab[j];
j++;
k++;
}
}
if (!(i<=mid)){
while(j<=end){
tab_pom[k]=tab[j];
k++;
j++;
}
}else{
if (!(j<=end)){
while(i<=mid){
tab_pom[k]=tab[i];
k++;
i++;
}
}
}
k=0;
for (i=begin;i<=end;i++){
tab[i]=tab_pom[k];
k++;
}
}
// dzieli dane az do otzrymania posortowanych tablic jedno elementowych
template <class typ>
void merge_sort(typ *tab, long int begin, long int end){
long int mid;
if (begin < end){
mid=(begin+end)/2;
merge_sort(tab,begin,mid);
merge_sort(tab,mid+1,end);
merge(tab,begin,mid,end);
}
}
I’ve wrote also introsort, which works perfect for any amount of elements. I tried increasing stack size in Visual Studio 11 -> project -> properties -> linker -> stack reserve size -> 80000000. Nothing changes. Any help appreciated.
source:
#include <iostream>
#include <time.h>
#include "introspectiv.h"
#include "introspectiv.cpp"
#include "mergesort.h"
#include "mergesort.cpp"
#include "quicksort.h"
#include "quicksort.cpp"
using namespace std;
//funkcja do kopiowania tablicy
template<class typ>
void kopiuj(typ* tabl_1, typ* tabl_2, int indeks){
for (int i = 0 ; i < indeks ; i++) tabl_2[i] = tabl_1[i];
}
int main() {
//tworzy tablice
cout << "podaj rozmiary tablicy wypelnionej losowymi liczbami: ";
int wybor;
clock_t czas[7];
clock_t start, stop;
long int n, i, end, begin;
cin >> n;
int *tablica = new int[n];
int *tab_pom = new int[n];
for (i=0; i<n; i++){
tablica[i] = rand() % 100;
}
end = n-1;
begin = 0;
float procent[] = {0, 0.25, 0.5, 0.75, 0.95, 0.99, 0.997};
cout << endl << "wybierz algorytm sortowania: " << endl;
cout << "quicksort - 1 " << endl;
cout << "mergesort - 2 " << endl;
cout << "introspektywne - 3 " << endl;
cin >> wybor;
switch (wybor)
{
case 1: {
for (i=0; i<7; i++){
kopiuj(tablica, tab_pom, end);
end = end*procent[i];
quicksort(tab_pom, begin , end);
end = n-1;
start=clock();
quicksort(tab_pom, begin , end);
stop=clock();
czas[i] = stop-start;
}
}
case 2: {
start=clock();
merge_sort(tablica, begin , end);
stop=clock();
}
case 3: {
start=clock();
introspective(tablica, n);
stop=clock();
}
default: break;
}
//for (i=0; i<n; i++){
// cout<<tablica[i]<< " ";
//}
cout << endl << "Czas wybranego sortowania: " << endl;
for (i=0; i<7; i++){
cout << "Dla " << procent[i]*100 << "% posortowanych danych: " << czas[i] << " ms" << endl << endl;
}
system("pause");
}
its not finished for counting times in intro and mergesort.
Quick sort: With 32000 elements, quick sort only goes about 15 deep in recursion, you shouldn’t worry about the stack size. Problem might be though that the object you try to pass is a local variable in a function, thus has a limited size. Try allocating it as a global or a static variable, eg:
Merge sort: I see you call new a tons of times, but no delete. I don’t see why you need to allocate memory anyways, you could just use pointers instead. Try adding
At the end of merge;
Next time try to give a little more diagnostics help, than it doesn’t work. Error message perhaps.
Cheers, hope it was helpful.