C ++ review the nine Bubble sorting method

Source: Internet
Author: User
Objective: To learn the Bubble Sorting Algorithm experiment content: implement the Bubble Sorting Algorithm and define it as a function. The parameter is a pointer variable pointing to an array.

* Program header Comments start * copyright and version statement of the program section * copyright (c) 2011, Yantai University Computer college student * All Rights Reserved. * File Name:*By:Zhang chuanxin* Completion date:2012Year 4Month 12Day * version number:1

* Description of the task and solution * input Description: data to be sorted initialized in the Program * Problem description: Implement Bubble Sorting * program output: sorting result * the comment in the program header ends (a slash is also deleted here)

# Include <iostream> using namespace STD; void bubble_sort (int * P, int num); // sort void output_array (int *, INT ); // output the sorted array int main () {int A [20] = }; int B [15] = {, 73}; bubble_sort (A, 20 ); // sort the elements output_array (A, 20) in a in descending order using the bubble method; // output the sorted array bubble_sort (B, 15 ); // sort the elements output_array (B, 15) in B in descending order using the bubble method; // output the sorted array return 0 ;} // The following custom function void bubble_sort (int * P, int num) {int I, j, T; // I, j is the control variable, T is the intermediate variable for (j = 0; j <= num-1; j ++) {for (I = 0; I <num-1-j; I ++) {If (* (p + I + 1)> * (p + I) // if the next number is greater than the previous one, the switching position {T = * (p + I ); * (p + I) = * (p + I + 1); * (p + I + 1) = T ;}}} void output_array (int * P, int num) {int I; for (I = 0; I <num; I ++) {cout <* (p + I) <"";} cout <Endl ;}

Running result:

Experience Accumulation: Bubble Sorting is very impressive, and it is always time to move a bigger or smaller one forward,

I think that, in my life, I keep putting the problems solved at the end, and putting new and important problems at the front.

When you solve the problem, the path you walk will be displayed at your feet !!!

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.