Algorithm for bubbling Sorting

Source: Internet
Author: User
Tags array length

Thought (Ascending):

1. Compare the size of the two adjacent data, guarantee the large value in the back, and finally, after a sort of sorting will select the maximum value of this number of groups, placed in the last one;

2. In the exclusion of the last maximum array, select one of the maximum values to be placed in the final one;

3. The sorting is done until the array length-1 maximum is excluded (selected);

For example: The array is {10, 34, 300, 56, 6, 56, 7, 87, 5} with an array length of 9;

  

 Public classFind_em { Public Static voidMain (string[] args) {intarr[]={10,34,300,56,6,56,7,87,5}; System.out.println ("Raw Data:");  for(inti=0;i<arr.length;i++) {System.out.print (Arr[i]+ "\ T"); } System.out.println ("\ n Sort (ascending):"); //Bubble Sort         for(intj=1;j<arr.length;j++{ //control the number of cycles, that is, select a few maximum values and then stop  for(inti=0;i<arr.length-j;i++) { //Exchange data, select Maximum Value if(Arr[i]>Arr[i+1]) { //If it is descending, just change it to < intT=arr[i+1]; Arr[i+1]=Arr[i]; Arr[i]=T; }            }             for(inti=0;i<arr.length;i++) {System.out.print (Arr[i]+ "\ T"); } System.out.println ("Select the maximum value:" +arr[arr.length-J]); } System.out.println ("Sorted Data:");  for(inti=0;i<arr.length;i++) {System.out.print (Arr[i]+ "\ T"); }            }}

Results:

Raw DATA:10 34 300 56 6 56 7 87 5sort (Ascending):10 34 56 6 56 7 87 5 -Select the maximum value: 30010 34 6 56 7 56 5 AboutSelect the maximum value: 8710 6 34 7 56 5 AboutSelect the maximum value: 566 10 7 34 5 AboutSelect the maximum value: 566 7 10 5 theSelect the maximum value: 346 7 5TenSelect the maximum value: 106 57-TenSelect the maximum value: 756 7Select the maximum value: 6sorted Data:5 6 7 10 34 56 56 87 300

Comparison number: 36
Number of exchanges: 20

Algorithm for bubbling 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.