C语言数据结构希尔排序

#include stdio.h#include stdbool.h#define MAX 7int intArray[MAX] = {4,6,3,2,1,9,7};void printline(int count) {

编程学习网为您整理以下代码实例,主要实现:C语言数据结构希尔排序,希望可以帮到各位朋友。

#include <stdio.h>
#include <stdbool.h>

#define MAX 7

int intArray[MAX] = {4,6,3,2,1,9,7};

voID printline(int count) {
   int i;

   for(i = 0;i < count-1;i++) {
      printf("=");
   }

   printf("=\n");
}

voID display() {
   int i;
   printf("[");

   // navigate through all items 
   for(i = 0;i < MAX;i++) {
      printf("%d ",intArray[i]);
   }

   printf("]\n");
}

voID shellsort() {
   int inner, outer;
   int valuetoInsert;
   int interval = 1;   
   int elements = MAX;
   int i = 0;

   while(interval <= elements/3) {
      interval = interval*3 +1;
   }

   while(interval > 0) {
      printf("iteration %d#:",i); 
      display();

      for(outer = interval; outer < elements; outer++) {
         valuetoInsert = intArray[outer];
         inner = outer;

         while(inner > interval -1 && intArray[inner - interval] 
            >= valuetoInsert) {
            intArray[inner] = intArray[inner - interval];
            inner -=interval;
            printf(" item moved :%d\n",intArray[inner]);
         }

         intArray[inner] = valuetoInsert;
         printf(" item inserted :%d, at position :%d\n",valuetoInsert,inner);
      }

      interval = (interval -1) /3;
      i++;
   }          
}

int main() {
   printf("input Array: ");
   display();
   printline(50);
   shellsort();
   printf("Output Array: ");
   display();
   printline(50);
   return 1;
}

本文标题为:C语言数据结构希尔排序

基础教程推荐