Three sorts of sorting methods implemented in Java

Source: Internet
Author: User

Three ways to sort several numbers: bubble sort, select sort, call library function in Java

1, bubble sort: As the name implies, is to take the small number step by step, the big number one by one to sink, the following is their own written on the understanding of the bubble sort, the word ugly will look at it.

2. Select sort: Take small to large as an example, define a variable min_inter, used to record the lowest value of the subscript, find the smallest number placed in the No. 0 place, the second small placed in the 1th place, followed by.

3. Direct call: Arrays.sort (num3); import java.util.Arrays required;

Here's the code:

1 Importjava.util.Arrays;2 3 /**4 * Sorting method, bubbling, selection5 * from small to large6 *@author: Archer-lcy7 * @date: January 16, 2018 PM 12:07:098 */9  Public classSortway {Ten      Public Static voidMain (string[] args) { One         Final intn=50000;//Constants A         int[] num1=New int[N]; -         int[] num2=New int[N]; -         int[] num3=New int[N]; the          for(inti=0;i<num1.length;i++) { -Num1[i]= (int) (Math.random () *n*10);//randomly generate n number, for comparison time efficiency, n in this first fetch 50000 -num2[i]=Num1[i]; -num3[i]=Num1[i]; +         } -         LongStar_time=system.currenttimemillis ();//Calculate start Time +         //method One: Bubbling A          for(inti=0;i<num1.length;i++) {//Bubble Sort at              for(intj=0;j<num1.length-i-1;j++) { -                 if(num1[j]>num1[j+1]) { -                     intTEMP=NUM1[J];//in Java, you can temporarily define a variable, if the previous number is larger than the two-digit exchange position -Num1[j]=num1[j+1]; -num1[j+1]=temp; -                 } in             } -         } to         LongEnd_time=System.currenttimemillis (); +System.out.println ("Bubble sort Time:" + (End_time-star_time) + "millisecond");//Calculate end time, note end_time-star_time must add (), otherwise wrong -          the         //method Two: Select *         LongStar_time2=System.currenttimemillis (); $                  for(inti=0;i<num1.length-1;i++) {//Select SortPanax Notoginseng                     intMin_inter=i;//record the minimum number of subscripts in this group comparison -                      for(intj=i+1;j<num1.length;j++) { the                         if(num1[i]>Num1[j]) { +Min_inter=J; A                         } the                     } +                     //find the smallest number and put the No. 0 one, the second one in the 1th, followed by -                     inttemp=Num2[i]; $num2[i]=Num2[min_inter]; $num2[min_inter]=Num2[i]; -                 } -         LongEnd_time2=System.currenttimemillis (); theSystem.out.println ("Select sort time:" + (end_time2-star_time2) + "milliseconds"); -         //method Three: Calling the functions in the Java library is also the fastestWuyi         Longstar_time3=System.currenttimemillis (); theArrays.sort (NUM3);//one sentence to solve, need to import imports java.util.Arrays; -         Longend_time3=System.currenttimemillis (); WuSystem.out.println ("Arrays.sort () Time:" + (End_time3-star_time3) + "milliseconds"); -          About          $ //        //output, compare three sorting time efficiency for the calculation, first do not print - //for (int i=0;i<num3.length;i++) { - //if (i%10==0) - //System.out.println (); A //Else + //System.out.print ("\ t"); the //System.out.print (Num3[i]);  - //        } $          the          the     } the}

Here are the three sorting algorithms that are time consuming:

From the running results, the bubble sort takes the longest time, followed by the selection sort, bubble sort and select sort, the bubble sort time is longer mainly because the bubble sort exchanges more times, the time is naturally longer.

Three sorts of sorting methods implemented in 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.