Guest User

Untitled

a guest
May 22nd, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class PancakeSorting {
  4. int [] pancakes = new int[1];
  5. Random random;
  6. int n;
  7.  
  8. public PancakeSorting(int n) {
  9. random = new Random();
  10. this.n = n;
  11. cookPancakes();
  12. }
  13.  
  14. public void start() {
  15. System.out.println("Original : ");
  16. show(pancakes);
  17. System.out.println("===================\n");
  18.  
  19. sort(pancakes);
  20.  
  21. System.out.println("===================\n");
  22. System.out.println("Sorted : ");
  23. show(pancakes);
  24. }
  25.  
  26. // Cook some pancakes
  27. private void cookPancakes() {
  28. pancakes = new int[n];
  29.  
  30. for (int i = 0; i < n; i++) {
  31. pancakes[i] = random.nextInt(n) + 1;
  32. }
  33. }
  34.  
  35. // sort the pancakes by flipping them
  36. private void sort(int [] arr) {
  37. for (int i = arr.length - 1; i >= 0; i--) {
  38. int idx = getMaxIndex(arr, i); // find the largest pancake from the stack we consider
  39.  
  40. if (idx == i)
  41. {
  42. // pancake is already in it's correct place
  43. // Nothing to do skipping here
  44. show(arr);
  45. System.out.println((i+1) + "th pancake is already in correct place, skipping to next");
  46. System.out.println();
  47. }
  48. else if (idx == 0) {
  49. flip(arr, i); // its in the front, then send it back
  50. } else {
  51. // Nothing works, its in middle somewhere, then bring it front, then flip to back
  52. flip(arr, idx); // bring it to front
  53. flip(arr, i); // send it to back and move on
  54. }
  55. }
  56. }
  57.  
  58. // returns max elem index in a given limit
  59. private int getMaxIndex(int [] arr, int limit){
  60. int idx = 0;
  61.  
  62. for (int i = 0; i <= limit; i++) {
  63. if (arr[idx] < arr[i]) idx = i;
  64. }
  65.  
  66. return idx;
  67. }
  68.  
  69. // flips the array at given end
  70. private void flip(int [] arr, int end) {
  71. int start = 0;
  72.  
  73. show(arr);
  74. System.out.println("Spatula is under " + (end + 1) + "th pancake");
  75. System.out.println();
  76.  
  77. // flip element by element from given end point
  78. while (start < end) {
  79. int tmp = arr[start];
  80. arr[start] = arr[end];
  81. arr[end] = tmp;
  82. start++;
  83. end--;
  84. }
  85. }
  86.  
  87. // show the stack of pancakes
  88. private void show(int [] arr) {
  89. System.out.print("Pancakes : ");
  90. for (int i = 0; i < arr.length; i++) {
  91. System.out.print(arr[i] + " ");
  92. }
  93.  
  94. System.out.println();
  95. }
  96.  
  97. public static void main(String[] args) {
  98. if (args.length != 1) {
  99. showUsage();
  100. }
  101. int n = -1;
  102. try {
  103. n = Integer.parseInt(args[0]);
  104. if (n <= 0)
  105. showUsage();
  106. }catch (Exception e) {
  107. showUsage();
  108. }
  109.  
  110. PancakeSorting pancakeSorting = new PancakeSorting(n);
  111. pancakeSorting.start();
  112. }
  113.  
  114. static void showUsage() {
  115. System.out.println("Invalid parameter");
  116. System.out.println("Usage: PancakeSorting <number of pancakes>");
  117. System.exit(1);
  118. }
  119. }
Add Comment
Please, Sign In to add comment