Advertisement
Marf_72

Untitled

Dec 11th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. //function to print the array
  2. void printarray(int arr[], int size){
  3. for(int i = 0; i < size; i++){
  4. cout << arr[i] << " ";
  5. }
  6. cout << endl;
  7. }
  8.  
  9. //function to swap the variables
  10. void swap(int *a, int *b){
  11. int temp;
  12. temp = *a;
  13. *a = *b;
  14. *b = temp;
  15. }
  16.  
  17. //permutation function
  18. void permutation(int *arr, int start, int end){
  19. if(start == end){
  20. printarray(arr, end + 1);
  21. return;
  22. }
  23. for(int i = start; i <= end; i++){
  24. //swapping numbers
  25. swap((arr + i), (arr + start));
  26. //fixing one first digit
  27. //and calling permutation on
  28. //the rest of the digits
  29. permutation(arr, start + 1, end);
  30. swap((arr + i), (arr + start));
  31. }
  32. }
  33.  
  34. int main(){
  35. //taking input to the array
  36. int size = 10;
  37. int arr[10];
  38. for(int i = 0; i < size; i++){
  39. arr[i] = i;
  40. }
  41.  
  42. //calling permutation function
  43. permutation(arr, 0, size - 1);
  44. return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement