Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //function to print the array
- void printarray(int arr[], int size){
- for(int i = 0; i < size; i++){
- cout << arr[i] << " ";
- }
- cout << endl;
- }
- //function to swap the variables
- void swap(int *a, int *b){
- int temp;
- temp = *a;
- *a = *b;
- *b = temp;
- }
- //permutation function
- void permutation(int *arr, int start, int end){
- if(start == end){
- printarray(arr, end + 1);
- return;
- }
- for(int i = start; i <= end; i++){
- //swapping numbers
- swap((arr + i), (arr + start));
- //fixing one first digit
- //and calling permutation on
- //the rest of the digits
- permutation(arr, start + 1, end);
- swap((arr + i), (arr + start));
- }
- }
- int main(){
- //taking input to the array
- int size = 10;
- int arr[10];
- for(int i = 0; i < size; i++){
- arr[i] = i;
- }
- //calling permutation function
- permutation(arr, 0, size - 1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement