Why does it say segmention fault : 11;

Write your question here.
Hi,
When I run this code it print segmention fault : 11 and does not print out he array. Here is my code:
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
  #include <iostream>

using namespace std;

void swap(int &a, int &b) {
	int temp;
	temp = a;
	a = b;
	b = temp;
}

int quickSort(int arr[], int size) {
	int pivot = arr[size - 1];
	int iCounter = 0;
	for(int jCounter = 1; jCounter < size - 1; jCounter++) {
		if(arr[jCounter] < pivot) {
				iCounter++;
				swap(arr[iCounter], arr[jCounter]);
		}
		if(jCounter == size - 2 && arr[jCounter] > pivot) {
				iCounter++;
				swap(pivot, iCounter);
				quickSort(arr, size);
		}
	}
	for(int counter = 0; counter < size - 1; counter++) {
		cout << "Sorted List :" << arr[counter];
	}
}

int main() {
	cout << "In this program I will do quick sort" << endl;
	cout << "The list will be 1, 8, 4" << endl;
	int arr[] = {0, 1, 8, 4};
	int size = sizeof(arr)/sizeof(arr[0]);
	quickSort(arr, size);
}
stack overflow, the problem is never reduced (line 23).

Also, ¿what's the meaning of `pivot'?
At line 13 it seems to be an element, but at line 22 you treat it as an index.

And you never consider the last element of the array, your loop stops too soon.
I am trying to do quick sort
Topic archived. No new replies allowed.