Way to write Hill (shell) ordered

Sorting shell is the first packet, compare the size of each packet with the corresponding position data, exchange places, after the first packet ordering, grouping the binary reduced, above operation is repeated until the last packet is assigned to each element in units the end of the sort.

 void ShellSort(int arr[], int length){//该数组下标从0开始
      int group, i, j, temp;//j是其他组*对应i所在组的相对位置*的位置
      for (group = len / 2; group > 0; group /= 2){//每次排完后不断将分组二分,直至为1
          for (i = group;i < length; i++){//从第二组的第一个位置走到数组最后,在每个位置上判断比当前组号小的组,比较它们组内对应位置的大小相比
               for (j = i - group; j >= 0; j-=group){//找出所有比当前i所在组号小的组内的一个位置,它们在组内的位置与i在当前组的相对位置对应,进行比较
                   if (arr[j] > arr[j + group]){
                      temp = arr[j[;
                      arr[j] = arr[j + group];
                      arr[j + group] = temp;
                   }//每组对应位置比较大小,交换顺序
               }
          }
      }
 }

Published 19 original articles · won praise 4 · Views 508

Guess you like

Origin blog.csdn.net/qq_35050438/article/details/102872558