zoukankan      html  css  js  c++  java
  • Algs4-2.1.9给出希尔排序的轨迹

     2.1.9按照算法2.3所示轨迹的格式给出希尔排序是如何将数组 E A S Y S H E L L S O R T Q U E S T I O N排序的。
    答:灰底色表示相关元素未互换,黄底色表示相关元素互换。1-sort省略,与插入排序相同。

    图片

    图片
    图片
    public class Shell
    {
        public static void sort(Comparable[] a)
        {
            int N=a.length;
            int h=1;
            while (h<N/3) h=3*h+1;
            while (h>=1)
            {
                for (int i=h;i<N;i++)
                {
                 //   for (int j=i;j>=h && less(a[j],a[j-h]);j-=h)
                     for (int j=i;j>=h ;j-=h)
                    {
                       if(h>1) StdOut.printf("h=%-5d i=%-5d j=%-5d ",h,i,j);
                        if( less(a[j],a[j-h]))   exch(a,j,j-h);
                    }
                }
                h=h/3;
            }
        }
       
        private static boolean less(Comparable v,Comparable w)
        { return v.compareTo(w)<0;}
       
        private static void exch(Comparable[] a,int i,int j)
        {
            Comparable t=a[i];
            a[i]=a[j];
            a[j]=t;
        }
       
        private static void show(Comparable[] a)
        {
            for (int i=0;i<a.length;i++)
                StdOut.print(a[i]+" ");
            StdOut.println();
        }
       
        public static boolean isSorted(Comparable[] a)
        {
            for (int i=0;i<a.length;i++)
                if(less(a[i],a[i-1])) return false;
            return true;
        }
       
        public static void main(String[] args)
        {
            String[] a={"E","A","S","Y","S","H","E","L","L","S","O","R","T","Q","U","E","T","I","O","N"};
            sort(a);
        }
      
    }

  • 相关阅读:
    文件上传漏洞总结篇
    python 构造mysql爆破器
    python写exploit采集器
    文件包含漏洞总结
    python Flask篇(一)
    Python写一个目录检索器
    python爬搜狗微信获取指定微信公众号的文章
    python打造文件包含漏洞检测工具
    python打造漏洞补丁缺少检测
    表单
  • 原文地址:https://www.cnblogs.com/longjin2018/p/9860015.html
Copyright © 2011-2022 走看看