Write a bubble sort, sort a shaped array
Bubbling method: For n number of small to large bubble sort, altogether need to bubble n-1 times. 22 to compare, each bubble sort once, the maximum value at the bottom (last position).
Like what:
87 12 56) 45 78
87 and 12 switching: 12 87 56 45 78
87 and 56 switching: 12 56 87 45 78
87 and 45 Switching: 12 56 45 87 78
87 and 78 switching: 12 56 45 78 87
By the end of this first trip, the next sequence is the same.
The code is as follows:
#include <stdio.h> #include <stdlib.h>void bubble (int str[],int len) {int i,j,temp;for (i=0;i<len-1;i++) Len-1 trip Comparison {for (j=0;j<len-1-i;j++)//len-1-i 22 Comparison {if (str[j]>str[j+1]) {temp=str[j];//Interchange two numbers str[j]=str[j+ 1];str[j+1]=temp;}}}} int main () {int arr[10],len,i;for (i=0;i<10;i++) {scanf ("%d", &arr[i]);} Len=sizeof (arr)/sizeof (arr[0]), Bubble (Arr,len), for (i=0;i<len;i++) {printf ("%d", Arr[i]);} System ("pause"); return 0;}
This article is from the "Materfer" blog, make sure to keep this source http://10741357.blog.51cto.com/10731357/1712342
Write a bubble sort, sort a shaped array