Quick sort an array with large number of elements Terminated

Hi all,

I have not used C++ for a very long time. Recently I wrote a quick sort code with random pivot, and I wanted to calculate the time needed to run the quick sort function, but it only works with the array size up to 6 million. When I put 7 million, the program will be terminated even though I assign long long data type to all variables. Also can any of you show me how to count the comparisons in this quick sort? Please help!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

void random_quicksort(int *array, int first, int last);
int random_partition(int *array, int first, int last);
void swap(int array[], int i, int j);

int main()
{
	int size;
	clock_t start, end;
	cout << "Enter the size of the array: ";
	cin >> size;
	int *array;
	array = new int [size];
	srand(time(0));
	cout << "Initial array: ";
	for (int i = 0; i < size; i++)
	{
		array[i] = rand() % 100;	// the array consists of numbers from 0 to 99
		cout << array[i] << " ";	// print out the initial array
	}
	cout << endl;
	start = clock();
	random_quicksort(array, 0, size-1);
	end = clock();
	cout << "Sorted array: ";
	for (int i = 0; i < size; i++)
		cout << array[i] << " ";	// print out the sorted array
	cout << endl;
	cout << "Total time: " << (end - start)/((double) CLOCKS_PER_SEC) << " seconds" << endl;
	delete [] array;
	return -1;
}

void random_quicksort(int *array, int first, int last)
{
	int q;
	if (first < last)
	{
		q = random_partition(array, first, last);
		random_quicksort(array, first, q-1);
		random_quicksort(array, q+1, last);
	}
}

int random_partition(int *array, int first, int last)
{
	int pivot_loc;
	srand(time(0));
	pivot_loc = first + rand() % (last-first+1);
	int pivot = array[pivot_loc];
	swap(array, pivot_loc, last);
	pivot_loc = last;
	int i = first - 1;
	for(int j = first; j <= last-1; j++)
	{
		if(array[j] <= pivot)
		{
			i++;
			swap(array, i, j);
		}
	}
	swap(array, i+1, pivot_loc);
	return i+1;
}

void swap(int *array, int i, int j)
{
	int temp;
	temp = array[i];
	array[i] = array[j];
	array[j] = temp;
}
Topic archived. No new replies allowed.