Bat面试算法

1.冒泡排序

#include 
using namespace std;
class BubbleSort
{
public:
	int* bubbleSort(int *a, int n)
	{
		for(int i=n-1;i>0;i--)
			for (int j=0;j a[j + 1])
				{
					int temp = a[j];
					a[j] = a[j + 1];
					a[j + 1] = temp;
				}
		return a;
	}
};
int main()
{
	BubbleSort a;
	int x[] = { 2,3,7,1,5 };
	int *p=a.bubbleSort(x, 5);
	for (int i = 0;i < 5;i++)
	{
		cout << p[i] << endl;
	}
}

2.选择排序

#include
using namespace std;
class SelectSort
{
public:
	void swap(int&a, int&b)
	{
		int temp = a;
		a = b;
		b = temp;
	}
	int *selectSort(int*a,int n)
	{
		for (int i = n - 1;i > 0;i--)
		{
			int MaxIndex=0;
			for (int j = 1;j < i + 1;j++)
			{
				if (a[j] > a[MaxIndex])
					MaxIndex = j;
			}
			swap(a[MaxIndex], a[i]);
		}
		return a;
	}
};
int main()
{
	SelectSort s;
	const int N = 5;
	int x[N] = { 1,6,3,7,0 };
	int *p = s.selectSort(x, N);
	cout << "从小到大排序为:"<



 

你可能感兴趣的:(Bat面试算法)