Java---Numeric sorting

Source: Internet
Author: User

In Java, there are 4 ways to sort the numbers by using arrays: Choosing the sorting method, bubbling method, fast sorting method and inserting sort method.

Select Sort is the first number in the array as the maximum or minimum number, and then by a circular comparison of the maximum number of exchanges or the minimum number and the first digit in a round comparison of the position of the order;

Bubble sort is also the first number in the array as the largest or smallest number, the loop compares the size of two adjacent numbers, meet the conditions on the interchange position, the maximum number or the minimum number sink;

The fast sorting method is mainly implemented by using the Arrays.sort method () in the Arrays class.

The insertion sort is the selection of data in an array, which is then sorted by the constant insertion comparison.

Select Sort method

 PackageCom.lovo; Public classTest01 { Public Static voidMain (string[] args) {int[] A =New int[5];  for(inti = 0; i < a.length; i++) {A[i]= (int) (Math.random () * 99 + 1); System.out.print (A[i]+ "  "); }        //Simple Selection Sorting         for(inti = 0; i < a.length-1; i++) {            intMinindex =i;  for(intj = i + 1; J < A.length; J + +) {                if(A[j] <A[minindex]) {Minindex=J; }            }            if(Minindex! =i) {inttemp =A[i]; A[i]=A[minindex]; A[minindex]=temp; }        }                 for(intx:a) {System.out.print (x+ "  "); }    }}

Bubble Sort Method

Package Com.lovo;public class Test02 {public static void main (string[] args) {int[] a = new Int[5];for (int i = 0; i < a . length; i++) {A[i] = (int) (Math.random () * 99 + 1); System.out.print (A[i] + "  ");} Bubble sort Boolean swapped = true;//has not occurred interchange for (int i = 1; swapped && i <= a.length-1; i++) {swapped = False;for (int j = 0; J < A.length-i; J + +) {if (A[j] > a[j + 1]) {int temp = A[j];a[j] = a[j + 1];a[j + 1] = Temp;swapped = True;}}} for (int x:a) {System.out.print (x + "  ");}}}

Quick Sort Method

Package Com.lovo;import Java.util.arrays;public class Test03 {//Quick sort public static void main (string[] args) {int[] a = NE W Int[5]; System.out.print ("Before sorting:"); for (int i = 0; i < a.length; i++) {A[i] = (int) (Math.random () * 99 + 1); System.out.print (A[i] + "  ");} System.out.print ("\ n sort after:"); Arrays.sort (a); Sort for (int x:a) {System.out.print (x + "  ");}}}

Java---Numeric sorting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.