In-depth analysis of sorting algorithms-Bubble Sorting

Source: Internet
Author: User

# Include <iostream>


Void swap (Int & A, Int & B ){

Int temp;

Temp =;

A = B;

B = temp;

}


/* Bubble sort

 

Basic Idea: first bubble to get a record with the smallest keyword, and second to get the second small value... for each record: the key words of the two adjacent records, if the reverse order is exchanged until there is no reverse order.

Features: time efficiency, a total of N-1 trip bubbles, a trip to the table of J records bubble needs to J-1 keyword comparison, space efficiency: Only one auxiliary unit (temporary variable)

*/

Void bubblesort (INT array [], int Len ){

Int I, J;

For (I = 0; I <len-1; I ++) {// I: 0 ~ Len-1 (I ++)

For (j = len-1; j> I; j --) {// J: len-1 ~ > I (j --) Minimum bubble

If (array [J-1]> array [J]) {

Swap (array [J], array [J-1]);

}

}

}

}


Int main (INT argc, const char * argv [])

{

Int I;

Int A [] = };

Int Len = sizeof (a)/sizeof (A [0]);

// Bubble sort

Bubblesort (A, Len );

For (I = 0; I <Len; I ++ ){

Printf ("% d", a [I]);

}

Printf ("\ n ");


Return0;

}

In-depth analysis of sorting algorithms-Bubble Sorting

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.