Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7831935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:56:52+00:00 2026-06-02T11:56:52+00:00

FIXED – I included .cpp files too, cause of linker problems with template class

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T11:56:55+00:00Added an answer on June 2, 2026 at 11:56 am

    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:

    void myfunction(){
        static int myelements[40000];
        //...
        merge_sort(myelements, 0, sizeof(myelements)/sizeof(int));
        //....
    }
    

    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

    delete[] tab_pom;
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I fixed all the problems described here (and one additional), and posted the modified
Fixed: See notes at bottom I am implementing a generic class that supports two
We fixed an issue we had with a browser from not being able to
THIS IS FIXED, I had named my function the same name as my controller,
EDIT: Fixed, I had to remove all spaces I had (before the commas in
EDIT: Fixed most of the problem (but not too sure why). Check the bottom
I have a fixed width centered template and I wanted to apply 10px padding
I am displaying a fixed sequence in the tree list along with some values
I've just fixed a very subtle bug in our code, caused by slicing of
Edit: closing anchor fixed. This issue exists when testing on the following browsers: Google

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.