quicksort alogorithm not sorting :-(

So after many helpful hints I finally am compiling!! However, my quicksort is not working correctly and I don't understand why! The goal is to sort a vector of student records by studentID. I thought I had accessed everything correctly ...should something be const or by reference that I am missing?

Thanks all!

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <cstdlib>
#include <iostream>
#include <vector>
#include "Student.h"

using namespace std;

void displayVector(vector<Student>);
void quickSort(vector<Student> &, int, int);
void swap(Student &, Student &);
int partition(vector<Student>, int, int);

int main(int argc, char *argv[])
{
       
    float janeDoeTests[] = { 85, 89.5, 90, 75, 56, 79, 100, 45, 67, 88  };
    float elvisPresleyTests[] = { 50, 34, 23, 26, 46, 56, 67, 89, 90, 22 };
    float mickJaggerTests[] = { 100, 24, 46, 56, 35, 46, 78, 90, 78, 90 };
    float cherTests[] = { 97, 98, 45, 67, 36, 67, 47, 89, 25, 46};
    float beethovenTests[] = { 45, 56, 78, 57, 68, 97, 86, 65, 86, 87}; 
    float johnLennonTests[] = { 68, 78, 89, 78, 68, 58, 78, 98, 97, 96};
    float roseanneTests[] = { 86, 87, 97, 68, 79, 89, 86, 76, 75, 78};
    float parisHiltonTests[] = { 100, 24, 46, 56, 67, 96, 74, 86, 97, 78};
    float seanConneryTests[] = { 68, 57, 79, 80, 68, 57, 79, 78, 57, 90};
    float mozartTests[] = { 75, 76, 74, 98, 67, 87, 98, 46, 68, 98}; 
    float pennyLaneTests[] = { 79, 89, 58, 78, 89, 78, 89, 96, 98, 99};
    float kellyRipaTests[] = { 76, 87, 97, 96, 85, 74, 67, 89, 90, 22 };
    float newtonPerryTests[] = { 57, 68, 79, 85, 74, 86, 97, 99, 47, 67 };
    float sarahParkerTests[] = { 97, 67, 78, 96, 76, 87, 98, 89, 25, 46};
    float joanRiversTests[] = { 65, 76, 96, 86, 87, 98, 99, 85, 65, 68}; 
    float barryWhiteTests[] = { 57, 86, 97, 75, 56, 79, 100, 45, 67, 88  };
    float caryGrantTests[] = { 67, 86, 97, 76, 86, 87, 98, 95, 90, 57 };
    float starJenkinsTests[] = { 100, 24, 78, 89, 90, 96, 95, 98, 99, 59};
    float mikeDouglasTests[] = { 68, 79, 89, 69, 59, 49, 89, 99, 69, 89};
    float drewSmithTests[] = { 100, 100, 100, 100, 100, 100, 100, 100, 100, 100}; 

    
    Student student1(1, "Jane Doe", janeDoeTests);
    Student student2(2, "Elvis Presley", elvisPresleyTests);    
    Student student3(3, "Mick Jagger", mickJaggerTests); 
    Student student4(4, "Cher", cherTests);
    Student student5(5, "Beethoven", beethovenTests);
    Student student6(6, "John Lennon", johnLennonTests);
    Student student7(7, "Roseanne", roseanneTests);
    Student student8(8, "Paris Hilton", parisHiltonTests);    
    Student student9(9, "Sean Connery", seanConneryTests); 
    Student student10(10, "Mozart", mozartTests);
    Student student11(11, "Penny Lane", pennyLaneTests);
    Student student12(12, "Kelly Ripa", kellyRipaTests);
    Student student13(13, "Newton Perry", newtonPerryTests);
    Student student14(14, "Sarah Parker", sarahParkerTests);    
    Student student15(15, "Joan Rivers", joanRiversTests); 
    Student student16(16, "Barry White", barryWhiteTests);
    Student student17(17, "Cary Grant", caryGrantTests);
    Student student18(18, "Star Jenkins", starJenkinsTests);
    Student student19(19, "Mike Douglas", mikeDouglasTests);
    Student student20(20, "Drew Smith", drewSmithTests);  

    const int numStudents = 20;
    vector<Student> studentVector;
    
    studentVector.push_back(student17);
    studentVector.push_back(student18);
    studentVector.push_back(student6);   
    studentVector.push_back(student19);
    studentVector.push_back(student20);    
    studentVector.push_back(student5);
    studentVector.push_back(student1);
    studentVector.push_back(student2);   
    studentVector.push_back(student4);
    studentVector.push_back(student15);   
    studentVector.push_back(student16);
    studentVector.push_back(student3);
    studentVector.push_back(student8);   
    studentVector.push_back(student7);
    studentVector.push_back(student13);   
    studentVector.push_back(student14);
    studentVector.push_back(student9);
    studentVector.push_back(student10);   
    studentVector.push_back(student11);
    studentVector.push_back(student12);   
    
    cout << "Here is your unsorted vector: " << endl;
    cout << "--------------------------------------------------------------------------" << endl;
    cout << endl;
    
    displayVector(studentVector);
    
    quickSort(studentVector, 0, numStudents-1);
    
    cout << "Here is your vector sorted by student IDs: " << endl;
    cout << "--------------------------------------------------------------------------" << endl;
    cout << endl;
    
    displayVector(studentVector);

    
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void displayVector(vector<Student> vect)
{
    for(int count = 0; count < vect.size(); count++)
    {
         cout << "Student ID: " << vect[count].getID() << endl;
         cout << "Student Name: " << vect[count].getName() << endl;
         cout << "Student Average: " << vect[count].getAverage() << endl;
         vect[count].displayTests();        
         cout << endl;
         cout << endl;
    }
}

int partition(vector<Student> vect, int start, int end)
{
    int pivotValue, pivotIdx, mid;
    
    mid = (start + end)/2;
    swap(vect[start], vect[mid]);
    pivotIdx = start;
    pivotValue = vect[start].getID();
    
    for(int i = start + 1; i <= end; i++)
    {
        if(vect[i].getID() < pivotValue)
        {
            pivotIdx++;
            swap(vect[pivotIdx], vect[i]);
        }
    }
    swap(vect[start], vect[pivotIdx]);
    return pivotIdx;
}

void quickSort(vector<Student> & vect, int start, int end)
{
    int pivotPoint;    
    
    if(start < end)
    {
        pivotPoint = partition(vect, start, end);
        quickSort(vect, start, pivotPoint-1);
        quickSort(vect, pivotPoint + 1, end);
    }
}

void swap(Student &student1, Student &student2)
{
    Student tempStudent = student1;
    student1 = student2;
    student2 = tempStudent;
}
On line 117 you're copying the vector. nothing inside of partition() will take place outside

you need a reference like in quickSort()
Brilliant!! Thank you!! Is it by reference because I move stuff around in the partition function? Please correct me if my thinking is wrong-- but you pass by reference if you are going to alter what you are passing, and if it's not by reference, then you are just accessing what you are passing?
Is it by reference because I move stuff around in the partition function?
Yes. It's because you want to modify that object

The main reason for passing parameters by reference is that you don't want to copy the data which is done when you passing paramter by value (like did with vect of partition()).

A vector might have a lot of elements. If you always copy them it's a waste of time.

if you hav a const reference (const vector<Student> & vect) it is like passing by value just without the time penalty.
Topic archived. No new replies allowed.