"Little Practice" js bubble sort

Source: Internet
Author: User

Task: Set a random number of 5 numbers, using the JS bubble sorting method to order from small to large.

such as: [5 3 4 1 2] The output is [1 2 3 4 5].

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

"Explanation: What is the bubble sorting method"

The so-called bubble sorting method, is a set of numbers from the big to small or from the smallest to the big sort of an algorithm. The exact method is that the adjacent value 22 is exchanged. Starting with the first value, if the next two numbers are arranged in a different order than our expectations, the position of the two digits is swapped (swapped), and if it is consistent with our expectations, it is not exchanged. Repeat the process until there is no value at the end that needs to be exchanged, then the sort is complete. Generally, if there are N number need to sort, then need to do (N-1) bubbling, we take from small to large sort as an example to see, the specific situation as shown:

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

"Analysis" How to sort [5, 3, 4, 2, 1] with the bubbling sort method?

(Important: When comparing sizes, the first compares the 1th and 2nd numbers, the second compares the 2nd and 3rd numbers, and so on.) )

Sequence: [5, 3, 4, 2, 1]

The first cycle, 5>3, the two Exchange positions: 3,5,4,2,1

Then, 5>4, the two Exchange positions: 3,4,5,2,1

Then, 5>2, the two Exchange positions: 3,4,2,5,1

Then, 5>1, the two Exchange positions: 3,4,2,1,5

At this point, the sequence becomes [3,4,2,1,5], followed by a second large loop:

Second cycle, 3<4, the two do not exchange position: 3,4,2,1,5

Then, 4>2, the two Exchange positions: 3,2,4,1,5

Then, 4>1, the two Exchange positions: 3,2,1,4,5

At this point, the sequence becomes [3,2,1,4,5], followed by a third large loop:

The third cycle, 3>2, is the exchange of the two positions: 2,3,1,4,5

Then, 3>1, the two Exchange positions: 2,1,3,4,5

At this point, the sequence becomes [2,1,3,4,5], followed by a fourth large loop:

Fourth cycle, 2>1, the two Exchange positions: 1,2,3,4,5

After 4 cycles of sorting, it is successfully sorted by the small to the general number.

Therefore, a total of 4 cycles are required (i = 0; I < series length; i++)

For each sort, compare the number of J and j+1 (j = 0; J < sequence length-I; j + +)

When number of J > number j+1, two digits are swapped.

"Transposition Mode":

When A>b, want to let A and B exchange position, how to do?

Set X as the intermediate over-zone.

Make x=b,//Put the value of B in X first

B=a,//Place the value of a in the position of B

A=x,//put the b value stored in X in the position of a

The value exchange of A and B is complete.

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

Program

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en"><HTML><Head><title>New Document</title></Head><Body><Scriptlanguage= "JavaScript"type= "Text/javascript"><!--varArray= [5, 3, 4, 2, 1];varTemp= 0; for (varI= 0; I<array.length; I++)//Set total number of loops { for (varJ= 0; J<Array.Length-i; J++)//Set Each loop to be sorted several times {if(Array[j]>array[j+ 1])//Compare the number of J and j+1, and change the position when j>j+1 {temp=array[j+ 1]; Array[j+ 1] =Array[j]; Transposition Process Array[j]=temp; }    }} for(varI=0; I<Array.length;i++) {document.write (Array[i]+" "); Output sequence in current order}// -</Script></Body></HTML>

"Little Practice" js bubble sort

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.