集册 Java实例教程 Shell排序的Java实现

Shell排序的Java实现

欢马劈雪     最近更新时间:2020-01-02 10:19:05

453
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
Shell排序的Java实现
/**
时代Java公众号 - nowjava.com 提供 
**/



public class shellSort 

{

    public void sort(int input[]) // Function implementing Insertion Sort

    {

          int n = input.length, temp, j;

          for (int gap = n / 2; gap > 0; gap /= 2) 

          {

              for (int i = gap; i < n; i++) // Gapped insertion sort

              {

                  temp = input[i];


                  for (j = i; j >= gap && input[j - gap] > temp; j = j - gap)

                      input[j] = input[j - gap];/* from n o w j a v a . c o m - 时代Java*/

                    

                  input[j] = temp;

              }

          }

    }


    public static void main(String[] args) 

    {

        int input[] = {5, 6, 8, 4, 6, 5, 3, 3, 2, 7, 8, 45, 85, 96}; //Input array


        shellSort ob = new shellSort();


        ob.sort(input);


        System.out.print(
展开阅读全文