Skip to content

Instantly share code, notes, and snippets.

@marceloboeira
Created April 30, 2014 23:37
Show Gist options
  • Save marceloboeira/8b4351dfc2d908d01e96 to your computer and use it in GitHub Desktop.
Save marceloboeira/8b4351dfc2d908d01e96 to your computer and use it in GitHub Desktop.
Quick Sort
#include<stdio.h>
void qs(int *v, int a, int b){
int p, aux, i = a, j = b, m;
m = (int) ((i + j) / 2);
p = v[m];
while(j > i) {
while (v[i] < p) i = i + 1;
while (v[j] > p) j = j - 1;
if(i <= j){
aux = v[i];
v[i] = v[j];
v[j] = aux;
i++;
j--;
}
}
if(a < j) qs(v, a, j);
if(i < b) qs(v, i, b);
}
#define SIZE 21
int main(){
int v[SIZE] = {10, 72, 55, 39, 7, 9, 4, 3, 6, 1, 18, 21, 10, 12, 13, 17 ,21 , 32, 33, 34, 35};
int i = 0;
printf("Vetor desordenado:\n");
for(i = 0; i < SIZE; i++){
printf("%d ", v[i]);
}
printf("\n");
qs(v, 0, SIZE-1);
printf("Vetor ordenado:\n");
for(i = 0; i < SIZE; i++){
printf("%d ", v[i]);
}
printf("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment