recursiva
#include <stdio.h> utilizando espacio de nombres std; int partición ( int s [], int l, int r); void QuickSort ( int s [], int l, int r); int main () { int s [ 9 ] = { 7 , 5 , 4 , 6 , 2 , 3 , 1 , 9 , 8 }; QuickSort (s, 0 , 8 ); para ( int I = 0 ; <I . 9 ; I ++) el printf ( " % D " , S [I]); } int reparto ( int S [], int L, int R & lt) { IF (L> = R & lt) de retorno L; int pivote S = [L]; // hub toma un primer número de la mientras que (L < R & lt) { el tiempo (L <R & lt && S [R & lt]> = pivote) r--; // comenzando por la derecha del cubo es menor que el escaneo IF (L <R & lt) S [L] = S [R & lt]; // a un valor pequeño a la izquierda de la mientras que (L <R & lt && S [L] <= pivote) L ++ ; SI (L <R & lt) S [R & lt] = S [L]; } S [L] = pivote; // foso cavado recuerde llenar retorno L; // devolver la posición hub } void QUICKSORT ( int S [], int L, int R & lt) { IF (L < R & lt) { int pivotIndex = partición (S, L, R & lt); QUICKSORT (S, L, pivotIndex - 1. ); QUICKSORT (S, pivotIndex + . 1 , R & lt); } } // fuente de referencia es QuickSort.h
No recursivo
#include <stdio.h> #include <pila> utilizando espacio de nombres std; int partición ( int s [], int l, int r); void quickSort_stack ( int s [], int n); int main () { int s [ 9 ] = { 7 , 5 , 4 , 6 , 2 , 3 , 1 , 9 , 8 }; quickSort_stack (s, 9 ); para ( int I = 0 ; <I . 9 ; I ++) el printf ( " % D " , S [I]); } int reparto ( int S [], int L, int R & lt) { IF (L> = R & lt) de retorno L; int S = pivote [L]; // hub toma un primer número de la mientras que (L < R & lt) { el tiempo (L <R & lt && S [R & lt]> = pivote) r--; // comenzando por la derecha es menor que el hub de exploración si (L <R & lt) S [L] = S [R & lt]; // a un valor pequeño a la izquierda de la mientras que (L <R & lt && S [L] <= pivote) L ++ ; SI (L <R & lt) S [R & lt] = S [L]; } S [L] = pivote; // foso cavado recuerde llenar retorno L; // devolver la posición hub } void quickSort_stack ( int A [], int n-) { int L = 0 , R & lt = N- 1. ; Stack < int > S; s.push (L); s.push (R & lt); el tiempo (! s.empty ()) { // naturaleza también juega un papel subproblemas información de posición pila almacenada R & lt = s.top () ; s.pop (); L = s.top (); s.pop (); int privotIndex = partición (A, L, R & lt); si(privotIndex- 1 > l) { s.push (l); s.push (privotIndex - 1 ); } Si (privotIndex + 1 < r) { s.push (privotIndex + 1 ); s.push (r); } } }