一、冒泡排序
1、基本思想:临近的两个数两两比较
2、实现代码:
#include "stdafx.h"
#include<stdlib.h>
#include<iostream>
using namespace std;
void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
void bubbleSort(int *a,int n)
{
int flag=n;
while(flag>0)
{
int k=flag;flag=0;
for(int j=1;j<k;j++)
{
if(a[j-1]<a[j])
{
swap(a[j-1],a[j]);
flag=j;
}
}
}
}
int main(int argc,char* argv[]){
int n;
int a[10000];
while(cin>>n)
{
for(int i=0;i<n;i++)
{
cin>>a[i];
}
bubbleSort(a,n);
for(int i=0;i<n;i++)
{
cout<<a[i];
}
cout<<endl;
}
system("pause");
return 0;
}
3、不完全测试结果
4、性能分析
时间复杂度:o(n)-o(n^2),平均o(n^2)
空间复杂度:o(1)
稳定