Data structure--sort--bubble sort algorithm

Source: Internet
Author: User

Bubble Sort (Bubblesort) is a simple sort algorithm. It repeatedly visited the sequence to sort, comparing two elements at a time, and swapping them out if they were wrong in the order. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted. The algorithm is named because the smaller elements will slowly "float" through the switch to the top of the sequence.

The bubble sort requires a comparison of O (N2) for n items and can be sorted in situ. Although this algorithm is one of the simplest to understand and implement a sorting algorithm, it is inefficient for ordering a sequence of numbers other than a few elements.

The bubbling sort is the same execution time as the insertion sort, but the two methods differ greatly in the number of exchanges required. In the worst case scenario, the bubble sort requires an O (N2) Exchange, while the insertion sort is as long as the O (N) interchange. The implementation of a bubbling sort (similar to the following) usually performs poorly on a sorted sequence (O (N2)), whereas the insertion order in this example requires only O (n) operations. As a result, many modern algorithmic textbooks avoid using bubble sorting and replace them with insertion sorting. Bubble sort if the internal loop is executed for the first time, it is possible to use a flag to indicate that there is no need to swap, and it is possible to reduce the best complexity to O (n). In this case, there is no need to exchange the sequenced sequence. If the sequence of visits and the size of comparison are reversed, the efficiency can be improved slightly. Sometimes called round-tripping, because the algorithm shuttles from one end of the sequence to the other.

The process of using bubble sort to sort a column of numbers

The bubbling sorting algorithm works as follows:

    1. Compares the adjacent elements. If the first one is bigger than the second one, swap them both.

    2. Do the same for each pair of adjacent elements, starting with the last pair from the first pair to the end. At this point, the last element should be the maximum number.

    3. Repeat the above steps for all elements, except for the last one.

    4. Repeat the above steps each time for fewer elements, until there are no pairs of numbers to compare.

#include <stdio.h>voidBubblesort (intArr[],intcount) {    inti =Count, J; inttemp;  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--; } }intMain () {//test Data    intArr[] = {5,4,1,3,6}; //Bubble SortBubblesort (arr,5); //Print Sort Results     for(inti =0; I <5; i++) printf ("%4d", Arr[i]);}

Bubble Sort Using flags

If the known quantity column is basically orderly, a flag can be used to reduce unnecessary judgment and improve efficiency.

voidBubblesort (intD[],intSize//assume that the 22 interchange takes place in the last two positions of the array{    intExchange = Size-1;  while(Exchange) {//record where the data exchange occurred        intbound =Exchange; Exchange=0;//Assuming there's no data exchange on this trip ,         for(inti =0; I < bound; i++) {            if(D[i] > d[i +1]) {Swap (&d[i], &d[i+1]); Exchange=i; }        }    }}

C-language generics implement the algorithm for bubbling sorting :

#include <stdio.h>#include<stdlib.h>//completed on 2014.10.7 23:45//Language:c99////Copyright (C) Codingwu (mail: [email protected])//Blog Address:http://www.cnblogs.com/archimedes/voidSwapvoid*VP1,void*VP2,intsize) {        CharBuffer[size];        memcpy (buffer, VP1, size);        memcpy (VP1, VP2, size);  memcpy (VP2, buffer, size); }intCmp_int (Const void*a,Const void*b) {return*(int*) A-* (int*) b; }intCmp_double (Const void*a,Const void*b) {return*(Double*) A > * (Double*) b?1: -1; } voidBubblesort (void*Base,intNintElemsize,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; }}intMainvoid){    //test Data    intArr1[] = {5,4,1,3,6, A,8, A, the, the}; //Bubble SortBubblesort (ARR1,Ten,sizeof(int), cmp_int); //Print Sort Results    inti;  for(i =0; I <Ten; i++) printf ("%d", Arr1[i]); printf ("\ n"); DoubleArr2[] = {5.4,4.8,1.2,3.4,6.7,12.12,8.6,22.12,34.5,76.3}; Bubblesort (ARR2,Ten,sizeof(Double), cmp_double);  for(i =0; I <Ten; i++) printf ("%.2f", Arr2[i]); printf ("\ n");}

The results of the operation are as follows:

Http://www.cnblogs.com/archimedes/p/bubble-sort-algorithm.html

Data structure--sort--bubble sort 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.