Exchange sort-bubble sort (Bubble sort) algorithm principle and Java implementation

Source: Internet
Author: User

Basic idea:

In the set of numbers to be sorted, the total number in the range that is not currently in sequence, the top-down pairs of adjacent two numbers are compared and adjusted sequentially, so that the larger number to sink , smaller upward. That is, each time a comparison of two adjacent numbers finds that they are in the opposite order of order, they are interchanged.

Example of bubbling sort:

Algorithm implementation

1 /**2  *3  * @authorZhangtao4  */5  Public classBubblesort6 {7       Public Static voidMain (string[] args)8     {9         intArr[]={3,1,5,7,2,4,9,6,45,0,-1};Ten Bubblesort (arr); One     } A     //Bubble Sort -    Static voidBubblesort (int[] arr) -    { the        intArrlength=arr.length; -         for(inti=0;i<arrlength-1;i++)//each time the position of an element is resolved, it is necessary to compare ArrLength-1 times -        { -             for(intj=0;j<arrlength-i-1;j++)//each position of an element is resolved, the next element determines the number of comparisons by 1 +            { -                if(arr[j]>arr[j+1]) +                { A                    inttemp=Arr[j]; atArr[j]=arr[j+1]; -arr[j+1]=temp; -                } -            } - PrintLine (arr,i); -        } in    } -     //Print the sort results each time to     Static voidPrintLine (int[] arr,inti) +     { -System.out.println (i+ ":"); the         intArrlength=arr.length; *          for(intj=0;j<arrlength;j++) $         {Panax NotoginsengSystem.out.print (arr[j]+ ""); -         } the System.out.println (); +     } A}

Improvement of bubble sorting algorithm

A common improvement on bubble sorting is to add a symbolic variable exchange, which is used to flag whether there is data exchange in a certain sort of trip, and if there is no data exchange during a certain trip, the data is arranged as required, and the sorting can be finished immediately, avoiding unnecessary comparisons. This article provides the following two improved algorithms:

1. Set up a symbolic variable POS, which records the position of the last interchange in the order of each trip. Since the POS location after the record has been exchanged in place, so in the next sequencing as long as the scan to POS location.

The improved algorithm is as follows :

1 voidBubble_1 (intR[],intN)2 {  3         intI= n-1;//initially, the last position remains the same4          while(i> 0) {   5             intpos= 0;//no record exchange at the beginning of each trip6              for(intj= 0; j< i; J + +)  7                 if(R[j]> r[j+1]) {  8Pos= J;//record the location of the interchange9                     intTMP = R[j]; r[j]=r[j+1];r[j+1]=tmp; Ten                 }    Onei= POS;//prepare for the next sequencing A          }    -}

2. The traditional bubble sort operation can only find a maximum or minimum value , we consider using the method of forward and reverse two-pass bubbling in each order to get two final values at a time (maximum and minimum ), thus reducing the number of sort passes by almost half.

The improved algorithm is implemented as follows:

1 voidBubble_2 (intR[],intN) {  2             intLow = 0; 3             intHigh= n-1;//set the initial value of a variable4             inttmp,j; 5              while(Low <High ) {  6                  for(j= low; j<; ++j)//to bubble up and find the biggest person7                     if(R[j]> r[j+1]) {  8TMP = R[j]; r[j]=r[j+1];r[j+1]=tmp; 9                     }   Ten--high;//Modify the high value, move forward one One                  for(J=high; j>low;--j)//reverse bubble, find the smallest person A                     if(r[j]<r[j-1]) {   -TMP = R[j]; r[j]=r[j-1];r[j-1]=tmp;  -                     }   the++low;//modify the low value and move back one -             }    -}

Exchange sort-bubble sort (Bubble sort) algorithm principle and Java implementation

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.