Java basic algorithm Select sort and bubble sort

Source: Internet
Author: User
Tags rounds

1. Bubble sort

Bubble sort (Bubble sort) is one of the most classic and simplest sorting algorithms.

Principle: Compare two adjacent elements, Exchange large value elements to the right (descending opposite).

Step: Compare adjacent elements. If the first one is bigger than the second one, swap them both. Do the same for each pair of adjacent elements, starting with the last pair from the first pair to the end. When this is done, the final element will be the maximum number.

Repeat the above steps for all elements, except for the last one. Repeat the above steps each time for fewer elements, until there are no pairs of numbers to compare.

Package First;import Java.util.scanner;public class Test1 {public static void main (String args[]) {Scanner input=new Scann ER (system.in); System.out.print ("Please enter any 10 numbers"), int[] nums=new int[10];for (int i=1;i<11;i++) {System.out.println ("input" +i+ "number"); Nums[i-1]=input.nextint ();} System.out.println ("Start sorting =============================="), int temp;//control how many rounds, altogether 10 numbers so just compare nine rounds for (int i=0;i< nums.length-1;i++) {//control how many times each round is compared, and the number of comparisons is reduced once for (int j=0;j<nums.length-1-i;j++) {if (nums[j]>nums[j+1]) {temp=nums[j];nums[j]=nums[j+1];nums[j+1]=temp;}}} for (int n=0;n<nums.length;n++) {System.out.print (nums[n]+ "");}}}

Results:

2. Select sort

Select Sort is a simple and intuitive sorting algorithm with the following basic principles: for a given set of records, the first comparison is followed by the smallest record, and then the position of the record is exchanged with the position of the first record, and the first record is not included.

The other records make a second comparison, get the smallest record and exchange it with the second position; Repeat the process, knowing that there is only one remaining record for the comparison. The performance of simple selection sorting is better than the bubble sort.

public class Test2 {public static void main (String args[]) {int[] nums={12,8,11,6,4,2,15,3,7,5};for (int i=0;i< nums.length-1;i++) {int temp=nums[i];int flag=i;for (int j=i+1;j<nums.length;j++) {//NUMS[J] < temp sort from small to large; nums[j ] > Temp from large to small sort if (Temp>nums[j]) {temp=nums[j];//assigns the subscript of this element to flag if there is less than the current minimum value, because we need to know where the nums[i] has replaced the element in the position after the swap location. J is just a local variable, and we want to assign it to the global variable flag=j; }}if (flag!=i) {nums[flag]=nums[i];nums[i]=temp;}} for (int n=0;n<nums.length;n++) {System.out.print (nums[n]+ "");}}}

Output Result:

  

Java basic algorithm Select sort and bubble sort

Related Article

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.