Quicksort troubles

I need to make a quicksort program that tells me how many swaps quicksortt goes through from an array that holds 10000 elements with a random number generator. I do have some code I have been trying to test but I can't get anything to show up on my compiler from my cout statements. What's wrong with this 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>

using namespace std;
int iaArray[50];
int iSize = 50;

void Partition(int* ipA, int iSize) 
{

    // Partitions of size 0 or 1 are already sorted
    if (iSize <= 1) 
    {
        return;
    }

    // Select a pivot from the array randomly
    int iPivot = ipA[rand() % iSize + 1];

    // Indices of the entries to be swapped
    int iLower = 0;
    int iUpper = iSize - 1;

    // Partition array into sections above and below the pivot
    while (iLower < iUpper) 
    {

        while (ipA[iLower] < iPivot) 
        {
            ++iLower;
        }

        while (ipA[iUpper] > iPivot) 
        {
            --iUpper;
        }

        // Swap the entries at the lower and upper indices
        int iTemp       = ipA[iLower];
        ipA[iLower]     = ipA[iUpper];
        ipA[iUpper]     = iTemp;
    }

    // Recursively call partition on each partititon.
    Partition(ipA, iLower);
    Partition(&(ipA[iLower + 1]), iSize - iLower - 1);
}

void Quicksort(int* ipA, int iSize) 
{
    // Seed the random number generator
  
     for (int x=0; x<50; x++)
    {
        iaArray[x] = rand() % 50 + 1;
    }
    Partition(ipA, iSize);
}

int main() 
{
    

    Quicksort(iaArray, iSize);

    // Output sorted array
    for (int i = 0; i < iSize; i++)
    {
        cout << iaArray[i] << "  ";
    }
    cout << endl;

cin.get();
    return 0;
}
1
2
3
4
5
6
7
8
       while (ipA[iLower] < iPivot) 
        {
            ++iLower;
        }

        while (ipA[iUpper] > iPivot) 
        {
            --iUpper;
¿What will happen when the 'Lower' index reaches the pivot element? As a suggestion, move the pivot to the "end" of the array and change the condition to while (ipA[iUpper] >= iPivot)

Also int iPivot = ipA[rand() % iSize + 1]; That's out of bounds.
Topic archived. No new replies allowed.