Advertisement
Guest User

shuffler

a guest
Mar 4th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1.  
  2. //------------------------------ Assignment -----------------------------------
  3. // Programmer:Junnun Ali Course-Section:AP comp sci
  4. // E-mail address: Assignment: activity 3
  5. // Creation Date: Last Modified: 3/2/15
  6. //-----------------------------------------------------------------------------
  7. // PURPOSE:
  8. //-----------------------------------------------------------------------------
  9. // INPUT:
  10. //-----------------------------------------------------------------------------
  11. // OUTPUT:
  12. //-----------------------------------------------------------------------------
  13. // NOTES: 1. No error checking is done on the information provided by user.
  14. //-----------------------------------------------------------------------------
  15.  
  16. /**
  17. * This class provides a convenient way to test shuffling methods.
  18. */
  19. public class Shuffler {
  20.  
  21.  
  22. //The number of consecutive shuffle steps to be performed in each call to each sorting procedure.
  23.  
  24. private static final int SHUFFLE_COUNT = 5;
  25.  
  26. private static final int VALUE_COUNT = 4;
  27.  
  28.  
  29. public static void main(String[] args) {
  30. System.out.println("Results of " + SHUFFLE_COUNT +
  31. " consecutive perfect shuffles:");
  32. int[] values1 = new int[VALUE_COUNT];
  33. for (int i = 0; i < values1.length; i++) {
  34. values1[i] = i;
  35. }
  36. for (int j = 1; j <= SHUFFLE_COUNT; j++) {
  37. perfectShuffle(values1);
  38. System.out.print(" " + j + ":");
  39. for (int k = 0; k < values1.length; k++) {
  40. System.out.print(" " + values1[k]);
  41. }
  42. System.out.println();
  43. }
  44. System.out.println();
  45.  
  46. System.out.println("Results of " + SHUFFLE_COUNT +
  47. " consecutive efficient selection shuffles:");
  48. int[] values2 = new int[VALUE_COUNT];
  49. for (int i = 0; i < values2.length; i++) {
  50. values2[i] = i;
  51. }
  52. for (int j = 1; j <= SHUFFLE_COUNT; j++) {
  53. selectionShuffle(values2);
  54. System.out.print(" " + j + ":");
  55. for (int k = 0; k < values2.length; k++) {
  56. System.out.print(" " + values2[k]);
  57. }
  58. System.out.println();
  59. }
  60. System.out.println();
  61. }
  62.  
  63.  
  64.  
  65. public static void perfectShuffle(int[] values) {
  66. int[] temp = new int[values.length];
  67. int mid = (values.length + 1) / 2;
  68.  
  69. // Interleave elements 0 ... mid-1 with elements mid ... length-1
  70. int unshuffledPos = 0;
  71. int k = 0;
  72. for ( ; k < mid; k++) {
  73. temp[unshuffledPos] = values[k];
  74. unshuffledPos += 2;
  75. }
  76. unshuffledPos = 1;
  77. for ( ; k < values.length; k++) {
  78. temp[unshuffledPos] = values[k];
  79. unshuffledPos += 2;
  80. }
  81.  
  82. // Copy elements back to values
  83. for (k = 0; k < values.length; k++) {
  84. values[k] = temp[k];
  85. }
  86. }
  87.  
  88.  
  89.  
  90. public static void selectionShuffle(int[] values) {
  91. for (int k = values.length - 1; k > 0; k--) {
  92. int pos = (int) (Math.random() * (k + 1)); // range 0 to k, inclusive
  93. int temp = values[pos];
  94. values[pos] = values[k];
  95. values[k] = temp;
  96. }
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement