Bubble sort is a sort of contact in high school, simpler and easier to understand relative to other sorts
The feature of this series of blogs is--give the results of each order
Originally wanted to write a good process, get a picture of what, but think online analysis too much, are better, so these blog even if it is a summary of their own it.
#include <stdio.h>void bubblesort (int *m, int n) {int i,j;for (i=0;i<n-1;i++) {for (j=1;j<n-i;j++) if (M[j] <m[j-1]) {m[j]=m[j]^m[j-1];m[j-1]=m[j]^m[j-1];m[j]=m[j]^m[j-1];} for (int t=0;t<10;t++) printf ("%d", m[t]);p rintf ("\ n");}} int main () {int i;int a[10]={2,8,5,7,4,3,1,9,6,10};for (i=0;i<10;i++) printf ("%d", A[i]);p rintf ("\ n \ n"); Bubblesort (a,10), for (i=0;i<10;i++) printf ("%d", A[i]);p rintf ("\ n"); return 0;}
After running
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6C/23/wKiom1VAkO-Qj4N4AAE-BorVOcY493.jpg "title=" 1.jpg " alt= "Wkiom1vako-qj4n4aae-borvocy493.jpg"/>
Here's an improvement to the sort.
void Bubblesort (int *m, int n) {int i,j;bool flag;for (i=0;i<n-1;i++) {flag=true;for (j=1;j<n-i;j++) if (m[j]<m[ J-1]) {m[j]=m[j]^m[j-1];m[j-1]=m[j]^m[j-1];m[j]=m[j]^m[j-1];flag=false;} for (int t=0;t<10;t++) printf ("%d", m[t]);p rintf ("\ n"); if (flag) break;}}
After running
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6C/24/wKiom1VAlRLBKdisAAEndH5zZZQ123.jpg "title=" 1.jpg " alt= "Wkiom1valrlbkdisaaendh5zzzq123.jpg"/>
This article is from the "hacker" blog, make sure to keep this source http://anglecode.blog.51cto.com/5628271/1640391
Sorting algorithm Big Collection _ Exchange Class--bubble sort