Guest User

Untitled

a guest
Jul 20th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. cat /home/djszapi/Downloads/Shellsort.java
  2. package uebung04;
  3. import static gdi.MakeItSimple.*;
  4.  
  5. public class Shellsort{
  6. public static int numberOfSwaps = 0;
  7. public static int numberOfChecks = 0;
  8. public static int numberOfRuns = 0;
  9.  
  10. /**
  11. * The Shell Sort, which sorted the Array with the given h-Sort Chain.
  12. * @param data contains the array which shall be sorted.
  13. * @param hSortChains the given h-Sort Chain
  14. */
  15. public static void shellsort(int[] data, int[] hSortChains) {
  16.  
  17. //Select the h-Sort Chain, which it should sort with.
  18. for(int stepInH = 0; stepInH < hSortChains.length; stepInH++) {
  19. int h = hSortChains[stepInH];
  20. numberOfRuns++;
  21.  
  22. /* Get hold of the element, which should be place on the right spot.
  23. * Both Indexpointer points at the same element.
  24. * IndexPointer1 is the pointer which, tells Indexpointer2 to point
  25. * at the same element.
  26. * IndexPointer2 is the pointer which checks the element.
  27. */
  28. for(int indexPointer1 = h; indexPointer1 < data.length; indexPointer1++) {
  29. int tmp = indexPointer1;
  30. int indexPointer2 = indexPointer1;
  31.  
  32. numberOfChecks++;
  33. // This loop starts to swap the elements.
  34. while(indexPointer2 >= h && data[indexPointer2-h] > tmp){
  35.  
  36. /* First swap, if the conditions are true.
  37. */
  38. numberOfChecks++;
  39. if (data[indexPointer2 - h] > tmp){
  40. data[indexPointer2] = data[indexPointer2 - h];
  41.  
  42. indexPointer2 = indexPointer2 - h;
  43. }
  44.  
  45. // Second Swap, this is always Swapping.
  46. data[indexPointer2] = tmp;
  47. numberOfSwaps++;
  48.  
  49. //Prints the swap of two selected elements.
  50. println("After swap No." + numberOfSwaps);
  51. for(int pos=0; pos < data.length ;pos++) {
  52. if(pos == indexPointer1 || pos == indexPointer2)
  53. print("("+data[pos]+") ");
  54. else
  55. print(data[pos]+" ");
  56. }
  57. println();
  58. }
  59. }
  60. }
  61. }
  62.  
  63. /** Print the content of the given array.
  64. * @param data contains the array which content shall be printed.
  65. * @param opt contains optional indices, that shall be marked
  66. */
  67. public static void printArrayContent(int[] data, int... opt) {
  68. // Array showing which field shall be marked
  69. boolean[] markMap = new boolean[data.length];
  70. if (opt.length > 0) {
  71. for (int i=0; i < opt.length; i++) {
  72. markMap[opt[i]] = true;
  73. }
  74. }
  75.  
  76. for (int i=0; i<data.length; i++) {
  77. if (markMap[i]) {
  78. print("(" + data[i] +") ");
  79. } else {
  80. print(data[i]+" ");
  81. }
  82. }
  83. println();
  84. }
  85.  
  86. //main method which calls the shellsort method and prints the sorted array.
  87. public static void main (String[] args){
  88. // Unsorted Array
  89. int[] data ={11, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  90.  
  91. // The given h-Sort sequences.
  92. int[] hSortChains = {9, 7, 4, 1};
  93.  
  94. shellsort(data, hSortChains);
  95. println();
  96. print("The sorted Array: ");
  97. printArrayContent(data);
  98. println();
  99. println("numberOfRuns:\t" + numberOfRuns);
  100. println("numberOfChecks:\t" + numberOfChecks);
  101. println("numberOfSwaps:\t" + numberOfSwaps);
  102. }
  103.  
  104. }
Add Comment
Please, Sign In to add comment