Bubble Sort-Fine Edition

Source: Internet
Author: User

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

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.