i am creating 5 deferent tables each one has 20000 more elements more than the previous when i try to sort them with the sort algorithm quick sort for the first table tha has 20000 elements runs grate then it throws this exception how is this fixable ? exception : terminate called after throwing an instanceof 'std::bad_alloc' this application has Requested the Runtime to terminate it in an unusual way. Please contact the applications's support team for more information. Process returned 255(0xFF) . here is the code that gives this return :
Code:
#include<time.h>
#include<stdlib.h>
#include <string.h>
#include <ctime>
using namespace std;
int * Create_Table(int table[],int N);
void Quic_kSort(int arr[], int left, int right);
int main()
{
for (int N=20000; N<=100000; N=N+20000)
{
//clock_t begin = clock();
int *table = new int32_t [N];
table=Create_Table(table,N);
Quic_kSort(table,0,N);
system("pause");
cout << endl;
clock_t end = clock();
//double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
//cout << "The elapsed time for N : "<<N<< " elements is : "<<elapsed_secs<<endl;
//delete table;
}
}
int * Create_Table(int table[],int N)
{
srand(time(NULL));
for (int i=0; i<N; i++)
table[i]= rand()%150000;
return table;
}
void Quic_kSort(int arr[], int left, int right)
{
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) / 2];
/* partition */
while (i <= j) {
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
}
/* recursion */
if (left < j)
Quic_kSort(arr, left, j);
if (i < right)
Quic_kSort(arr, i, right);
}