Java Array Sort sort(Object[] array, Comparator comparator)

Here you can find the source of sort(Object[] array, Comparator comparator)

Description

sort

License

Open Source License

Declaration

public static void sort(Object[] array, Comparator comparator) 

Method Source Code

//package com.java2s;
/**/*from   w  ww .  ja  v a2  s  .  c  o m*/
  *         Commmon Internet File System Java API (JCIFS)
  *----------------------------------------------------------------
  *  Copyright (C) 1999  Norbert Hranitzky
  *
  *  This program is free software; you can redistribute it and/or
  *  modify it under the terms of the GNU General Public License as
  *  published by the Free Software Foundation; either version 2 of
  *  the License, or (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  *  General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  *
  *  The full copyright text: http://www.gnu.org/copyleft/gpl.html
  *
  *----------------------------------------------------------------
  *  Author: Norbert Hranitzky
  *  Email : norbert.hranitzky@mchp.siemens.de
  *  Web   : http://www.hranitzky.purespace.de
  */

import java.util.*;

public class Main {
    public static void sort(Object[] array, Comparator comparator) {
        sort(array, array.length, comparator);
    }

    /**
     * Sorts array (Heapsort)
     */
    public static void sort(Object[] array, int count, Comparator comparator) {

        int i, top, t, largest, l, r, here;
        Object temp;

        int elementCount = count;

        if (elementCount <= 1) {
            return;
        }

        top = elementCount - 1;
        t = elementCount / 2;

        do {
            --t;
            largest = t;

            /* heapify */

            do {
                i = largest;
                l = left(largest);
                r = right(largest);

                if (l <= top) {

                    if (comparator.compare(array[l], array[i]) > 0)
                        largest = l;

                }
                if (r <= top) {

                    if (comparator.compare(array[r], array[l]) > 0)
                        largest = r;

                }
                if (largest != i) {
                    temp = array[largest];
                    array[largest] = array[i];
                    array[i] = temp;
                }
            } while (largest != i);
        } while (t > 0);

        t = elementCount;

        do {
            --top;
            --t;

            here = t;

            temp = array[here];
            array[here] = array[0];
            array[0] = temp;

            largest = 0;

            do {
                i = largest;
                l = left(largest);
                r = right(largest);

                if (l <= top) {

                    if (comparator.compare(array[l], array[i]) > 0)
                        largest = l;

                }
                if (r <= top) {

                    if (comparator.compare(array[r], array[largest]) > 0)
                        largest = r;

                }
                if (largest != i) {
                    temp = array[largest];
                    array[largest] = array[i];
                    array[i] = temp;
                }
            } while (largest != i);

        } while (t > 1);

    }

    private static int left(int i) {
        return 2 * i + 1;
    }

    private static int right(int i) {
        return 2 * i + 2;
    }
}

Related

  1. sort(Number[] array)
  2. sort(O[] array)
  3. sort(Object[] a)
  4. sort(Object[] arr, int start, int end)
  5. sort(Object[] array)
  6. sort(Object[] array, int start, int end)
  7. sort(Object[] array_, boolean accendingOrder_)
  8. sort(String a[])
  9. sort(String[] colnos)