1. 程式人生 > >C++ 對n個互不相同且均在0—n-1整數排序

C++ 對n個互不相同且均在0—n-1整數排序

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

using namespace std;

//對n個互不相同且均在0—n-1整數排序
Type
void Sort(T R[], T A[] ,int n)
{
	for (int i = 0; i < n; i++)
		A[R[i]] = R[i];
}

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

int main()
{
	int R[10] = { 1,3, 2, 9, 0, 5, 4, 7, 8,6 };
	int len = ArrayLen(R);
	int *A = new int[len];
	
	Sort(R, A,len);
	Visit(A, len);
	return 0;
}