English 中文(简体)
快速执行
原标题:quicksort implementation

Following code for quicksort does not work and I can t understand what is reason.

#include <iostream>
using namespace std;
void exch(int a[],int i,int j){
    int s=a[i];
    a[i]=a[j];
    a[j]=s;

}
int  partition(int a[],int l,int h);
void quick(int a[],int l,int h){
    if (h<=l) return ;
    int j=partition(a,l,h);
    quick(a,l,j-1);
    quick(a,j+1,h);
    }
int partition(int a[],int l,int h){
    int i=l-1;
    int j=h;
    int v=a[l];
    while(true){

        while( a[++i]<v);

        while(a[--j]>v) if (j==i)  break;

            if (i>=j) break;

        exch(a,i,j);

    }

    exch(a,i,h);
    return i;



}
int main(){

    int a[]={12,43,13,5,8,10,11,9,20,17};
    int n=sizeof(a)/sizeof(int);
quick(a,0,n-1);
 for (int  i=0;i<n;i++){
     cout<<a[i]<<"  ";
 }
     return 0;
 }

产出

5  8  9  11  10  17  12  20  13  43
最佳回答

在<条码>中,应当采用方法。

int v = a[h]; 

http://www.ohchr.org。

int v = a[l];

[最新情况:我用这一变化对代码进行了考验,而且其运作是正确地的,产出:

5  8  9  10  11  12  13  17  20  43 
问题回答

下面是分治步骤的更明确的执行:

def quicksort(arr, low, high):
    if low > high or low == high:
        return

    pivot = randint(low, high)
    updated_pivot = partition(pivot,arr, low, high)
    quicksort(arr, low, updated_pivot-1)
    quicksort(arr, updated_pivot+1, high)


def partition(pivot, arr, low, high):
    arr[low], arr[pivot] = arr[pivot], arr[low] #now pivot is at  low  index of current subarray    
    start_of_ = 1 
    curr = 1
    while curr <= high:
       if arr[curr] <= arr[low]:
           arr[start], arr[curr] = arr[curr], arr[start] #making sure all values between index low and index start (exclusive)  are less than or equal to the pivot.
               start+=1 
       curr += 1

    new_pivot_location = start - 1 #the value at index start is the first value greater than the pivot (the range considered in the while loop is exclusive)
    arr[new_pivot_location], arr[low] =arr[low], arr[new_pivot_location]
    return new_pivot_location

EXAMPLE RUN:

投入:

[5,1,3,8, 0,2]
     |
   pivot

分配算法:

[3,1,5,8,0,2] --> after switching pivot s position
 |
pivot

  start
   |
[3,1,5,8,0,2] --> initializing start and curr
   |
  curr

    start
     |
[3,1,5,8,0,2] --> 1 < 3, so we swap 1 & 1, and start++, curr++
     |
    curr

    start
     |
[3,1,5,8,0,2] --> 5 > 3, so we don t swap. Don t move start, curr++
       |
      curr

    start
     |     
[3,1,5,8,0,2] --> 8 > 3, so we don t swap. Don t move start, curr++
         |
        curr

      start
       |
[3,1,0,8,5,2] --> 0 < 3, so we swap 5 and 0. start++, curr++
           |
           curr

        start
         |
[3,1,0,2,5,8] --> 2 < 3, so we swap 8 and 2. start++, curr++
             |
            curr

[2,1,0,3,5,8] --> swap 2 and 3, and reset pivot index.

产出:

[2,1,0,3,5,8]
       |
      pivot




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

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->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签