准备一个笔试,温习一下排序算法。将数据结构书上的排序算法都自己写出来运行一下。刚写了个冒泡的,3分钟不到,写好了,试着运行一下,还很顺利,结果完全正确。呵呵,还很少这么顺利,原来经常是费了半天劲,写出来不说逻辑错误,排除语法错误都要一些时间。不错,也许是因为这个简单吧。
#include "stdafx.h"
#include<iostream>
using namespace std;
void BubbleSort(int a[],int n);
int _tmain()
{
int arr[]={9,8,7,6,89,4,3,2,1,0};
BubbleSort(arr,10);
for(int i=0;i!=10;i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}
void BubbleSort(int a[],int n)
{
for(int i=0;i<n;i++)
for(int j=n-1;j>=i;j--)
{
int temp;
if(a[j]<a[j-1])
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
}
}