Read Access Violation on QuickSort?

My program compiles, but when I do the quicksort call, I get a read access error...and I can't figure out why. Any help? 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
template <class T>
void sortQS(std::vector<T>& items, int first, int last) {
  if(first >= last)
    return;

  int i = first;
  int j = last;
  int pivot;
  int mid;
    cout << "i= " << i << " j= " << j << endl;
  if(first < last) {
    mid = (i + j) / 2;
    pivot = items[mid];
    while(i <= j) {
      while (items[i] < pivot) ++i;
      while (items[j] > pivot) ++j;
      if ( i <= j) {
        swap(items[i], items[j]);
        ++i;
        ++j;
      }
    }
    sortQS(items, first, i - 1); // My guess is this is the problem...
    sortQS(items, i, last);
  }
}
Last edited on
Topic archived. No new replies allowed.