Example usage for java.util Arrays sort

List of usage examples for java.util Arrays sort

Introduction

In this page you can find the example usage for java.util Arrays sort.

Prototype

public static void sort(Object[] a) 

Source Link

Document

Sorts the specified array of objects into ascending order, according to the Comparable natural ordering of its elements.

Usage

From source file:Main.java

public static void main(String[] args) {
    int[] i1 = new int[] { 2, 2, 5, 4, 1 };
    for (int i : i1) {
        System.out.print(" " + i);
    }/*from  w ww .ja v  a 2 s . c  o m*/
    Arrays.sort(i1);
    for (int i : i1) {
        System.out.print(" " + i);
    }

    int[] i2 = new int[] { 51, 21, 31, 11, 41 };

    Arrays.sort(i2, 1, 4);
    for (int i : i2) {
        System.out.print(" " + i);
    }
}

From source file:SortArray.java

public static void main(String[] args) {
    int[] a = new int[10];
    for (int i = 0; i < a.length; i++) {
        a[i] = (int) (Math.random() * 100);
    }//from ww w .java  2  s. co m
    Arrays.sort(a);
    System.out.println("Sorted array!");
    for (int i = 0; i < a.length; i++)
        System.out.println(a[i]);
}

From source file:Main.java

public static void main(String[] args) {
    byte[] b1 = new byte[] { 3, 2, 5, 4, 1 };
    for (byte b : b1) {
        System.out.println(b);/*from ww w  . j a v a  2  s  .  c o m*/
    }

    Arrays.sort(b1);
    for (byte b : b1) {
        System.out.println(b);
    }

    byte[] b2 = new byte[] { 5, 2, 3, 1, 4 };
    Arrays.sort(b2, 1, 4);

    for (byte b : b2) {
        System.out.println(b);
    }
}

From source file:Main.java

  public static void main(String[] args) {
  byte bArray[] = { 1, 2, 4, 5 };
  Arrays.sort(bArray);

  byte searchValue = 2;

  int intResult = Arrays.binarySearch(bArray, searchValue);
  System.out.println("Result of binary search of 2 is : " + intResult);

  searchValue = 7;// w w w .  ja va2s  .co m
  intResult = Arrays.binarySearch(bArray, searchValue);
  System.out.println("Result of binary search of 3 is : " + intResult);
}

From source file:Main.java

public static void main(String[] args) {
    int[] unsorted = { -3, 10, -4, 11, 5, 1, 2 };
    System.out.println(Arrays.toString(unsorted));
    int[] sorted = unsorted;
    Arrays.sort(sorted);
    System.out.println(Arrays.toString(sorted));
    int breakingPoint = Arrays.binarySearch(sorted, 5);
    for (int i = breakingPoint; i < sorted.length; i++) {
        System.out.println(sorted[i]);
    }//w  ww .j a  va  2  s.  co m
}

From source file:Main.java

public static void main(String[] args) {
    short[] s1 = new short[] { 31, 21, 51, 41, 11 };
    for (short s : s1) {
        System.out.print(" " + s);
    }//  w w  w.  j  a v  a2  s  .  co m
    Arrays.sort(s1);
    for (short s : s1) {
        System.out.print(" " + s);
    }

    short[] s2 = new short[] { 5, 2, 3, 1, 4 };
    Arrays.sort(s2, 1, 4);

    for (short s : s2) {
        System.out.print(" " + s);
    }
}

From source file:Main.java

public static void main(String[] args) {
    char[] c1 = new char[] { 'c', 'h', 'a', 'r', 's' };
    for (char ch : c1) {
        System.out.print(ch);//from   ww  w . j  a v a 2s.  co m
    }
    Arrays.sort(c1);

    for (char ch : c1) {
        System.out.print(ch);
    }

    char[] c2 = new char[] { 'c', 'h', 'a', 'r', 's' };
    Arrays.sort(c2, 1, 4);
    for (char ch : c1) {
        System.out.print(ch);
    }
}

From source file:Main.java

public static void main(String[] args) {
    long[] l1 = new long[] { 3L, 2L, 5L, 4L, 1L };
    for (long l : l1) {
        System.out.print(" " + l);
    }/*from w ww.j  a  va  2 s .  c o  m*/
    Arrays.sort(l1);

    for (long l : l1) {
        System.out.print(" " + l);
    }

    long[] l2 = new long[] { 5, 2, 3, 1, 4 };

    Arrays.sort(l2, 1, 4);

    for (long l : l2) {
        System.out.print(" " + l);
    }
}

From source file:SearchTestDemo.java

public static void main(String args[]) throws Exception {
    int array[] = { 2, 5, -2, 6, -3, 8, 0, -7, -9, 4 };

    // Ensure array sorted
    Arrays.sort(array);
    printArray("Sorted array", array);

    // Search for element in array
    int index = Arrays.binarySearch(array, 2);
    System.out.println("Found 2 @ " + index);

    // Search for element not in array
    index = Arrays.binarySearch(array, 1);
    System.out.println("Didn't find 1 @ " + index);

    // Insert/*ww  w . j a v  a  2  s .  c  o m*/
    int newIndex = -index - 1;
    array = insertElement(array, 1, newIndex);
    printArray("With 1 added", array);

}

From source file:Main.java

public static void main(String[] args) {
    double[] d1 = new double[] { 3.1, 2.1, 5.1, 4.1, 1.1 };
    for (double d : d1) {
        System.out.print(" " + d);
    }/*from  w w w . j  a  v  a  2s. c  o  m*/
    Arrays.sort(d1);

    for (double d : d1) {
        System.out.print(" " + d);
    }

    double[] d2 = new double[] { 5, 2, 3, 1, 4 };
    Arrays.sort(d2, 1, 4);
    for (double d : d2) {
        System.out.print(" " + d);
    }
}