/* shell_sort.c
Author : Mr. Jake Rodriguez Pomperada,MAED-IT, MIT
Tool : Dev C++ 5.11
Date : August 15, 2021 Sunday 4:29 PM
Websites : www.jakerpomperada.com and www.jakerpomperada.blogspot.com
Email :jakerpomperada@gmail.com
Location : Bacolod City, Negros Occidental, Philippines
*/
#include <stdio.h>
#include <stdlib.h>
void shellsort(int arr[], int num);
int main()
{
int items[1000], num=0,min=0,loc=0;
int c=0, b=0,change=0,j=0,temp=0;
system("cls");
printf("\n\n");
printf("\tShell Sort Program in C");
printf("\n\n");
printf("\tHow many items? : ");
scanf("%d", &num);
printf("\n\n");
for (c= 0; c < num; c++) {
printf("\tEnter item no. %d: ", c+1);
scanf("%d", &items[c]);
}
printf("\n\n");
printf("\tOriginal Arrangement of Numbers");
printf("\n\n");
for ( c = 0 ; c < num ; c++ ) {
printf("\t%d ",items[c]);
}
shellsort(items, num);
printf("\n\n");
printf("\tAsceding Order of Numbers");
printf("\n\n");
for ( c = 0 ; c < num ; c++ ) {
printf("\t%d ", items[c]);
}
printf("\n\n");
printf("\tEnd of Program");
printf("\n\n");
system("pause");
}
void shellsort(int arr[], int num)
{
int i, j, k, tmp;
for (i = num / 2; i > 0; i = i / 2)
{
for (j = i; j < num; j++)
{
for(k = j - i; k >= 0; k = k - i)
{
if (arr[k+i] >= arr[k])
break;
else
{
tmp = arr[k];
arr[k] = arr[k+i];
arr[k+i] = tmp;
}
}
}
}
}
No comments:
Post a Comment