Four sorting methods of using arrays in Java basic knowledge hardening 12:java

Source: Internet
Author: User

1. Use sort () in the Arrays class in the Javaapi document to quickly sort

First we look directly at the code as follows:

1  PackageHimi.text;2 3 Importjava.util.Arrays;4 5  Public classTestDemo01 {6 7      Public Static voidMain (string[] args) {8         int[] Array = {2,12,3,44,27};9         /**Ten * Quick sorting using sort () from the arrays class in the JAVAAPI document One          */ AArrays.sort (array);//call the static method of array sort sorted in ascending order -          for(intShow:array) { -System.out.print (show+ "");//Ascending Output the         } - System.out.println (); -System.out.println ("---------------"); -          +          for(inti=0; i<array.length-1; i++) -System.out.print (array[array.length-1-i]+ "");//Descending Output +          A          at     } -  -}

The result of the code operation is as follows:

2. Sort by bubbling :

Bubble method Algorithm Idea: If there are n number, you want to do N-1 comparison, in each comparison to do N-1 times 22 comparison. The so-called 22 comparison is that the two adjacent numbers are compared from beginning to end, and the larger number is placed before or after (placed in the front is descending, placed after ascending)

1  PackageHimi.text;2 3  Public classTestDemo02 {4 5      Public Static voidMain (string[] args) {6         int[] arrays = {12, 23, 8, 17, 5 };7 bubblesort (arrays);8          for(intshow:arrays)9System.out.print (show + "");Ten     } One  A      Public Static voidBubblesort (int[] Array) { -         inttemp; -          for(inti = 0; i < array.length-1; i++) { the              for(intj = 0; J < array.length-1-I; J + +) { -                 if(Array[j] > array[j + 1]) { -                     //Mode 1 -                     //temp = array[j]; +                     //array[j]=array[j+1]; -                     //array[j+1]=temp; +                      A                     //Mode 2 atARRAY[J] = Array[j] ^ array[j + 1]; -Array[j + 1] = array[j + 1] ^Array[j]; -ARRAY[J] = Array[j] ^ array[j + 1]; -                 } -             } -         } in     } -  to}

Output Result:

3. Select sort :

First, starting with a single element, scan from One direction, such as left-to-right scan, a[0] as the benchmark, then find the smallest element from a[0]....a[9] and swap it with a[0] . Then move its datum position to the right one bit, repeat the above action, for example, a[1] as the benchmark, find the smallest in a[1]~a[9] , and exchange it with a[1] . Continues until the sort ends when the datum position is moved to the last element of the array.

1  PackageHimi.text;2 3  Public classTestDemo03 {4 5      Public Static voidMain (string[] args) {6         int[] Arrays = {12,23,8,17,5};7         int[] temp =selectsort (arrays);8          for(intshow:temp)9System.out.print (show+ "");Ten     } One      A      -      Public Static int[] Selectsort (int[] args) {//Select sorting Algorithm -          for(inti=0;i<args.length-1; i++ ){    the                intmin=i;  -                 for(intJ=i+1;j<args.length; j + + ){    -                      if(args[min]>Args[j]) {    -min=J;  +                           }    -                  }    +                      if(min!=i) {   A                      inttemp=Args[i];  atargs[i]=Args[min];  -args[min]=temp;  -                  }   -            }   -             returnargs;  -    }  in  -}

The results of the operation are as follows:

4. Insert sorting algorithm :

The so-called insertion sorting method, is to check the number I, if the number on its left is larger than it, to exchange, this action continues until the number of the left of the number is smaller than it, it can be stopped. Insert Sort method The main loop has two variables: I and J, each time the loop is executed, the number I will be placed in the appropriate position on the left.

1  PackageHimi.text;2 3  Public classTestDemo04 {4 5      Public Static voidMain (string[] args) {6         int[] Arrays = {12,23,8,17,5};7         int[] temp =insertsort (arrays);8          for(intshow:temp)9System.out.print (show+ "");Ten     }     One      Public Static int[] Insertsort (int[] args) {//Insert Sort Algorithm A          for(inti=1;i<args.length;i++){    -                  for(intj=i;j>0;j--){    -                         if(args[j]<args[j-1]){    the                                 intTemp=args[j-1];  -args[j-1]=Args[j];  -args[j]=temp;  -}Else  Break;  +                }   -         }   +         returnargs;  A }   at  -}

Operation Result:

Four sorting methods of using arrays in Java basic knowledge hardening 12:java

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.