Bubble Sorting of sorting algorithms and Bubble sorting algorithms

Source: Internet
Author: User

Bubble Sorting of sorting algorithms and Bubble sorting algorithms

Principle of the bubble sort algorithm:

1. Compare adjacent elements. If the first is bigger than the second, exchange the two of them.

2. perform the same operation on each adjacent element, starting from the first pair to the last one at the end. At this point, the final element should be the largest number.

3. Repeat the preceding steps for all elements except the last one.

4. Continue to repeat the above steps for fewer and fewer elements until there is no need to compare any number.

Class Program {static void Main (string [] args) {int [] array = new [] {234,632, 23,643, 2, 6,-2,423,234 2, 43}; Console. writeLine ("Before sorting:"); Console. writeLine (string. join (",", array); BubbleSort (array); Console. writeLine ("sorted:"); Console. writeLine (string. join (",", array); Console. readKey ();} /// <summary> /// bubble sort /// </summary> /// <param name = "sources"> target array </param> private static void BubbleSort (int [] sources) {int I, j, temp; for (I = 0; I <sources. length-1; I ++) {// compare with the following elements for (j = 0; j <sources. length-1-I; j ++) {if (sources [j]> sources [j + 1]) //> ascending, <descending {// exchange element temp = sources [j]; sources [j] = sources [j + 1]; sources [j + 1] = temp ;}}}}}

 


Implement the Bubble Sorting Algorithm (from small to large)

# Include <stdio. h>
Int main ()
{
Int temp, a [2000], n, I, j;
Char c1;
Printf ("Enter the number of elements n \ n ");
Scanf ("% d", & n );
Printf ("Enter the element \ n ");
For (I = 0; I <n; I ++)
Scanf ("% d % c", & a [I], & c1 );
For (I = 0; I <n; I ++)
{
For (j = n-1; j> 0; j --)
{
If (a [j] <a [J-1])
{
Temp = a [j];
A [j] = a [J-1];
A [J-1] = temp;
}
}
}
For (I = 0; I <n-1; I ++)
Printf ("% d,", a [I]);
Printf ("% d \ n", a [n-1]);
Return 0;
}

Can I introduce sorting algorithms such as the bubble method?

Algorithm Analysis and Improvement of Bubble Sorting
The basic idea of exchanging sorting is to compare the keywords of the records to be sorted in pairs. If the order of the two records is the opposite, the two records are exchanged until there is no reverse order record.
The basic concepts of application exchange sorting include Bubble sorting and quick sorting.

Bubble Sorting

1. Sorting Method
Vertically arrange the sorted record array R [1. n]. Each record R is considered as a bubble with the weight of R. key. According to the principle that a Light Bubble cannot be under a heavy bubble, scan the array R from the bottom up: Any Light Bubble scanned to a violation of this principle will make it "float" up ". This is repeated until the last two bubbles are light and heavy.
(1) initial
R [1. n] is an unordered area.

(2) First scan
The weights of two adjacent bubbles are compared from the bottom of the unordered area to the top. If the light bubbles are found to be in the lower and severe bubbles, the positions of the two bubbles are exchanged. That is, compare (R [n], R [n-1]), (R [n-1], R [N-2]),…, (R [2], R [1]); for each pair of bubbles (R [j + 1], R [j]), if R [j + 1]. key <R [j]. key, then the contents of R [j + 1] and R [j] are exchanged.
When the first scan is complete, the "lightest" bubble floated to the top of the interval, that is, the record with the smallest keyword is placed on the highest position R [1.

(3) second scan
Scan R [2. n]. When scanning is completed, the "light" bubble floated to the R [2] position ......
Finally, the sequential area R [1. n] can be obtained through n-1 scanning.
Note:
During the I-trip scan, R [1 .. I-1] and R [I.. n] are the current sequential and disordered areas, respectively. The scan continues from the bottom of the unordered area to the top of the area. When scanning is completed, the shortest bubbles in the area float to the top position R. The result is that R [1. I] is changed to a new ordered area.

2. Bubble sorting process example
Bubble Sorting of files whose keyword sequence is 49 38 65 97 76 13 27 49

3. Sorting Algorithm
(1) Analysis
Because each sort adds a bubble to the ordered area, there are n-1 bubbles in the ordered area after N-1 sort, in the disordered area, the bubble weight is always greater than or equal to the bubble weight in the ordered area. Therefore, the entire Bubble sorting process requires at most n-1 sorting.
If no bubble position exchange is found in a sorting, it means that all bubbles in the unordered area to be sorted meet the principle of being light and heavy. Therefore, the Bubble sorting process can be terminated after this sorting. Therefore, in the following algorithm, a Boolean exchange is introduced, which is set to FALSE before each sort starts. If an exchange occurs during the sorting process, set it to TRUE. Check exchange at the end of sorting. If exchange has not occurred, terminate the algorithm and no longer perform the next sorting.

(2) specific algorithms
Void BubbleSort (SeqList R)
{// R (l. n) is the file to be sorted. It uses bottom-up scanning to perform Bubble Sorting on R.
Int I, j;
Boolean exchange; // exchange flag
For (I = 1; I <n; I ++) {// a maximum of N-1 sequential sorting can be performed.
Exchange = FALSE; // The exchange flag should be FALSE before this sort starts.
For (j = n-1; j> = I; j --) // scan the current unordered zone R [I. n] from bottom to top.
If (R [j + 1]. key <R [j]. key) {// exchange Record
R [0] = R [j + 1]; // R [0] is not a sentry, only a temporary storage unit
R [j + 1] = R [j];
R [j] = R [0];
Exchange = TRUE; // The exchange flag is set to TRUE due to exchange.
}
If (! Exchange) // This sorting has not been handed in... the remaining full text>

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.