Two-way bubble sorting algorithm--java

Source: Internet
Author: User

An example of an ascending integer sequence is the process of two-way bubbling: First move the maximum number from the back to the last, and then move the smallest number from the back to the front of the array, which is the first round, then repeat the process, and eventually the entire array is arranged from small to large. The two-way bubble sort is slightly better than the traditional bubble sort, because the two-way sort is sorted at both ends of the array, we only need to deal with the middle part of the array, while the one-way, the traditional bubble sort only the tail of the elements are ordered, then each round of processing needs to be processed from the beginning to the first element of the While it has improved in efficiency, it does not significantly improve the efficiency of its sequencing, as determined by the basic process of bubble sequencing. Based on this, the following code can be implemented to sort the odd even numbers separately

Bidirectional bubble Sort Source code:

1  PackageCom.zc.manythread;2 3 ImportJava.util.Random;4 5 /**6 * Bidirectional bubble sort7  * @authoreven my yes8  *9  */Ten  Public classBbsort { One  A      -     //two-way bubbling algorithm, which greatly reduces the number of cyclic sorting -      Public int[] Sort (int[] a)throwsexception{ the         intJ; -         intlimit=a.length; -         intSt=-1; -          while(st<limit) { +              //You must assign a value to St and limit, otherwise if the array is ordered from the beginning -st++; +limit--; A             BooleanSwapped=false; at             //first loop puts the maximum value at the end -              for(j = St; J < Limit; J + +) { -                  if(a[j]>a[j+1]) { -                     intt=A[j]; -A[j]=a[j+1]; -a[j+1]=T; inSwapped=true; -                 } to             } +              -             if(!swapped) { the                 returnA; *}Else { $Swapped=false;Panax Notoginseng                 //The second loop places the smallest value at the beginning -                  for(j = limit;--j>=St;) { the                     if(a[j]>a[j+1]){ +                         intt=A[j]; AA[j]=a[j+1]; thea[j+1]=T; +Swapped=true; -                     } $                 } $                 if(!swapped) { -                     returnA; -                 } the             } -         }Wuyi         returnA; the     } -      Wu     Private Static int[] CreateDate (intcount) { -         /** About * No repeating array $          */ -          -         int[] Data=New int[Count]; -Random Rand =NewRandom (); A           Boolean[] bool =New Boolean[100]; +           intnum = 0; the            for(inti = 0; I < count; i++) { -             Do { $             //if the resulting number is the same, continue the loop thenum = rand.nextint (100); the} while(Bool[num]); theBool[num] =true; the         /*list.add (num);*///List Lists -data[i]=num; in           } the           returndata; the     } About  Public Static voidMain (string[] args) { the     Final intcount=10; the     int[] Data=CreateDate (count); the      for(intn:data) { +System.out.print (n+ "\ T"); -         } the System.out.println ();BayiBsrot bsrot=NewBsrot (data); the     Try { the          -     int[] A=bsrot.sort (data); -      for(intn:a) { theSystem.out.print (n+ "\ T"); the         } the  the}Catch(Exception e) { -         //TODO auto-generated Catch block the e.printstacktrace (); the     } the     94          the } the}

Operation Result:

Two-way bubble sorting algorithm--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.