Skip to content

Instantly share code, notes, and snippets.

@salluzziluca
Last active July 7, 2022 17:29
Show Gist options
  • Save salluzziluca/4ad8a6e0c83d2d4b0ef4750c6730cffd to your computer and use it in GitHub Desktop.
Save salluzziluca/4ad8a6e0c83d2d4b0ef4750c6730cffd to your computer and use it in GitHub Desktop.
Selection Sort
//A selection sort algorithm that sorts descendingly or ascendingly depending of the boolean parameter.
void selection_sort(char vector_desordenado[MAX_VECTOR], char vector_ordenado[MAX_VECTOR], int tope, bool ascendente){
for(int i = 0, i < tope, i++)
vector_ordenado[i] = vector_desordenado[i];
for (int i = 0; i < tope; i++){
int pos_min = i;
for (int j = i + 1; j < tope; j++){
if (ascendente){
if (vector_ordenado[j] < vector_ordenado[pos_min])
pos_min = j;
}
else{
if (vector_ordenado[j] > vector_ordenado[pos_min])
pos_min = j;
}
}
char aux = vector_ordenado[i];
vector_ordenado[i] = vector_ordenado[pos_min];
vector_ordenado[pos_min] = aux;
}
}
def seleccion(lista):
largo = len(lista)
for i in range(n):
min = i
for j in range (i+1, n):
if (lista[j]<lista[min]):
min = j
lista[i], lista[min] = lista[min], lista [i]
return lista
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment