heap Sort String array - Android java.lang

Android examples for java.lang:array sort

Description

heap Sort String array

Demo Code


import java.io.IOException;
import java.io.Writer;
import java.util.Vector;

public class Main{
    public static void main(String[] argv) throws Exception{
        String as = "java2s.com";
        heapSort(as);//from   ww w  .  j av a2  s .  c o  m
    }
    public static void heapSort(String as[]) {
        heapSort(as, as.length);
    }
    public static void heapSort(String as[], int i) {
        for (int j = i / 2; j >= 0; j--)
            fall(as, i, j);
        for (int k = i - 1; k > 0; k--) {
            String s = as[0];
            as[0] = as[k];
            as[k] = s;
            fall(as, k, 0);
        }
    }
    private static void fall(String as[], int i, int j) {
        int k = 2 * j + 1;
        if (k < i) {
            if (k + 1 < i && as[k].compareTo(as[k + 1]) < 0)
                k = 2 * j + 2;
            if (as[j].compareTo(as[k]) < 0) {
                String s = as[j];
                as[j] = as[k];
                as[k] = s;
                fall(as, i, k);
            }
        }
    }
}

Related Tutorials