Last active
July 1, 2018 01:10
-
-
Save senapk/96d43424a79d3992eb5c37e8f0024a53 to your computer and use it in GitHub Desktop.
quicksort compactado em c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> //rand srand | |
#include <time.h>//time | |
#define SWAP(x, y) do { typeof(x) SWAP = x; x = y; y = SWAP; } while (0) //gcc only | |
void quick(int * vet, int C, int F){ | |
if(C >= F) return; | |
int i = C, j = F, pivo = vet[(i + j)/2]; | |
while(i <= j){ | |
while (vet[i] < pivo) i++; | |
while (vet[j] > pivo) j--; | |
if(i <= j) {SWAP(vet[i], vet[j]); i++; j--;} | |
} | |
quick(vet, C, j); | |
quick(vet, i, F); | |
} | |
void show(int * vet, int size){ | |
printf("[ "); | |
for(int i = 0; i < size; i++) | |
printf("%d ", vet[i]); | |
printf("]\n"); | |
} | |
int main(){ | |
srand(time(NULL)); | |
int vet[30]; | |
int size = sizeof(vet)/sizeof(int); | |
for(int i = 0; i < size; i++) | |
vet[i] = rand() % 90 + 10; //gerar números de 2 dígitos | |
show(vet, size); | |
quick(vet, 0, size - 1); | |
show(vet, size); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment