Bubbling is a classic algorithm.
This paragraph code adds some optimizations:
Increase B_exchange, if the bubbling does not exchange data, then the sort is successful, exit adds n_exchange, n_head, records the most recent swap position, and the lower wheel bubble will take the position.
/ ************************************************** ****************** Created: 2006/06/15 filename: c: / documents and settings / administrator / desktop / TMMMP/POPOSORT.C FILE PATH: C: / Documents and Settings / Administrator / Deskttings / TMMP File Base: Poposort File Ext: C Author: a.tng Version: 0.0.1 Purpose: Adding Bubble Sort by Adding B_exchange, if the bubble is not exchanged, then Indicates that the sort is successful, exit adds n_exchange, n_head, records the most recent swap position, the lower wheel bubble can only go to this position ******************************************** ************************************************************************************ / # include < Stdio.h> #include
/ * * Name: poposort * params: * Polist [in / out] INT array to be sorted * n [in] int array length * Return: * 1-success 0- Failure * Notes: * Sprinkle POLIST * * Author: a.tng 2006/06/15 9:00 * / int Poposort (int * polist, int N) {INT I, J; int N_exchange
IF (null == polist) Return 0;
n_exchange = 0; for (i = 0; i B_EXCHANGE = 0; n_head = n_exchange; for (j = n - 1; j> = n_head 1; j -) {/ * 第 第 第 比, 第 第 第 第 第 * / if ( Polist [J]) {INT N_TMP_NUM; N_TMP_NUM = Polist [J]; Polist [J] = Polist [J - 1]; Polist [J - 1] = n_tmp_num; B_exchange = 1; N_EXCHANGE = J;} / * end-if * /} / * end-for * / / * If the first round of bubbling is not exchanged, it is said that it has been sorted by * / if (0 == B_EXChange) Return 1;} / * end-for * / return 1;} / * Name: main * Params: * none * return: * none * notes: * none * * author: a.tng 2006/06/15 9:05 * / int main () {// int polist [10] = {45, 12, 43, 11, 32, 34, 91, 58, 20, 82}; int Polist [10] = {0, 1, 2, 3, 4, 5, 6, 7, 9, 8}; (void) Poposort (PoliSt, 10); System ("pause"); return 0;}