Vernacular sorting algorithm-Bubble sorting, vernacular algorithm-bubble

Source: Internet
Author: User

Vernacular sorting algorithm-Bubble sorting, vernacular algorithm-bubble

Preface:

These two days I studied the sorting algorithm. The algorithm is abstract and easy to bypass without a stroke. So I came up with a demonstration by queuing in the physical education class.

The scenario of other chapters in the series is the same as above

Bubble Sorting:I repeatedly visited the series to be sorted and compared two elements at a time. If their order is wrong, I will exchange them. The work of visiting a sequence is repeated until there is no need for exchange, that is, the sequence has been sorted.

Scenario Description:

In the physical education class, the teacher needs a column (standing in a row from low to high). When the column is set, everyone randomly makes up a column, but the height is not consistent. A total of six people said, I will give you a way to arrange your formation according to my approach.

In this way, the students exchanged messages and sorted the queues over and over again .....

 

 

Code snippet:

/*** Bubble Sorting: Only one of the largest values can be determined at a time, placed at the end of the array, and the next time you ignore the determined maximum value, continue to compare * advantages: stability * disadvantages: efficiency (compare duplicate adjacent data, and change the position for each comparison) * @ param arr * @ return */public static int [] bubbleSort (int [] arr) {int len = arr. length; // array length for (int I = 0; I <len; I ++) {// traverses the array to limit the number of inner loops for (int j = 1; j <len-I; j ++) {// inner loop, which reduces the number of times by 1 (that is, when compared several times, the last few digits are ignored) if (arr [j-1]> arr [j]) {// if the first digit is greater than the last one, that is, after, the switch location int k = arr [j-1]; // cache the previous one. arr [j-1] = arr [j]; // Let the previous one be equal to the last arr [j] = k; // The last one is equal to the cache value before the previous one. }}return arr ;}

Therefore, it can be used when there is little data, but it should be avoided when there is a large amount of data, and other sorting methods should be selected.

 

      It is not easy to write, and it is inevitable that there are omissions and errors. Please make a generous correction. It is good and recommended.

Ps: You are welcome to reprint it. Please indicate the source for reprinting:Http://www.cnblogs.com/liuyitian/p/4054437.html

Next, we will update other sorting methods (insert, select, and quickly sort), and then integrate a series of addresses...

After work... withdraw ):

Learn a little more code every day             


Sort algorithms use (improved) bubble sort Algorithms

# I nclude <stdlib. h>
# I nclude <time. h>

Void maopao (int source [], int n)
{
Int start = 0, end = n-1;
Int I;
While (start <= end)/* if there are other elements that are not determined */
{
For (I = start; I <end; I ++)/* Find the maximum number of remaining elements */
If (source [I]> source [I + 1])
{
Int t;
T = source [I];
Source [I] = source [I + 1];
Source [I + 1] = t;
}
End --;/* Find the maximum number */
For (I = end; I> start; I --)/* Find the minimum element of the remaining element */
If (source [I] <source [I-1])
{
Int t;
T = source [I];
Source [I] = source [I-1];
Source [I-1] = t;
}
Start ++;/* Find a minimum number */
}
}

Void output (int data [], int n)
{
Int I;
For (I = 0; I <n; I ++)
{
If (I % 10 = 0)
Printf ("\ n ");
Printf ("% 4d", data [I]);
}
}

Int check (int data [], int n)
{/* Check whether the result data is sorted in ascending order */
Int I;
For (I = 0; I <n-1; I ++)
If (data [I]> data [I + 1])
Return 0;
Return 1;
}

Void main ()
{
Int data [500];
Int I;
Srand (time (NULL ));
For (I = 0; I <500; I ++)
Data [I] = random (500 );
Printf ("\ nThe original data is: \ n ");
Output (data, 500 );
Maopao (data, 500 );
Printf ("\ nAfter sort: \ n ");
Output (data, 500 );
Printf ("\ n ");
If (check (data, 500) = 1)
Printf ("\ nRight .");
Else
Printf ("\ nWrong .");
}... Remaining full text>

Bubble Sorting Algorithm

The basic concept of BubbleSort is to compare two adjacent numbers in sequence, put decimal places in front, and put large numbers in the back. That is, first compare the numbers of 1st and 2nd, and put the decimal places before and after the large numbers. Then compare the numbers of 2nd and 3rd, place the decimal places before and after the large number, and continue until the last two digits are compared. Place the decimal places before and after the large number. Repeat the above process and compare it from the first logarithm (because of the exchange of 2nd numbers and 3rd numbers, the number of 1st numbers is no longer less than 2nd, always compare to the adjacent number before the maximum number. Place the decimal number before, place the large number, and end the second row. In the second to last number, a new maximum number is obtained. So on until the sorting is completed.
Because the sorting process always places decimal places forward and large numbers backward, it is equivalent to bubbles rising, so it is called Bubble sorting.
It is implemented using a double loop. The External Loop Variable is set to I and the inner loop variable is set to j. The External Loop repeats 9 times, and the inner loop repeats 9, 8,..., 1 time in sequence. The two elements for each comparison are related to the inner loop j, which can be identified by a [j] And a [j + 1] respectively, the values of I are 1, 2 ,..., 9. For each I, j values are 1, 2 ,... 10-i.
Generate
In many program designs, we need to sort a series to facilitate statistics, and Bubble Sorting has always been favored by its concise ideas and methods.
Sorting Process
Imagine the sorted array R [1 .. n) Vertically erect. Each data element is regarded as a bubble with weight. According to the principle that light bubbles cannot be under heavy bubbles, the array R is scanned from bottom to top, when a light bubble that violates this principle is scanned, it is made to "float" up, so it is repeated until the last two bubbles are both light and heavy.
Algorithm example
49 13 13 13 13 13 13 13 13
38 49 27 27 27 27 27
65 38 49 38 38 38 38 38 38
97 65 38 49 49 49
76 97 65 49 49 49 49
13 76 97 65 65 65 65 65
27 27 76 97 76 76 76
49 49 49 76 97 97 97
Procedure BubbleSort (Var R: FileType) // Bubble Sorting from bottom to top //
Begin
For I: = 1 To N-1 Do // Do N-1 sort //
Begin
NoSwap: = True; // set unordered flag //
For J: = N-1 DownTo 1 Do // scan from bottom up //
Begin
If R [J + 1] <R [J] Then // exchange element //
Begin
Temp: = R [J + 1]; R [J + 1: = R [J]; R [J]: = Temp;
NoSwap: = False
End;
End;
If NoSwap Then Return // If no exchange occurs in this sort, terminate the algorithm //
End
End; // BubbleSort //
The time complexity of this algorithm is O (n ^ 2), and the algorithm is a stable ordering party.
Edit this section
Bubble sort code
AAuto
Bubble_sort = function (array ){
Var temp;
For (I = 1; # array ){
// The number before I is already the smallest and sorted
For (j = # array; I + 1 ...... remaining full text>

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.