Помогите реализовать полный перебор элементов массива
Количество элементов массива задавать вручную - собственно N.
Массив заполняется числами от 1 до N.
Стоит задача выполнить полный перебор массива чисел, т.е., например, при N=3 на выходе должно быть:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
Код:
#include <iostream>
#include <ctime>
int variant_counter = 0;
void fill_array(int * arr, int num_of_elem) {
for (int i = 0; i < num_of_elem; i++) {
arr[i] = i + 1;
}
}
void print_array(int *arr, int num_of_elem) {
variant_counter++;
for (int i = 0; i < num_of_elem; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}
void swap(int *a, int *b) {
int aVal = *a;
int bVal = *b;
*a = bVal;
*b = aVal;
}
void permute(int *arr, int first_elem, int last_elem, int num_of_elem) {
if (first_elem == last_elem) {
print_array(arr, num_of_elem);
return;
}
for (int i = first_elem; i <= last_elem; i++) {
swap((arr + first_elem), (arr + i));
permute(arr, first_elem + 1, last_elem, num_of_elem);
swap((arr + first_elem), (arr + i));
}
}
int main() {
using namespace std;
cout << "Enter the number of elements in the array (1 - 10): ";
int num_of_elem = 0;
cin >> num_of_elem;
if (num_of_elem <= 0 || num_of_elem > 10) {
cout << "The number you entered is out of range.nBye...n";
return 0;
}
int *arr = new int[num_of_elem];
fill_array(arr, num_of_elem);
clock_t start_time = clock();
permute(arr, 0, num_of_elem - 1, num_of_elem);
clock_t finish_time = clock();
double duration = (double)(finish_time - start_time) / CLOCKS_PER_SEC;
cout << "Execution time: " << duration << " secondsn";
delete [] arr;
cout << variant_counter << " variants found" << endl;
return 0;
}
#include <ctime>
int variant_counter = 0;
void fill_array(int * arr, int num_of_elem) {
for (int i = 0; i < num_of_elem; i++) {
arr[i] = i + 1;
}
}
void print_array(int *arr, int num_of_elem) {
variant_counter++;
for (int i = 0; i < num_of_elem; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}
void swap(int *a, int *b) {
int aVal = *a;
int bVal = *b;
*a = bVal;
*b = aVal;
}
void permute(int *arr, int first_elem, int last_elem, int num_of_elem) {
if (first_elem == last_elem) {
print_array(arr, num_of_elem);
return;
}
for (int i = first_elem; i <= last_elem; i++) {
swap((arr + first_elem), (arr + i));
permute(arr, first_elem + 1, last_elem, num_of_elem);
swap((arr + first_elem), (arr + i));
}
}
int main() {
using namespace std;
cout << "Enter the number of elements in the array (1 - 10): ";
int num_of_elem = 0;
cin >> num_of_elem;
if (num_of_elem <= 0 || num_of_elem > 10) {
cout << "The number you entered is out of range.nBye...n";
return 0;
}
int *arr = new int[num_of_elem];
fill_array(arr, num_of_elem);
clock_t start_time = clock();
permute(arr, 0, num_of_elem - 1, num_of_elem);
clock_t finish_time = clock();
double duration = (double)(finish_time - start_time) / CLOCKS_PER_SEC;
cout << "Execution time: " << duration << " secondsn";
delete [] arr;
cout << variant_counter << " variants found" << endl;
return 0;
}