Files
Data-Structure/Recursion/P27_All_Sorted.cpp
2025-07-18 09:32:21 +01:00

39 lines
814 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//For a,b,c we have sort abc acb bac bca cab cba(6 kinds) This Program is to sort all of the kinds
#include <iostream>
using namespace std;
// 递减n叉树
// 可能需要包展开这里先不写先以3个为主
template<typename T>
T* sort(int n, int l, T* tmp, bool* used, T* arr){
if(n == l){
cout << "[";
for(int i = 0;i < n; i++){
cout << tmp[i] <<" ";
}
cout << "]" << endl;
}
for(int i = 0;i < l; i++){
if(used[i] == false){
tmp[n] = arr[i];
used[i] = true;
sort(n + 1, l, tmp, used, arr);
used[i] = false; //是否可以删除?不可以,回溯本节点要用
}
}
}
int main() {
int n;
cin >> n;
int* tmp = new int[n];
int* arr = new int[n];
for(int i = 0;i < n; i++){
cin >> arr[i];
}
bool* used = new bool[n];
sort(0, n, tmp, used, arr);
return 0;
}