1#include <stdio.h>2 /**************************************************************3 * Bubblesort: Bubble sort implementation function (from small to large *4 * parameter: a[]: array of integers before sorting *5 * N: Array element primes *6 * return value: None *7 ***************************************************************/8 voidBubblesort (intA[],intN)9 {Ten inttemp=0; One intI=0; A intj=0; - intk=0; - intswap=0; the intswapnum=0; - - for(i=0; i<n-1&&!swap; ++i)//if there is no swap in the round, the order is complete. - { +swap=1; - for(j=n-1; j>i; --j) + { Aprintf"start compare a[%d] and a[%d]\n\n", j,j-1); at if(a[j]<a[j-1] )//change to > To sort from large to small - { -printf"a[%d]<a[%d]\n", j,j-1); -temp=a[j-1]; -printf"temp=a[%d]\n", J-1); -a[j-1]=A[j]; inprintf"a[%d]=a[%d]\n", J-1, j); -a[j]=temp; toprintf"a[%d]=temp\n", j); +swap=0; -swapnum++; the } * Else $printf"A[%d]>a[%d],no need to change.\n\n", j,j-1);Panax Notoginsengprintf"The current sort is: \ n"); - for(k=0; k<n; k++)//prints the values in the current array theprintf"%d", A[k]); +printf"\ n"); A } theprintf"******%d bubbles out ******\n", A[i]); + } -printf"total number of exchanges:%d\n", swapnum); $ } $ /*************************************************************** - * Main function: Call bubble sort function to implement sort * - ****************************************************************/ the voidMainvoid) - {Wuyi intI=0; the intj=0; - intk=0; Wu inta[5]={Ten,7, -, One,0}; - AboutBubblesort (A,5); $ - for(i=0; i<5; i++ ) -printf"%d", A[i]); - Aprintf"\ n"); +}
Bubble Sort-Fine Edition