diff options
author | leo <leo@azuminha.com> | 2023-03-06 01:32:31 -0300 |
---|---|---|
committer | leo <leo@azuminha.com> | 2023-03-06 01:32:31 -0300 |
commit | 033867165ee1398d5d94f265754534edc2a2a394 (patch) | |
tree | b44aae61efa7dca262655d593026635c8cd7c43e /Random/quicksort.c | |
parent | acadd0b6cd11d384e66302a66df8d4edb6a0e862 (diff) |
Diffstat (limited to 'Random/quicksort.c')
-rw-r--r-- | Random/quicksort.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/Random/quicksort.c b/Random/quicksort.c new file mode 100644 index 0000000..3d261db --- /dev/null +++ b/Random/quicksort.c @@ -0,0 +1,45 @@ +#include <stdio.h> +#include <stdlib.h> +int ve = 0; +void quick_sort(int *v, int inicio, int fim){ + if(inicio >= fim) return; + int posPivot = (inicio+fim)/2; + int pivot = v[posPivot]; + int sp = inicio; + int sv = fim; + while(sp != sv){ + ve++; + if(sp < posPivot){ + if(v[sp] > pivot){ + v[posPivot] = v[sp]; + posPivot = sp; + }else{ + sp++; + } + }else if(sv > posPivot){ + if(v[sv] < pivot){ + v[posPivot] = v[sv]; + posPivot = sv; + }else{ + sv--; + } + } + } + v[sv] = pivot; + + quick_sort(v, inicio, sv-1); + quick_sort(v, sv+1, fim); +} + +int main(){ + + int v[3] = {7,4,2}; + + for(int i = 0; i < 3; ++i) printf("%d ", v[i]); + printf("\n"); + quick_sort(v, 0, 2); + for(int i = 0; i < 3; ++i) printf("%d ", v[i]); + printf("\n"); + printf("%d\n", ve); + return 0; +} |