zoukankan      html  css  js  c++  java
  • 各排序算法及其比较

    heap:// worst: O(n*lgn)

    public class heap {
    public static void sort(int[]a){
    int b[] = copy(a);
    int N = b.length-1;
    for(int k = N/2;k > 0;k--){
    sink(b,k,N);
    }
    while(N > 1){
    exch(b,1,N--);
    sink(b,1,N);
    }
    //for(int i = 1;i < b.length; i++){
    //System.out.println(b[i] + " ");

    // }


    }

    public static int[] copy(int[] a){
    int n = a.length;
    int[] copy = new int[a.length + 1];
    for(int i = n;i>0;i--){
    copy[i] = a[i-1];
    }
    copy[0] = -1;
    return copy;
    }

    private static void sink(int[]a,int k,int N){
    while (2*k <= N){
    int j = 2*k;
    if(j < N && a[j] < a[j+1]){j++;}
    if(a[j] > a[k]){
    exch(a,k,j);}
    else{break;}
    k = j;
    }
    }
    public static void exch(int[] a,int i,int j)
    { int t = a[i]; a[i] = a[j] ; a[j] = t;}
    }

    ------------------------------------------------------------------------------------------------

    Insertion:

    public static void sort(int[] a) {
    int n = a.length;
    int[] b = copy(a);
    for (int i = 0; i < n; i++) {
    for (int j = i; j > 0 && less(b[j], b[j-1]); j--) {
    exch(b, j, j-1);
    }
    //assert isSorted(a, 0, i);
    }
    // for(int i = 0;i < a.length; i++)
    //System.out.println(b[i] + " ");
    //assert isSorted(a);
    }
    private static boolean less(int v,int w)
    { return v-w < 0; }

    private static void exch(int[] a,int i,int j)
    { int t = a[i]; a[i] = a[j] ; a[j] = t;}

    public static boolean isSorted(int[] a)
    {
    for(int i = 1; i < a.length; i++)
    if(less(a[i],a[i-1])) return false;
    return true;
    }



    private static int[] copy(int[] a){
    int n = a.length;
    int[] copy = new int[a.length];
    for(int i = n;i>0;i--){
    copy[i-1] = a[i-1];
    }
    return copy;
    }
    /* public static void main(String[] args)
    {
    String[] a = args;
    sort(a);
    assert isSorted(a);
    show(a);

    }*/

    }

    -------------------------------------------------------------------------------------------------------

    quicksort:  worst:O-(n^2)   Aver:O-(n*lgn)(expected)

    public static void sort(int[] a) {
    int n = a.length;
    int[] b = merge.copy(a);
    qs(b,0,n-1);
    //for(int i = 0;i < b.length; i++){
    //System.out.println(b[i] + " ");

    // }
    //assert isSorted(a);
    }
    //}


    public static void qs(int[] a,int lo,int hi){
    int mid = 0;
    if(lo < hi){
    mid = partition(a,lo,hi);
    qs(a,lo,mid-1);
    qs(a,mid+1,hi);
    }
    }
    public static int partition(int[] a,int lo,int hi){
    int key = a[hi];
    int i = lo -1;
    for(int j = lo;j <= hi -1;j++){
    if(a[j] < key){
    i = i+1;
    heap.exch(a,i,j);
    }
    }
    heap.exch(a,i+1,hi);
    return i+1;

    }

    }

    --------------------------------------------------------------------------------------------------

     binarytree: //same as quick

    public class binarytree {
    ///private Node root = null;
    private static class Node{
    private int val;
    private Node left,right;
    private int N; //the number of the all nodes of its child nodes and itself
    //private int num;//the number
    public Node(int val,int N){
    this.val = val; this.N = N;
    }

    }

    public static void sort(int[] a) {
    int n = a.length;
    Node root = null;
    for(int i = n-1;i >= 0;i-- ){
    root = put(root,a[i]);
    }
    int[] b = new int[n];
    for(int i = n-1;i >= 0;i-- ){
    Node x = select(root,i);
    b[i] = x.val;
    }
    // for(int i = 0;i < b.length; i++){
    //System.out.println(b[i] + " ");

    // }

    }

    private static Node put(Node x,int val){
    if(x == null) return new Node(val,1);
    if(val < x.val) x.left = put(x.left,val);
    else if(val > x.val) x.right = put(x.right,val);
    else x.val = val;
    x.N = size(x.left) + size(x.right) + 1;
    return x;
    }
    private static int size(Node x){
    if(x == null){return 0;}
    else return x.N;
    }



    private static Node select(Node x,int k){
    if(x == null) return null;
    int t = size(x.left);
    if (t>k) return select(x.left,k);
    else if(t<k) return select (x.right,k-t-1);
    else return x;
    }



    }

    -------------------------------------------------------------------------------------------------------------

    merge:  //worst:O-(nlgn)    Aver:O-(n*lgn)

    public static void sort(int[] a) {
    int n = a.length;
    int[] b = copy(a);
    for (int sz = 1; sz < n; sz = 2*sz) {
    for (int lo = 0; lo < n-sz; lo = lo +2*sz) {
    merge1(b,lo,lo + sz - 1,Math.min(lo +2*sz-1, n-1));
    }



    //assert isSorted(a, 0, i);
    }
    // for(int i = 0;i < b.length; i++){
    //System.out.println(b[i] + " ");

    // }
    //assert isSorted(a);
    }

    public static void merge1(int[] a, int lo, int mid, int high){
    int[] aux = new int[a.length];
    int i = lo,j = mid +1;
    for(int k = lo; k <= high ; k++)
    {
    aux[k] = a[k];
    }
    for(int k = lo; k <= high ; k++)
    {
    if(i >= mid + 1)
    {
    a[k] = aux[j++];
    }
    else if(j > high)
    {
    a[k] = aux[i++];
    }

    else if(aux[i] < aux[j])
    {
    a[k] = aux[i++];
    }
    else
    {
    a[k] = aux[j++];
    }
    }

    }

    public static int[] copy(int[] a){
    int n = a.length;
    int[] copy = new int[a.length ];
    for(int i = n-1;i>=0;i--){
    copy[i] = a[i];
    }
    return copy;
    }


    /* public static void main(String[] args)
    {
    //String[] a = null;
    sort(args);
    //assert isSorted(a);
    //show(a);
    }*/
    }

    --------------------------------------------------------------------------------------------------------------------

    comparison:  

    public class mysort {


    public static void main(String[] args){
    int number = 50000;
    int[] array = new int[number] ;
    boolean flag = true;
    Random random = new Random();
    FileWriter writer = null;


    try {

    writer = new FileWriter("D:\input.txt", false);
    for(int i = number;i > 0 ;i--){
    int r = random.nextInt(100000);
    flag = true;
    for(int k = number-i -1;k >=0;k--){
    if(r == array[k]){ flag = false; i++;}
    }
    if(flag == true){ array[number-i] = r;
    String s = Integer.toString(r);
    //System.out.println(s);
    writer.write(s + " "); }
    //if(i % 5 == 0){
    //writer.write(" ");
    //}
    }
    } catch (IOException e) {
    System.out.println("no1");
    } finally {
    try {
    if(writer != null){
    writer.close();
    }
    } catch (IOException e) {
    System.out.println("no2");
    }
    }
    long startTime = System.currentTimeMillis();

    Insertion.sort(array);

    long endTime = System.currentTimeMillis();

    System.out.println("Insertion " + (endTime - startTime)+ "ms");


    startTime = System.currentTimeMillis();

    heap.sort(array);

    endTime = System.currentTimeMillis();

    System.out.println("heap " + (endTime - startTime) + "ms");

    startTime = System.currentTimeMillis();

    merge.sort(array);

    endTime = System.currentTimeMillis();



    System.out.println("merge " + (endTime - startTime) + "ms");


    startTime = System.currentTimeMillis();

    quicksort.sort(array);

    endTime = System.currentTimeMillis();

    System.out.println("quicksort " + (endTime - startTime) + "ms");


    startTime = System.currentTimeMillis();

    binarytree.sort(array);

    endTime = System.currentTimeMillis();

    System.out.println("binarytree " + (endTime - startTime) + "ms");

    }
    }

    --------------------------------------------------------------------------------------------------------------------------------

    report:

    100 int type number from 0 to 500
    Insertion 2ms
    heap 1ms
    merge 1ms
    quicksort 1ms
    binarytree 3ms

    1000 int type number from 0 to 5000
    Insertion 10ms
    heap 1.5ms
    merge 2.5ms
    quicksort 2ms
    binarytree 4ms

    10000 int type number from 0 to 50000
    Insertion 73ms
    heap 4ms
    merge 103ms
    quicksort 4ms
    binarytree 12ms

    50000 int type number from 0 to 100000
    Insertion 1769ms
    heap 15ms
    merge 1233ms
    quicksort 12ms //not stable(1x-8x)
    binarytree 33ms

  • 相关阅读:
    陪伴
    休假
    I'm back
    Mysql程序
    Mysql 行转列
    Struts2 设置--Myelipse
    struts2 严重: Error filterStart 原因
    廉价药
    在linux下用tomcat部署java web项目的过程与注意事项
    创建表格式
  • 原文地址:https://www.cnblogs.com/wujunde/p/6938639.html
Copyright © 2011-2022 走看看