Sort an array in Java

后端 未结 17 951
傲寒
傲寒 2020-11-22 04:53

I\'m trying to make a program that consists of an array of 10 integers which all has a random value, so far so good.

However, now I need to sort them in order from l

相关标签:
17条回答
  • 2020-11-22 05:12

    You can sort a int array with Arrays.sort( array ).

    0 讨论(0)
  • 2020-11-22 05:13

    It may help you understand loops by implementing yourself. See Bubble sort is easy to understand:

    public void bubbleSort(int[] array) {
        boolean swapped = true;
        int j = 0;
        int tmp;
        while (swapped) {
            swapped = false;
            j++;
            for (int i = 0; i < array.length - j; i++) {
                if (array[i] > array[i + 1]) {
                    tmp = array[i];
                    array[i] = array[i + 1];
                    array[i + 1] = tmp;
                    swapped = true;
                }
            }
        }
    }
    

    Of course, you should not use it in production as there are better performing algorithms for large lists such as QuickSort or MergeSort which are implemented by Arrays.sort(array)

    0 讨论(0)
  • 2020-11-22 05:13

    MOST EFFECTIVE WAY!

    public static void main(String args[])
    {
        int [] array = new int[10];//creates an array named array to hold 10 int's
        for(int x: array)//for-each loop!
          x = ((int)(Math.random()*100+1));
        Array.sort(array);
        for(int x: array)
          System.out.println(x+" ");
    }
    
    0 讨论(0)
  • 2020-11-22 05:15

    If you want to build the Quick sort algorithm yourself and have more understanding of how it works check the below code :

    1- Create sort class

    class QuickSort {
        private int input[];
        private int length;
    
        public void sort(int[] numbers) {
            if (numbers == null || numbers.length == 0) {
                return;
            }
            this.input = numbers;
            length = numbers.length;
            quickSort(0, length - 1);
        }
        /*
         * This method implements in-place quicksort algorithm recursively.
         */
    
        private void quickSort(int low, int high) {
            int i = low;
            int j = high;
    
            // pivot is middle index
            int pivot = input[low + (high - low) / 2];
    
            // Divide into two arrays
            while (i <= j) {
                /**
                 * As shown in above image, In each iteration, we will identify a
                 * number from left side which is greater then the pivot value, and
                 * a number from right side which is less then the pivot value. Once
                 * search is complete, we can swap both numbers.
                 */
                while (input[i] < pivot) {
                    i++;
                }
                while (input[j] > pivot) {
                    j--;
                }
                if (i <= j) {
                    swap(i, j);
                    // move index to next position on both sides
                    i++;
                    j--;
                }
            }
    
            // calls quickSort() method recursively
            if (low < j) {
                quickSort(low, j);
            }
    
            if (i < high) {
                quickSort(i, high);
            }
        }
    
        private void swap(int i, int j) {
            int temp = input[i];
            input[i] = input[j];
            input[j] = temp;
        }
    }
    

    2- Send your unsorted array to Quicksort class

    import java.util.Arrays;
    
    
    public class QuickSortDemo {
    
        public static void main(String args[]) {
            // unsorted integer array
            int[] unsorted = {6, 5, 3, 1, 8, 7, 2, 4};
            System.out.println("Unsorted array :" + Arrays.toString(unsorted));
            QuickSort algorithm = new QuickSort();
            // sorting integer array using quicksort algorithm
            algorithm.sort(unsorted);
            // printing sorted array
            System.out.println("Sorted array :" + Arrays.toString(unsorted));
        }
    }
    

    3- Output

    Unsorted array :[6, 5, 3, 1, 8, 7, 2, 4] 
    Sorted array :[1, 2, 3, 4, 5, 6, 7, 8]
    
    0 讨论(0)
  • 2020-11-22 05:18
    int[] array = {2, 3, 4, 5, 3, 4, 2, 34, 2, 56, 98, 32, 54};
    
    for (int i = 0; i < array.length; i++) {
        for (int j = 0; j < array.length; j++) {
            if (array[i] < array[j]) {
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 05:18

    just FYI, you can now use Java 8 new API for sorting any type of array using parallelSort

    parallelSort uses Fork/Join framework introduced in Java 7 to assign the sorting tasks to multiple threads available in the thread pool.

    the two methods that can be used to sort int array,

    parallelSort(int[] a)
    parallelSort(int[] a,int fromIndex,int toIndex)
    
    0 讨论(0)
提交回复
热议问题