Skip to content

Instantly share code, notes, and snippets.

@MrDave1999
Last active July 27, 2019 21:58
Show Gist options
  • Save MrDave1999/d036d005763e5c3f4f43dc8a10d90de4 to your computer and use it in GitHub Desktop.
Save MrDave1999/d036d005763e5c3f4f43dc8a10d90de4 to your computer and use it in GitHub Desktop.
Algoritmo shell en C
#include <stdio.h>
#define MAX_NUM 50
int ShellSort(int* const parray, const int size)
{
int aux;
int count = 0;
int total = 0;
int* i;
const int* const psize = parray + size;
unsigned char change = 0;
int jumps = size;
while(1)
{
if(jumps != 1)
jumps /= 2;
if(jumps == 1)
change = 1;
count = 0;
for(i = parray; i + jumps < psize; ++i)
{
++count;
if(*(i + jumps) < *i) //Si el elemento siguiente llega a ser menor al anterior, ...
{
aux = *(i + jumps);
*(i + jumps) = *i;
*i = aux;
change = 0;
}
}
total += count;
if(change) break;
}
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 = ShellSort(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