zoukankan      html  css  js  c++  java
  • java算法:排序实现

    java算法:排序实现

    排序最基本规则:先比较,再对数据项进行排序。

    例1:数据项接口

    Java代码 复制代码
    1. interface Item{   
    2.     boolean less(Item v);   
    3. }  

    例2:排序方法类

    Java代码 复制代码
    1. class Sort{   
    2.     static boolean less(Item v, Item w){   
    3.         return v.less(w);   
    4.     }   
    5.     static void exch(Item [] a, int i, int j){   
    6.         Item t = a[i];   
    7.         a[i] = a[j];   
    8.         a[j] = t;   
    9.     }   
    10.     static void comExch(Item [] a, int i, int j){   
    11.         if(less(a[j], a[i])){   
    12.             exch(a, i, j);   
    13.     }   
    14.     void sort(Item [] a, int l, int r){   
    15.         example(a, l, r);   
    16.     }   
    17.     static void example(Item [] a, int l, int r){   
    18.         for(int i = l; i < r; i++){   
    19.             for(int j = i; j > l; j--){   
    20.                 compExch(a, j - 1, j);   
    21.             }   
    22.         }   
    23.     }   
    24. }  

    数据项ADT的接口:数据项,或要排序的一般对象;数据项数组。

    例3:数据项ADT接口

    Java代码 复制代码
    1. class MyItem implements Item{   
    2.     public boolean less(Item)   
    3.     void read()   
    4.     void rand()   
    5.     pubic String toString()   
    6. }  

    排序算法不仅对数据项,也对数据项数组起作用。

    例4:可排序的数组ADT接口

    Java代码 复制代码
    1. class SArray{   
    2.     SArray(int)   
    3.     void rand()   
    4.     void read()   
    5.     void show(intint)   
    6.     void sort(intint)   
    7. }  

    例5:可排序数组的排序驱动程序

    Java代码 复制代码
    1. class ArraySort{   
    2.     public static void main(String [] args){   
    3.         int N = 100;   
    4.         SArray sa = new SArray(100);   
    5.         sa.rand();   
    6.         sa.sort(0, N - 1);   
    7.         sa.show(0, N - 1);   
    8.     }   
    9. }  

    该程序表明:可以定义一个这样的计算而不需要涉及要排序的数据类型。

    模块化的组织方式允许用别的实现来代替,这取决于该应用。如,当对大型数组测试排序时,可能会使用只输出部分数组的show的实现。

    例6:可排序数组ADT的样本实现

    Java代码 复制代码
    1. class MyArray{   
    2.     private Item[] a;   
    3.     private int N;   
    4.     MyArray(int N){   
    5.         this.N = N;   
    6.         a = new Item[N];   
    7.         for(int i = 0; i < N; i++){   
    8.             a[i] = new Item();   
    9.         }   
    10.     }   
    11.     void rand(){   
    12.         for(int i = 0; i < N; i++){   
    13.             a[i].rand();   
    14.         }   
    15.     }   
    16.     void read(){   
    17.         for(int i = 0; i < N; i++){   
    18.             if(!In.empty()){   
    19.                 a[i].read();   
    20.             }   
    21.         }   
    22.     }   
    23.     void show(int l, int r){   
    24.         for(int  i = l; i < r; i++){   
    25.             System.out.println(a[i] + " ");   
    26.         }   
    27.     }   
    28.     void sort(int l, int r){   
    29.         Sort.sort(a, l, r);   
    30.     }   
    31. }  

    对不同类型数据项进行排序的ADT实现。

    例7:整数数据项的ADT实现

    Java代码 复制代码
    1. class MyItem implements Item{   
    2.     private int key;   
    3.     public boolean less(Item v){   
    4.         return key < ((Item)v).key;   
    5.     }   
    6.     void read(){   
    7.         key = In.getInt();   
    8.     }   
    9.     void rand(){   
    10.         key = (int)(1000 * Math.random());   
    11.     }   
    12.     public String toString(){   
    13.         return key + "";   
    14.     }   
    15. }  

    例8:样本记录类

    Java代码 复制代码
    1. class Record{   
    2.     int id;   
    3.     double balance;   
    4.     String who;   
    5.     static int SortKeyField = 0;   
    6.     public String toString(){   
    7.         return id + " " + balance + " " + who;   
    8.     }   
    9. }  

    例9:记录项的ADT实现

    Java代码 复制代码
    1. class MyItem extends Record implements Item{   
    2.     public boolean less(Item v){   
    3.         MyItem r = (MyItem) v;   
    4.         switch(SortKeyField){   
    5.             case 2return who.compareTo(r.who) < 0;   
    6.             case 1return balance < r.balance;   
    7.             defaultreturn id < r.id;   
    8.         }   
    9.     }   
    10.     void read(){   
    11.         id = In.getInt();   
    12.         balance = In.getDouble();   
    13.         who = In.getString();   
    14.     }   
    15. }  

    上述方法在经典文献中被称为指针排序。

    例10:字符串数据项的ADT实现

    Java代码 复制代码
    1. class MyItem implements Item{   
    2.     String key;   
    3.     public boolean(Item v){   
    4.         return key.compareTo(((MyItem)v).key) < 0;   
    5.     }   
    6.     void read(){   
    7.         key = In.getString();   
    8.     }   
    9.     void rand(){   
    10.         int a = (int)('a');   
    11.         key = "";   
    12.         for(int i = 0; i < 1 + 9*Math.random(); i++){   
    13.             key += (char)(a + 26*Math.random();   
    14.         }   
    15.     }   
    16.     public String toString(){   
    17.         return key;   
    18.     }   
    19. }  

    说明了在java中对关键字使用String域直接实现String对象的MyItemADT。

    java方法有很多好处。在排序情况下,使用引用避免对排序的数组直接进行操作。即时是只读文件,也能对它进行“排序”。而且,通过使用多重引用数组,能对一个数据体用两种不同的排序表示法来表示。这种不改变数据就能对数据进行操作的灵活性在许多应用中都十分有用的,同时,使用引用能节省移动整个记录的开销。

  • 相关阅读:
    树剖
    codeforces round 589
    codeforces round 590
    code craft 20
    Ozon Tech Challenge 2020 (Div.1 + Div.2)
    codeforces round 625
    Crime HDU
    codeforces 594
    codeforces 596
    python操作mysql方法和常见问题
  • 原文地址:https://www.cnblogs.com/wuyida/p/6301139.html
Copyright © 2011-2022 走看看