Last active
July 7, 2022 17:50
-
-
Save salluzziluca/7769ae0fbc7ca0a52fe9f3a63c8eaefd to your computer and use it in GitHub Desktop.
Quicksort
This file contains 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 <stdbool.h> | |
void swap(int*vector,int a,int b){ | |
int aux=vector[a]; | |
vector[a]=vector[b]; | |
vector[b]=aux; | |
} | |
int pivotear(int*cosas,int cantidad, bool ascendente){ | |
int pos_pivote=cantidad-1; | |
int pos_pivote_final=0; | |
for(int i=0;i<pos_pivote;i++) | |
if(ascendente){ | |
if(cosas[i]<cosas[pos_pivote]){ | |
swap(cosas,i,pos_pivote_final); | |
pos_pivote_final ++; | |
} | |
} | |
else{ | |
if(cosas[i]>cosas[pos_pivote]){ | |
swap(cosas,i,pos_pivote_final); | |
pos_pivote_final ++; | |
} | |
} | |
swap(cosas,pos_pivote,pos_pivote_final); | |
return pos_pivote_final; | |
} | |
void quick_sort(int*vector,int tamanio, bool ascendente) | |
{ | |
if(tamanio <= 1) | |
return; | |
int pos_pivote=pivotear(vector,tamanio, ascendente); | |
quick_sort(vector, pos_pivote, ascendente); | |
quick_sort(vector+pos_pivote+1,tamanio-pos_pivote-1, ascendente); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment