Write a bubble sort, sort a shaped array

Source: Internet
Author: User

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

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.