使用 C 希尔排序的 DSA


概述

希尔排序是一种高效的排序算法,基于插入排序算法。该算法避免了大的移位,就像插入排序的情况一样,如果较小的值位于最右边并且必须移动到最左边。该算法首先对分布广泛的元素使用插入排序来对它们进行排序,然后对分布较窄的元素进行排序。该间距称为间隔。该间隔是根据 Knuth 公式计算的 (h=h*3 +1),其中 h 是间隔,初始值为 1。该算法对于中等规模的数据集非常有效,因为其平均和最坏情况复杂度均为 O(n ) 其中 n 为否。的项目。

伪代码

procedure shellSort( A : array of items )
   int innerPosition, outerPosition
   int valueToInsert, interval = 1
   /* calculate interval*/
   while interval < A.length /3 do:
      interval = interval * 3 +1	    
   
   while interval > 0 do:
      for outer = interval; outer < A.length; outer ++ do:
         /* select value to be inserted */
	      valueToInsert = A[outer]
         inner = outer;
         /*shift element towards right*/
         while inner > interval -1 && A[inner - interval] >= valueToInsert do:
            A[inner] = A[inner-1]
            inner = inner - interval
         end while
         /* insert the number at hole position */
         A[inner] = valueToInsert
      end for
      /* calculate interval*/
      interval = (interval -1) /3;	  
   end while
end procedure

例子

#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++;
   }          
}
main(){
   printf("Input Array: ");
   display();
   printline(50);
   shellSort();
   printf("Output Array: ");
   display();
   printline(50);
}

输出

如果我们编译并运行上面的程序,那么它将产生以下输出 -

Input Array: [4, 6, 3, 2, 1, 9, 7]
==================================================
iteration 0#: [4, 6, 3, 2, 1, 9, 7]
   item moved :4
   item inserted :1, at position :0
   item inserted :9, at position :5
   item inserted :7, at position :6
iteration 1#: [1, 6, 3, 2, 4, 9, 7]
   item inserted :6, at position :1
   item moved :6
   item inserted :3, at position :1
   item moved :6
   item moved :3
   item inserted :2, at position :1
   item moved :6
   item inserted :4, at position :3
   item inserted :9, at position :5
   item moved :9
   item inserted :7, at position :5
Output Array: [1, 2, 3, 4, 6, 7, 9]
==================================================
dsa_using_c_sorting_techniques.htm