1. 程式人生 > >C++ 雙向氣泡排序演算法

C++ 雙向氣泡排序演算法

#include <bits/stdc++.h>
#define MaxSize 100
#define ArrayLen(array) sizeof(array)/sizeof(array[0])
#define TypeName template<class T>
/*
* Created by HarvestWu on 2018/07/23.
*/

using namespace std;

TypeName
void SWAP(T R[], int i, int j)
{
	int temp = R[i];
	R[i] = R[j];
	R[j] = temp;
}

//計數排序
TypeName
void Sort(T R[],int n)
{
	int l = 0, r = n - 1;
	while (l<r)
	{
		for (int i = l; i < r; i++)//由左向右掃描,將最大值置於右邊
		{
			if (R[i]>R[i + 1])
				SWAP(R, i, i + 1);
		}
		--r;
		for (int j = r; j > l; --j)//由右向左掃描,將最小值置於左邊
		{
			if (R[j] < R[j - 1])
				SWAP(R, j, j - 1);
		}
		++l;
	}
}


//列印
TypeName
void Visit(T R[],int n)
{
	for (int i = 0; i < n; i++)
		cout << R[i] << " ";
	cout << endl;
}

int main()
{
	int R[10] = { 1, 8, 115, 6, 7, 12, 4, 83, 9,0 };
	int len = ArrayLen(R);
	Sort(R, len);
	Visit(R, len);
	return 0;
}