Partitioning with a Pivot

Hey everyone,
I'm trying to write a program that has an array with 10 elements. It takes in a Pivot from the user and then gives the output:

elements lower than Pivot < Pivot < elements higher than Pivot


The thing is, there is a bug in my code and I cant for the love of me find it. It seems as there is always an increment in one number or something, because the same number pops up twice, i.e. if my array is {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, and I do this with the Pivot value 6, I get:
4 5 4 3 2 1 0 6 10 9 8

As you can see, the number 4 comes up twice. Anyone spot the bug? 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
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>

using namespace std;

void swap(int a[], int i, int j)
{
    int temp;
    temp = a[i];
    a[i] = a[j];
    a[j] = temp;
}

void find(int array[], int left, int right, int pivotIndex)
{
	int pivotValue = array[pivotIndex]; 
	swap(array, pivotIndex, right);  //Move pivot to end
	int storeIndex = left;
	int i;
	
	for (i = left; i < right; i++)
	{
		if (array[i] <= pivotValue)
		{
			swap(array, i, storeIndex);
			storeIndex++;
		}
	}
	swap(array, storeIndex, right);  //Move pivot to its final place
}

int main()
{
	int test[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
	int b, pivot;
	int size = sizeof( test ) / sizeof( test[0] );
	
	cout << "What position in the array should be the Pivot?" << endl;
	cin >> pivot;
	cout << "The Pivot value is: " << test[pivot] << endl;
	
	find(test, 0, size, pivot);
	
	while(b < size)
	{
		cout << test[b] << " ";
		b++;
	}
	
	return 0;
}
Lines 16,28 are out of bounds.
Topic archived. No new replies allowed.