Bubble Sorting Algorithm

Source: Internet
Author: User

Bubble Sorting(Bubble sort) Is a simple sorting algorithm. It repeatedly visits the series to be sorted, compares two elements at a time, and exchanges them if their order is wrong. The work of visiting a sequence is repeated until there is no need for exchange, that is, the sequence has been sorted. The name of this algorithm comes from because the smaller elements will slowly "float" to the top of the series through the exchange.

Address:Http://www.cnblogs.com/archimedes/p/bubble-sort-algorithm.html, Reprinted, please specify the source address.

Bubble Sorting requires the comparison times of O (n2) for N items and can be sorted in the same place. Although this algorithm is one of the simplest sorting algorithms to understand and implement, it is inefficient for sequence sorting except a few elements.

Bubble Sorting has the same execution time as insert sorting, but the number of exchanges required by the two methods is quite different. In the worst case, Bubble Sorting requires O (n2) switching, while insert sorting requires O (n) switching at most. The implementation of Bubble Sorting (similar to the following) usually performs (O (n2) poorly on the sorted series, while inserting sorting in this example only requires O (n) operations. Therefore, many modern algorithm textbooks do not use Bubble Sorting instead of insert sorting. If a flag is used for the first execution of the internal loop to indicate whether there is a possibility of exchange, the bubble sort may also reduce the best complexity to O (n ). In this case, there is no need to exchange the sorted series. If you reverse the visit sequence and compare the size of each visit sequence, you can also slightly improve the efficiency. This is sometimes called round-trip sorting, because the algorithm shuttles between one end of the series and the other end.

The process of sorting a column of numbers using Bubble Sorting

The Bubble Sorting Algorithm operates as follows:

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

  2. Perform the same operation on each adjacent element, from the first to the last. 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 them.

Due to its simplicity, Bubble Sorting is usually used to introduce the concept of algorithms to students who are getting started with programming.

Many algorithms for Bubble sorting are implemented online,This article uses the C language generic implementation:

# include # include // completed on 2014.10.7 // language: c99 // All Rights Reserved (c) codingwu (Mail: [email protected]) // blog address: http://www.cnblogs.com/archimedes/void swap (void * csv4, void * csv4, int size) {char buffer [size]; memcpy (buffer, csv3, size); memcpy (csv4, csv4, size ); memcpy (KD, buffer, size);} int cmp_int (const void * a, const void * B) {return * (int *) A-* (int *) B ;} int cmp_d Ouble (const void * a, const void * B) {return * (double *) A> * (double *) B? 1:-1;} void bubblesort (void * base, int N, int elemsize, INT (* CMP) (const void *, const void *)) {char * q = (char *) base; char * P = (char *) base + N * elemsize; while (P> q) {for (; Q! = P-elemsize; q + = elemsize) {If (CMP (Q, q + elemsize)> 0) {swap (Q, q + elemsize, elemsize );}} Q = (char *) base; P-= elemsize ;}} int main (void) {// test data int arr1 [] = {5, 4, 1, 3, 6, 12, 8, 22, 34, 76}; // bubble sort bubblesort (arr1, 10, sizeof (INT), cmp_int); // print the sorting result int I; for (I = 0; I <10; I ++) printf ("% d", arr1 [I]); printf ("\ n "); double arr2 [] = {5.4, 4.8, 1.2, 3.4, 6.7, 12.12, 8.6, 22.12, 34.5}; bubblesort (arr2, 10, sizeof (double ), cmp_double); for (I = 0; I <10; I ++) printf ("%. 2f ", arr2 [I]); printf (" \ n ") ;}

The running result is as follows:

The general implementation is as follows (c ):

# Include <stdio. h> void bubblesort (INT arr [], int count) {int I = count, J; int temp; while (I> 0) {for (j = 0; j <I-1; j ++) {If (ARR [J]> arr [J + 1]) {temp = arr [J]; arr [J] = arr [J + 1]; arr [J + 1] = temp;} I -- ;}} int main () {// test data int arr [] = {5, 4, 1, 3, 6}; // bubble sort bubblesort (ARR, 5 ); // print the sorting result for (INT I = 0; I <5; I ++) printf ("% 4D", arr [I]);}

Sort by flags in bubble Mode

If the sequence is known to be ordered, a flag can be used to reduce unnecessary judgment and improve efficiency.

Void bubblesort (INT d [], int size) // assume that the pair-wise exchange occurs at the last two positions of the array {int exchange = size-1; while (Exchange) {// the location where data exchange occurs under the record int bound = exchange; exchange = 0; // assume there is no data exchange for (INT I = 0; I <bound; I ++) {If (d [I]> d [I + 1]) {swap (& D [I], & D [I + 1]); exchange = I ;}}}}

Bubble Sorting Algorithm

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.