Skip to content

Instantly share code, notes, and snippets.

@MrDave1999
Created July 27, 2019 16:14
Show Gist options
  • Save MrDave1999/2605bdf56338862584252e24614d18f9 to your computer and use it in GitHub Desktop.
Save MrDave1999/2605bdf56338862584252e24614d18f9 to your computer and use it in GitHub Desktop.
Algoritmo de inserción en C
#include <stdio.h>
#define MAX_NUM 50
int InsertionSort(int* const parray, const int size)
{
int aux;
int* i;
int* j;
const int* const psize = parray + size;
const int* const pbegin = parray - 1; //este puntero apunta a la dirección base del arreglo.
int count = 0;
int total = 0;
for(i = parray + 1; i != psize; ++i)
{
count = 0;
for(j = i - 1; j != pbegin; --j)
{
++count;
if(*(j + 1) < *j)
{
aux = *j;
*j = *(j + 1);
*(j + 1) = aux;
}
}
total += count;
}
return total;
}
int main(void)
{
srand(time(NULL));
int num[MAX_NUM];
int tot;
for(int i = 0; i != MAX_NUM; ++i)
num[i] = rand() % 100;
tot = InsertionSort(num, MAX_NUM);
for(int i = 0; i != MAX_NUM; ++i)
printf("%d\n", num[i]);
printf("\nTotal de iteraciones: %d\n", tot);
getchar();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment