实现方法:
// letcode.cpp : 定义控制台应用程序的入口点。
//
#include"stdafx.h"
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int MaoPao(int *a,int n)
{
int i, j, temp;
for(int i=0;i<n-1;i++)
for (int j = 0; j < n - 1 - i; j++)
{
if (a[j] > a[j + 1])
{
int temp = a[j];
a[j]=a[j+1];
a[j+1] = temp;
}
}
return 0;
}
int main()
{
int list[10];
cout << "请输入一组数据(10):" << endl;
for (int i = 0; i < 10; i++)
cin >> list[i];
MaoPao(list,10);
for (int i = 0; i < 10; i++)
cout << list[i]<<"->";
cout << endl;
system("pause");
return 0;
}
总结
遍历若干次要排序的数列,每次遍历是,他都会从前往后以此的比较相邻两个树的大小,如果前者比后者大,则交换他们的位置。这样一次遍历后,最大的元素就在数列的末尾,重复此操作,直到整个数列都有序为止。