简体   繁体   中英

Question about quicksort with std::vector

Below are some code I found from internet which is using integer array for input. It is working well but if I change array to vector, it just print the original input 95, 45, 48, 98, 1, 485, 65, 478, 1, 2325. Can anyone explain the reason of it happens and how to fix it?

#include <iostream>
#include <vector>

using namespace std;



            void printArray(vector<int> array, int n)
            {
                for (int i = 0; i < n; ++i)
                    cout << array[i] << endl;
            }

            void quickSort(vector<int> array, int low, int high)
            {
                int i = low;
                int j = high;
                int pivot = array[(i + j) / 2];
                int temp;

                while (i <= j)
                {
                    while (array[i] < pivot)
                        i++;
                    while (array[j] > pivot)
                        j--;
                    if (i <= j)
                    {
                        temp = array[i];
                        array[i] = array[j];
                        array[j] = temp;
                        i++;
                        j--;
                    }
                }
                if (j > low)
                    quickSort(array, low, j);
                if (i < high)
                    quickSort(array, i, high);
            }

            int main()
            {
                vector<int> array = {95, 45, 48, 98, 1, 485, 65, 478, 1, 2325};
                int n = sizeof(array)/sizeof(array[0]);

                cout << "Before Quick Sort :" << endl;
                printArray(array, n);

                quickSort(array, 0, n);

                cout << "After Quick Sort :" << endl;
                printArray(array, n);
                return (0);
            }

You are passing the vector by value to the quicksort function, which causes it to operate on a copy of the input vector (hence the original vector remains unchanged). A possible solution is to pass it by reference. So the declaration of quicksort should be:

void quickSort(std::vector<int> &array, int low, int high)

Another problem with the code is that sizeof(array)/sizeof(array[0]) is not the correct way to get the size of a vector, the valid way is to use the std::vector::size() method (as also pointed out in H. Guijt's answer)

  1. You are passing the array by value, so any changes you make to it in the quicksort function will not be visible to the caller. Pass it by reference instead.
  2. sizeof will return the size of the control block of the vector, not the number of bytes it is storing. Use vector::size() to get the count of elements.

Bonus: use std::swap instead of that temp variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM