The basic idea of bubble sort
The bubble sort (Bubble sort) is a sort of exchange, the basic idea is: 22 compare the keywords of adjacent records, if any need to exchange, until there is no record location required.
First, bubble sort simple implementation (beginner version)
#include "stdafx.h" #define OK 1#define ERROR 0#define TRUE 1#define FALSE 0typedef int Status; #define MAXSIZE 10000 /* For the maximum number of arrays to sort, modify */typedef struct{int r[maxsize+1];/* to store the array you want to sort, r[0] as a sentinel or temporary variable */int length;/* is used to record the length of the sequential table */}sqlist;void print (SqList L) {int i;for (i=1;i<l.length;i++) printf ("%d,", L.r[i]);p rintf ("%d ", L.r[i]);p rintf (" \ n ");} /* The subscript of the array r in the interchange L is the value of I and J */void swap (SqList *l,int i,int j) {int temp=l->r[i]; l->r[i]=l->r[j]; l->r[j]=temp; }/* Order table L for Exchange sort (Bubble sort starter) */void BubbleSort0 (sqlist *l) {int i,j;for (i=1;i<l->length;i++) {for (j=i+1;j<=l-> length;j++) {if (L->r[i]>l->r[j]) {swap (L,I,J);/* Exchange l->r[i] and L->r[j] (*}}} #define N 9int _tmain ( int argc, _tchar* argv[]) {int d[n]={9,1,5,8,3,7,4,6,2}; SqList l0;int i;for (i=0;i<n;i++) l0.r[i+1]=d[i]; l0.length=n;printf ("Pre-order: \ n");p rint (L0); BubbleSort0 (&l0);p rintf ("after sorting: \ n");p rint (L0); GetChar (); return 0;}
Operation Result:
Sorting ideas: To achieve the previous and subsequent comparisons, each time only the smallest move to the highest, the other element position almost unchanged.
Cons: It can be seen that the sorting of the remaining keywords does not help when the position is 1 and 2 (the number 3 is replaced by the last one). In other words, the efficiency of this algorithm is very low.
Two: Authentic bubble sort
/* Bubble sort order table L */void bubblesort (sqlist *l) {int i,j;for (i=1;i<l->length;i++) {for (j=l->length-1;j>=i;j--) / * Note that J is from back-to-front loop */{if (l->r[j]>l->r[j+1])/* If the former is greater than the latter (note the difference between the previous algorithm here) */{swap (l,j,j+1);/* Exchange l->r[j] and L >R[J+1] ' value */}}}}
Code Analysis: In the process of continuous circulation, in addition to the keyword 1 in the first position, we also put the keyword 2 from the Nineth place mentioned the third position, obviously this algorithm than the previous to have progress, in the 100,000 data in the sequencing process, this difference will be reflected. The smaller numbers in the figure float up to the top as bubbles, so the algorithm is named Bubble algorithm.
Summary of Advantages: 1) The minimum keyword 1 to the highest, 2) the first comparison of the keyword 2 is placed near the appropriate location.
Three: Bubble sort upgrade Version
/* Make an improved bubbling algorithm for order table L */void BubbleSort2 (sqlist *l) {int i,j; Status flag=true;/* flag is used as a marker */for (i=1;i<l->length && flag;i++)/* If flag is true to indicate that there has been a data exchange, otherwise stop looping */{flag= False;/* initially false */for (j=l->length-1;j>=i;j--) {if (l->r[j]>l->r[j+1]) {swap (l,j,j+1);/* Swap l->r[j ]/l->r[j+1] */flag=true;/* If there is data exchange, flag is TRUE */}}}}
In addition to the first and second keywords that need to be swapped out, everything else is already in the normal order. When I=1, 2 and 1 were exchanged, and the sequence was ordered, but the algorithm still bricks i=2 to 9 and the loops in each loop were executed once, although no data was exchanged, but a large number of comparisons were largely superfluous.
Pros: minus after sorting k times, the table is already ordered, and subsequent comparisons are not required.
4. Analysis of bubble sequencing complexity
Analyze the complexity of its time. When the best case, that is, to sort the table itself is ordered, according to the last improved code, the comparison of n-1 times, the complexity of O (n). When the worst case, the list to be sorted in reverse order, at this time need to compare the total number of times, and for the record movement of equal numbers. Therefore, the total time complexity is O (N2).
Bubble sort in-depth explanation