Bubble Sort algorithm

Source: Internet
Author: User

/* date:2014.12.13
Bubble sort idea: Exchange sort, through the comparison of adjacent data, exchange to achieve the purpose of sorting.
Process: 1). The data in the array, comparing the size of the adjacent two elements in turn;
2). If the previous data is larger than the subsequent data, the two data will be exchanged, after the first round of multiple comparisons sorted, the smallest (or maximum) data can be taken well;
3). Compare the remaining data one by one in the same way until (n-1) order.
Time complexity: Worst O (n^2), average O (n^2).
Space complexity: O (1).
is a stable sorting algorithm.

*/

void Bubblesort (int arr[],int len)
{
int i,j,k,temp;

for (i = 0;i < Len-1;i + +)
{
for (j = len-1;j > I;j--)
{
if (Arr[j-1] > Arr[j])
{
temp = Arr[j];
ARR[J] = arr[j-1];
ARR[J-1] = temp;
}
}
printf ("The result of order%d is:", i + 1);
for (k = 0;k < Len;k + +)
{
printf ("%d", arr[k]);
}
printf ("\ n");
}
}



Bubble Sort algorithm

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.