English 中文(简体)
C++: 不得因沃德职能而受影响
原标题:C++: Variable Not Affected By The Void Function

我试图实施基本的快速算法,我认为我已经正确执行。 然而,这些职能根本不影响阵列。 原因何在? 我可以列举一下我决定在这里与我的同业者协商的错误:

#include <iostream>
#include <vector>

using namespace std;

int partition(vector<int> A,int low,int high)
{
    int pivot=A[high-1];
    int boundryForLowerArray=low-1;
    for(int i=low;i<high-2;i++)
    {
        if(A[i]<=pivot)
        {
            boundryForLowerArray++;
            swap(A[i],A[boundryForLowerArray]);
        }
    }
    swap(pivot,A[boundryForLowerArray+1]);
    return boundryForLowerArray+1;
}
void quickSort(vector<int>A,int low,int high)
{
    if(low<high)
    {
        int q=partition(A,low,high);
        quickSort(A, low, q-1);
        quickSort(A, q+1, high);
    }
}


int main(int argc, const char * argv[])
{

    vector<int>A,sorted;
    A.push_back(2);
    A.push_back(8);
    A.push_back(7);
    A.push_back(1);
    A.push_back(3);
    A.push_back(5);
    A.push_back(6);
    A.push_back(4);
    quickSort(A, 0, A.size());
    for(int i=0;i<A.size();i++)
        cout<<A[i]<<" ";
    return 0;
}
最佳回答

您通过A,而不是参考,因此,quickSort<>/code>正在制成A版并作分类。 而是试图通过参考通过病媒:

int partition(vector<int>& A,int low,int high)

......

void quickSort(vector<int>& A,int low,int high)
问题回答

因为你以价值而不是参照的方式通过斜线。 实际上,你应当与主持人一道,作为参数,在阵列的开始和终结时,发挥功能。 此外,你的算法应当接受任何类型的代用器。 因此,你们应当使用模板!

template<class Iterator>
void quick_sort(Iterator begin, Iterator end) { 
   for(auto iter = begin;iter != end;iter++) 
      *iter; // access to the value of the iterator




相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...