Skip to content

Instantly share code, notes, and snippets.

@salluzziluca
Last active July 7, 2022 13:22
Show Gist options
  • Save salluzziluca/68928c604db01a8124cc41d588bb8a01 to your computer and use it in GitHub Desktop.
Save salluzziluca/68928c604db01a8124cc41d588bb8a01 to your computer and use it in GitHub Desktop.
Ordenamiento por Inserccion
void insertion_sort(char vector_desordenado[MAX_VECTOR], char vector_ordenado[MAX_VECTOR], int tope, bool ascendente){
int i, j;
for(i = 0; i < tope; i++)
vector_ordenado[i] = vector_desordenado[i];
for(i = 0; i < tope; i++){
int elemento_actual = vector_ordenado[i];
j = i-1;
if(ascendente){
while (j >= 0 && elemento_actual < vector_ordenado[j]){
vector_ordenado[j+1] = vector_ordenado[j];
j--;
}
vector_ordenado[j+1] = elemento_actual;
}
if(!ascendente){
while (j >= 0 && elemento_actual > vector_ordenado[j]){
vector_ordenado[j+1] = vector_ordenado[j];
j--;
}
vector_ordenado[j+1] = elemento_actual;
}
}
}
def inserccion(lista):
largo = len(lista)
for i in range(1, largo):
elemento = lista[i]
j = i-1
while j>=0 and elemento <lista[j]:
lista[j+1]=lista[j]
j-=1
lista[j+1]=elemento
return lista
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment