Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9. int permute_this(int *array, int size, int count) {
  10.  
  11. int k, x, tmp;
  12. if (size < 6)
  13. return 0;
  14. k = size -2;
  15. while ( k >= 0 && array[k] > array[k+1]) {
  16. --k;
  17. }
  18.  
  19. if (k < 0)
  20. return 0;
  21. x = size-1;
  22.  
  23. while (array[k] > array[x]){
  24. --x;
  25. }
  26.  
  27. tmp = array[k];
  28. array[k] = array[x];
  29. array[x] = tmp;
  30. x = size-1;
  31. ++k;
  32.  
  33. while (k <= x) {
  34. if (array[k] > array[x]) {
  35. tmp = array[k];
  36. array[k] = array[x];
  37. array[x] = tmp;
  38. }
  39. ++k;
  40. --x;
  41. }
  42. count = count +1;
  43. printf("Count: %d ", count);
  44. for (int i=0; i < size; ++i)
  45. printf(" %d ", array[i]);
  46. printf("\n");
  47. if (count == 500) {
  48. return 0;
  49. }else{
  50. permute_this(array, size, count);
  51. }
  52. }
  53. int main() {
  54.  
  55. int size_in;
  56. printf("\nEnter the n value to which to permute to: ");
  57. scanf("%d", &size_in);
  58. int array[size_in];
  59. int i, count=1;
  60.  
  61. for (i = 0; i < size_in; i++)
  62. array[i] = i+1;
  63. for (i = 0; i < size_in; i++)
  64. printf("%d ", array[i]);
  65. printf("\n");
  66. permute_this(array, size_in, count);
  67.  
  68.  
  69.  
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement