Java Array Sort sort(int[] array)

Here you can find the source of sort(int[] array)

Description

sort

License

Open Source License

Declaration

public static void sort(int[] array) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static void sort(int[] array) {
        privateSort(array, 0, array.length - 1);
    }/*ww w. j  ava  2 s.c om*/

    public static void sort(int[] array, int leftBorder, int rightBorder) {
        boundsTest(array.length, leftBorder, rightBorder);
        privateSort(array, leftBorder, rightBorder);
    }

    private static void privateSort(int[] array, int leftBorder, int rightBorder) {
        if (leftBorder >= rightBorder) {
            return;
        }
        int start = leftBorder;
        int end = rightBorder;
        int currentPosition = start + (end - start) / 2;
        while (start < end) {
            while ((start < currentPosition) && (array[start] <= array[currentPosition])) {
                start++;
            }
            while ((end > currentPosition) && (array[currentPosition] <= array[end])) {
                end--;
            }
            if (start < end) {
                int temp = array[start];
                array[start] = array[end];
                array[end] = temp;
                if (start == currentPosition) {
                    currentPosition = end;
                } else if (end == currentPosition) {
                    currentPosition = start;
                }
            }
        }
        privateSort(array, leftBorder, currentPosition);
        privateSort(array, currentPosition + 1, rightBorder);
    }

    private static void boundsTest(int arrayLength, int leftBorder, int rightBorder) {
        if (leftBorder < 0) {
            throw new ArrayIndexOutOfBoundsException("leftBorder index < 0: " + leftBorder);
        }
        if (rightBorder >= arrayLength) {
            throw new ArrayIndexOutOfBoundsException(
                    "rightBorder index > number of arrays elements : " + rightBorder);
        }
        if (leftBorder > rightBorder) {
            throw new IllegalArgumentException("leftBorder > rightBorder which is illegal!");
        }
    }
}

Related

  1. sort(final long[] primary, final double[] secondary)
  2. Sort(float cais[])
  3. sort(int v[], int left, int right)
  4. sort(int values[])
  5. sort(int[] array)
  6. sort(int[] array)
  7. sort(int[] array)
  8. sort(int[] array)
  9. sort(int[] array)