Advertisement
harrisonmk

Quick sort

Mar 16th, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3.  
  4. using namespace std;
  5.  
  6. int quickSort(int Vet[], int inicio, int fim);
  7.  
  8. int main(int argc, char** argv)
  9. {
  10.     int n = 5;
  11.     int vetor[] = {20, 35, 15, 12, 25};
  12.     int i;
  13.  
  14.     quickSort (vetor, 0, 4);
  15.     //no quick sort se passa o vetor o inicio do vetor e o fim e não o tamanho como os outros//
  16.  
  17.     printf("Vetor Ordenado Quick Sort\n\n");
  18.  
  19.     for(i = 0; i < n; i++)
  20.     {
  21.  
  22.         printf (" %d ", vetor[i]);
  23.  
  24.     }
  25.  
  26.     printf ("\n");
  27.  
  28.     return 0;
  29. }
  30.  
  31.  
  32.  
  33. int quickSort(int Vet[], int inicio, int fim)
  34. {
  35.     int i, j, pivot, aux;
  36.     i = inicio;
  37.     j = fim;
  38.     pivot = Vet[(inicio + fim) / 2];
  39.     do
  40.     {
  41.         while (Vet[i] < pivot && i < fim) i++;
  42.         while (pivot < Vet[j] && j > inicio) j--;
  43.         if (i <= j)
  44.         {
  45.             if (i < j)
  46.             {
  47.                 aux = Vet[i];
  48.                 Vet[i] = Vet[j];
  49.                 Vet[j] = aux ;
  50.             }
  51.             i++;
  52.             j--;
  53.         }
  54.     }
  55.     while (i <= j);
  56.     if(inicio < j) quickSort (Vet, inicio, j);
  57.     if(i < fim ) quickSort (Vet, i, fim);
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement