Algs4-2.1.17动画-插入排序
生活随笔
收集整理的這篇文章主要介紹了
Algs4-2.1.17动画-插入排序
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
2.1.17動畫。修改插入排序和選擇排序的代碼,使之將數(shù)組內(nèi)容繪制成正文中所示的棒狀圖。在每一輪排序后重繪圖片來產(chǎn)生動畫效果,并以一張“有序”的圖片作為結(jié)束,即所有圓棒均已按照高度有序排列。提示:使用類似于正文中的用例來隨機生成Double值,在排序代碼的適當(dāng)位置調(diào)用show()方法,并在show()方法中清理畫布并繪制棒狀圖。
public class Insertion
{
??? public static void sort(Comparable[] a)
??? {
??????? int N=a.length;
??????? for (int i=0;i<N;i++)
??????? {
??????????? for(int j=i;j>0 && less(a[j],a[j-1]);j--)
??????????????? exch(a,j,j-1);
???????????? showAnimation(a);
??????? }
??? }
???
??? 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();
??? }
???
??? private static void showAnimation(Comparable[] a)
??? {
?????? StdDraw.setXscale(0.0,a.length);
?????? StdDraw.setYscale(0.0,a.length);
?????? StdDraw.setPenRadius(0.005);
?????? StdDraw.pause(100);
?????? StdDraw.clear(StdDraw.GRAY);
?????? StdDraw.setPenColor(StdDraw.BLACK);
??????? for(int i=0;i<a.length;i++)
??????? {
??????????? StdDraw.line(i*1.0,0.0,i*1.0,(Double)a[i]*1.0);
??????? }
??? }
???
??? 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)
??? {
??????? int N=Integer.parseInt(args[0]);
??????? Double[] a=new Double[N];
??????? for(int i=0;i<N;i++)
??????????? a[i]=StdRandom.uniform(0.0,N*1.0);
??????? //
??????? StdDraw.pause(5000);
??????? sort(a);
??? }
}
public class Insertion
{
??? public static void sort(Comparable[] a)
??? {
??????? int N=a.length;
??????? for (int i=0;i<N;i++)
??????? {
??????????? for(int j=i;j>0 && less(a[j],a[j-1]);j--)
??????????????? exch(a,j,j-1);
???????????? showAnimation(a);
??????? }
??? }
???
??? 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();
??? }
???
??? private static void showAnimation(Comparable[] a)
??? {
?????? StdDraw.setXscale(0.0,a.length);
?????? StdDraw.setYscale(0.0,a.length);
?????? StdDraw.setPenRadius(0.005);
?????? StdDraw.pause(100);
?????? StdDraw.clear(StdDraw.GRAY);
?????? StdDraw.setPenColor(StdDraw.BLACK);
??????? for(int i=0;i<a.length;i++)
??????? {
??????????? StdDraw.line(i*1.0,0.0,i*1.0,(Double)a[i]*1.0);
??????? }
??? }
???
??? 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)
??? {
??????? int N=Integer.parseInt(args[0]);
??????? Double[] a=new Double[N];
??????? for(int i=0;i<N;i++)
??????????? a[i]=StdRandom.uniform(0.0,N*1.0);
??????? //
??????? StdDraw.pause(5000);
??????? sort(a);
??? }
}
轉(zhuǎn)載于:https://www.cnblogs.com/longjin2018/p/9860033.html
總結(jié)
以上是生活随笔為你收集整理的Algs4-2.1.17动画-插入排序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 重磅!阿里巴巴和全球最大奢侈品电商YNA
- 下一篇: jmeter --- 基于InfluxD