Java Array Sort sort(Object[] arr, int start, int end)

Here you can find the source of sort(Object[] arr, int start, int end)

Description

sort

License

Open Source License

Declaration

public static void sort(Object[] arr, int start, int end) 

Method Source Code

//package com.java2s;
/* Copyright (c) 2002,2003, Stefan Haustein, Oberhausen, Rhld., Germany
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The  above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE. *//*  w w w.  j  ava 2s.c o m*/

public class Main {
    public static void sort(Object[] arr, int start, int end) {
        //     System.err.println("sorting: ["+start+", "+end+"[");

        if (end - start <= 2) {
            if (end - start == 2 && arr[start].toString().compareTo(arr[start + 1].toString()) > 0) {
                Object tmp = arr[start];
                arr[start] = arr[start + 1];
                arr[start + 1] = tmp;
            }
            //        System.err.println("sorted to "+arr[start]+" < "+arr[start+1]);

            return;
        }

        if (end - start == 3) {
            sort(arr, start, start + 2);
            sort(arr, start + 1, start + 3);
            sort(arr, start, start + 2);

            return;
        }

        int middle = (start + end) / 2;

        sort(arr, start, middle);
        sort(arr, middle, end);

        Object[] tmp = new Object[end - start];

        int i0 = start;
        int i1 = middle;

        //     System.err.println("merging ["+start+", "+middle+", "+end+"[ of "+arr.length);
        for (int i = 0; i < tmp.length; i++) {
            //        System.err.println("i: "+i+" i0: "+i0+" i1: "+i1);
            if (i0 == middle) {
                tmp[i] = arr[i1++];
            } else if (i1 == end || arr[i0].toString().compareTo(arr[i1].toString()) < 0) {
                tmp[i] = arr[i0++];
            } else {
                tmp[i] = arr[i1++];
            }

        }
        System.arraycopy(tmp, 0, arr, start, tmp.length);
    }
}

Related

  1. sort(int[] keys, int[] values)
  2. sort(int[] values)
  3. sort(Number[] array)
  4. sort(O[] array)
  5. sort(Object[] a)
  6. sort(Object[] array)
  7. sort(Object[] array, Comparator comparator)
  8. sort(Object[] array, int start, int end)
  9. sort(Object[] array_, boolean accendingOrder_)