Skip to content

Instantly share code, notes, and snippets.

@AstDerek
Created October 2, 2012 19:23
Show Gist options
  • Save AstDerek/3822712 to your computer and use it in GitHub Desktop.
Save AstDerek/3822712 to your computer and use it in GitHub Desktop.
Implementación simple de Quick Sort
#include <iostream>
using namespace std;
void intercambiar_int (int *a, int *b) {
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
void intercambiar_double (double *a, double *b) {
double tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
/**
* Implementación simple de quick sort
*/
void ordenar (double lista[],int longitud) {
int actual = 0,
pivote = longitud - 1,
direccion = 1;
if (longitud <= 1) {
return;
}
while (pivote != actual) {
if ((direccion == 1 && lista[actual] > lista[pivote]) || (direccion == -1 && lista[actual] < lista[pivote])) {
intercambiar_double(&lista[actual],&lista[pivote]);
intercambiar_int(&actual,&pivote);
direccion *= -1;
}
actual += direccion;
}
ordenar(&lista[0],pivote);
ordenar(&lista[pivote + 1],longitud - pivote - 1);
}
int main (int narg, char *varg[]) {
int n = 0, total = 3;
double *numeros, numero;
numeros = (double *)calloc(total,sizeof(double));
if (!numeros) {
cout << "No fue posible alojar memoria para iniciar el programa" << endl;
return 0;
}
cout << "Por favor capture " << total << " números a ordenar, uno a la vez:" << endl;
while (n < total) {
cin >> numeros[n++];
}
ordenar(numeros,total);
cout << "Los números ordenados son:" << endl;
for (n=0;n<total;n++) {
cout << numeros[n] << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment