I'm trying to time the quicksort and selection sort methods on my machine and my arrays are too big to fit on the stack. I've been trying to research ways to solve this but am struggling to implement what I've read. Here's some code:
#ifndef PROGRAM1_H_
#define PROGRAM1_H_
#include "stdafx.h"
#include "timer.h"
#include "quicksort.h"
#include <stdlib.h>
#include <vector>
/*
* Description: This class times the selection sort method for different
* array lengths.
*/
int main(){
//creates three arrays with lengths 10000,100000,
//and 1000000 respectively and initalizes them.
int array1_size = 10000;
int array2_size = 100000;
int array3_size = 1000000;
int array4_size = 10000;
int array5_size = 100000;
int array6_size = 1000000;
int *array1 = newint [array1_size];
int *array2 = newint [array2_size];
int *array3 = newint [array3_size];
int *array4 = newint [array4_size];
int *array5 = newint [array5_size];
int *array6 = newint [array6_size];
//std::vector<std::vector<int> > array1[array1_size]; (this is me fooling around)
//std::vector<std::vector<int> > array2;
//std::vector<std::vector<int> > array3;
//std::vector<std::vector<int> > array4;
//std::vector<std::vector<int> > array5;
//std::vector<std::vector<int> > array6;
Timer t;
for (int x=0; x<array1_size; x++){ //loop 10000 times.
array1[x] = ((rand()*rand() % 1000000)+1); //sets slot x of array1 to a random number.
t.Start(); //starts the timer.
quickSort(array1,0,array1_size-1); //calls quicksort supplying the array and range to sort.
t.Stop(); //stops the timer
}
cout<< "(quicksort) size: "<< array1_size <<" time: "<< t.Seconds()<<" seconds"<<endl ; //prints result
t.Reset(); //resets the timer back to 0
delete [] array1; //clean up
for (int x=0; x<array2_size; x++){ //loops 100000 times
array2[x] = ((rand()*rand() % 1000000)+1); //assigns random int to slot x of array2
t.Start();
quickSort(array2,0,array2_size-1); //calls quicksort
t.Stop();
}
cout << "(quicksort) size: " << array2_size <<" time: " << t.Seconds() << " seconds"<<endl; //prints array2 results
t.Reset();
delete [] array2; //clean up
for (int x=0; x<array3_size; x++){ //loops 1000000 times
array3[x] = ((rand()*rand() % 1000000)+1); //assigns a random number to each slot in array3
t.Start();
quickSort(array3,0,array3_size-1); //calls the quicksort method
t.Stop();
}
cout << "(quicksort) size: " << array3_size <<" time: " << t.Seconds()<< " seconds"<<endl; //prints the result
t.Reset();
delete [] array3; //clean up
for (int x=0; x<array4_size; x++){ //loops 10000 times
array4[x] = ((rand()*rand() % 1000000)+1); //assigns a random number to each slot in array4
t.Start();
selectionSort(array4,array4_size); //calls the selection sort method
t.Stop();
}
cout << "(selectionsort) size: " << array4_size <<" time: " << t.Seconds()<< " seconds"<<endl; //prints the result
t.Reset();
delete [] array4; //clean up
for (int x=0; x<array5_size; x++){ //loops 100000 times
array5[x] = ((rand()*rand() % 1000000)+1); //assigns a random number to each slot in array5
t.Start();
selectionSort(array5,array5_size); //calls the selection sort method
t.Stop();
}
cout << "(selectionsort) size: " << array5_size <<" time: " << t.Seconds()<< " seconds"<<endl; //prints the result
t.Reset();
delete [] array5; //clean up
for (int x=0; x<array6_size; x++){ //loops 1000000 times
array6[x] = ((rand()*rand() % 1000000)+1); //assigns a random number to each slot in array6
t.Start();
selectionSort(array6,array6_size); //calls the selection sort method
t.Stop();
}
cout << "(selectionsort) size: " << array6_size <<" time: " << t.Seconds()<< " seconds"<<endl; //prints the result
t.Reset();
delete [] array6; //clean up
}
int main(){
//creates three array lengths 10000,100000,
//and 1000000 respectively.
int array1_size = 10000;
int array2_size = 100000;
int array3_size = 1000000;
int array4_size = 10000;
int array5_size = 100000;
int array6_size = 1000000;
int *array1 = newint [array1_size]; // creates and initializes array1
Timer t;
for (int x=0; x<array1_size; x++){ //loop 10000 times.
array1[x] = ((rand()*rand() % 1000000)+1); //sets slot x of array1 to a random number.
t.Start(); //starts the timer.
quickSort(array1,0,array1_size-1); //calls quicksort supplying the array and range to sort.
t.Stop(); //stops the timer
}
cout<< "(quicksort) size: "<< array1_size <<" time: "<< t.Seconds()<<" seconds"<<endl ; //prints result
t.Reset(); //resets the timer back to 0
delete [] array1; //clean up
int *array2 = newint [array2_size];
for (int x=0; x<array2_size; x++){ //loops 100000 times
array2[x] = ((rand()*rand() % 1000000)+1); //assigns random int to slot x of array2
t.Start();
quickSort(array2,0,array2_size-1); //calls quicksort
t.Stop();
}
cout << "(quicksort) size: " << array2_size <<" time: " << t.Seconds() << " seconds"<<endl; //prints array2 results
t.Reset();
delete [] array2; //clean up
If by "causing a problem" you mean "containing very large numbers", then I guess you could say that. But really, are the smaller numbers so fast on your machine that the calculation is not accurate enough? (If so, you should probably give NASA their computer back...)