Bubble sort and cocktail ordering code analysis

Source: Internet
Author: User

Bubble sort

  Bubble sort (buble sort) is a sort of getting started sorting algorithm. As the name implies, it is sorted by bubbling the largest (or smallest) number in turn.

As shown, the white part is the array to be sorted, the red part is the "larger" number that has been found, and each iteration simply finds the largest number from the white part until the n-1 "larger" number is found, and the array is sorted.

Note: Find n-1 a "larger" number, because the last one must be the smallest number.

Code:

vars = [8, 7, 6, 5, 4, 3, 2, 1];varBublesort =function(array) {vartemp;  for(vari=0; i < array.length-1; i++{//Outer loop 7 times for(varj=0; J < array.length-i-1; J + +{//I have a larger number being bubbled and the number of comparisons is N-i-1if(Array[j] > array[j+1]) {temp=Array[j]; ARRAY[J]= Array[j+1]; Array[j+1] =temp; }}}};bublesort (s); Console.log (s);//=>[1, 2, 3, 4, 5, 6, 7, 8]

Code Analysis: implemented with nested for loops. The outer loop control finds the number of "larger" numbers (n-1), and the inner loop control finds the number of times that the "larger" number of "I" needs to be compared (n-1-i), because I have a "larger" number being bubbled up, so the more times you need to compare it.

Cyclic invariant type:

At the end of the 1th cycle, the last one is the maximum number;

At the end of the first I cycle, s[n-i, n-1] is the "larger" number ordered by the order;

...

At the end of the nth cycle, s[1,n-1] is the ordered "large" number, while s[0] must be the minimum number, so the entire array is ordered.

Add a line of code in the console watcher process:

vars = [8, 7, 6, 5, 4, 3, 2, 1];varBublesort =function(array) {vartemp;  for(vari=0; i < array.length-1; i++) {         for(varj=0; J < array.length-i-1; J + +) {            if(Array[j] > array[j+1]) {temp=Array[j]; ARRAY[J]= Array[j+1]; Array[j+1] =temp;    }} Console.log (s); }};bublesort (s); Console.log (s);//=>[7, 6, 5, 4, 3, 2, 1, 8 ][6, 5, 4, 3, 2, 1,7, 8 ]    [ 5, 4, 3, 2, 1,6, 7, 8 ]    [ 4, 3, 2, 1,5, 6, 7, 8 ]    [ 3, 2, 1,4, 5, 6, 7, 8 ]    [ 2, 1,3, 4, 5, 6, 7, 8 ]    [ 1,2, 3, 4, 5, 6, 7, 8 ]    [ 1, 2, 3, 4, 5, 6, 7, 8]

Cocktail Ordering

Cocktail sequencing (cocktail sort) optimizes the bubbling sort so that the outer loop can find two sorted numbers (maximum and minimum) at a time and can be interpreted as a "bidirectional" bubbling sort.

Note: Since the cocktail sort outer loop can find two sort numbers at a time, its outer loop number is binary, while the inner loop is two parallel for loops (control forward and reverse respectively). In general, cocktail sequencing is more efficient than bubble sorting in most cases.

Code:

vars = [8, 7, 6, 5, 4, 3, 2, 1];varCocktailsort =function(array) {varCount = 0; vartemp;  for(vari = 0; i < ARRAY.LENGTH/2; i++) {//Outer loop count binary for(varj = Count; J < Array.length-count-1; J + +) {//one-time forward loopif(Array[j] > array[j+1]) {temp=Array[j]; ARRAY[J]= Array[j+1]; Array[j+1] =temp; }} Count++; Record has found "large" number of numbers for(varK = array.length-1-count; K > count-1; k--) {//one-time reverse loopif(Array[k] < array[k-1]) {temp=Array[k]; ARRAY[K]= Array[k-1]; Array[k-1] =temp; }}}};cocktailsort (s); Console.log (s);//=>[1, 2, 3, 4, 5, 6, 7, 8]

Code Analysis: Outer loop number binary, because the outer loop once the inner layer already contains a round trip, the inner positive loop to find the "large" number, where the starting point is count (currently find "large" number); The starting point of the inner reverse loop is n-1-count (the first bit of the "larger" number of the wheel), The end point is count-1 (the "smaller" number currently found).

Cyclic invariant type:

At the end of the 1th cycle: The nth number is the maximum number and the 1th number is the minimum number;

At the end of the first cycle: S[n-i, n-1] is ordered by the "larger" number, s[0, I-1] is ordered by the "small" number;

...

At the end of the nth cycle: S[N-N/2, n-1] is ordered "larger", S[0, n/2-1] is ordered by the "small" number, so the entire array has been ordered.

Observe the output in the console:

vars = [8, 7, 6, 5, 4, 3, 2, 1];varCocktailsort =function(array) {varCount = 0; vartemp;  for(vari = 0; i < ARRAY.LENGTH/2; i++) { for(varj = Count; J < Array.length-count-1; J + +) {            if(Array[j] > array[j+1]) {temp=Array[j]; ARRAY[J]= Array[j+1]; Array[j+1] =temp; }} Count++;  for(varK = array.length-1-count; K > count-1; k--) {            if(Array[k] < array[k-1]) {temp=Array[k]; ARRAY[K]= Array[k-1]; Array[k-1] =temp;    }} Console.log (s); }};cocktailsort (s); Console.log (s);//=>[ 1, 7, 6, 5, 4, 3, 2, 8 ][1, 2, 6, 5, 4, 3,7, 8 ]    [ 1, 2, 3, 5, 4,6, 7, 8 ]    [ 1, 2, 3, 4, 5, 6, 7, 8 ]    [ 1, 2, 3, 4, 5, 6, 7, 8]

However, when it comes to arrays that have been ordered (such as: [1, 2, 3, 4, 5, 6, 7, 8]), the process of bubbling and cocktail sequencing results in a lot of "useless" work. You can define a flag inside a loop, and when an array element of a loop is no longer exchanging, you can think of the array as ordered:

vars = [1,2,3,4,5,8,6,7];varCocktailsort =function(array) {varCount = 0; vartemp;  for(vari = 0; i < ARRAY.LENGTH/2; i++) { var flag = false ; Define Flag for(varj = Count; J < Array.length-count-1; J + +) {            if(Array[j] > array[j+1]) {temp=Array[j]; ARRAY[J]= Array[j+1]; Array[j+1] =temp; Flag = true ; When swapping occurs, change the value of flag to True}} count++;  for(varK = array.length-1-count; K > count-1; k--) {            if(Array[k] < array[k-1]) {temp=Array[k]; ARRAY[K]= Array[k-1]; Array[k-1] =temp; Flag = true ;        When swapping occurs, change the value of flag to True}} console.log (s);  if(!  Flag)  Break; }};cocktailsort (s);//=>[1, 2, 3, 4, 5, 6, 7, 8]//output results show that the algorithm will end prematurely[1, 2, 3, 4, 5, 6, 7, 8]

--------------------------------------------------------------------------------------------------

Note: This article is for individual learning essays, only for individual study use, if there is similar, please forgive!

Bubble sort and cocktail ordering code analysis

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.