排序

//C++ 排序方法的总结
#include <iostream>
#include <vector>
using namespace std;

/*
采用插入法进行排序
*/
void InsertArray(int a[],int n, int* &b)
{
	int temp;
    int j;
	for(int i = 0;i<n-1;i++)
	{
		j  = i;
		while(a[j]>a[j+1])  //后者小于前者,交换
		{
			temp = a[j];
			a[j] = a[j+1];
			a[j+1] = temp;

			if(j>0)
			{
				j--;
			}
		}

	}
	b = a;
}

/*
采用冒泡排序
*/
void SortArray(int a[], int n)
{
	int temp ;
	for (int i = 1;i<n;i++)//n-1次
		for (int j = 0;j<n-i;j++)//n-i次
		{
			if (a[j]>a[j+1])
			{
				temp = a[j];
				a[j]= a[j+1];
				a[j+1] = temp;
			}
		}
}


void main()
{
	int *b;
	int a[13]= {43,21,89,15,43,28,24,5,67,8,78,23,46};
// 	InsertArray(a,13,b);
// 	cout<<"选择排序的结果"<<endl;
// 	for(int k = 0;k<13;k++)
// 	{
// 		cout<<*(b+k)<<' ';
// 	}
 	cout<<"冒泡排序的结果"<<endl;
	SortArray(a,13);
	for(int k = 0;k<13;k++)
	{
		cout<<a[k]<<' ';
	}
}

 

posted @ 2012-08-08 15:41  CBDoctor  阅读(209)  评论(0)    收藏  举报