Advertisement
Guest User

multi merge sort

a guest
Jan 28th, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. package edu.wmich.cs3310.A1;
  2.  
  3. public class MultiMergeSort {
  4.  
  5. /**
  6. * Takes the array of items and sorts them first by bag alphabetically
  7. * and then by current strength if there are duplicate items, afterwards
  8. * merge the array of items as a whole and sort them alphabetically and then
  9. * by current strength if there are duplicate items
  10. * @param item the list of items
  11. * @param index the number of items
  12. * @param num_bag the number of bags
  13. * @return sorted1 the items sorted
  14. */
  15. public Node[] multi_Sort(Node[][] item, int index, int num_bag){
  16.  
  17. Sort sorting = new Sort();
  18. Bag bag_function = new Bag();
  19.  
  20. Node[] sorted = new Node[index];
  21. Node temp;
  22.  
  23. //Go through each bag individually
  24. for(int x = 0; x < num_bag;) {
  25. for(int i = 0; i < 24; i++) {
  26. for(int j = i + 1; j < 25; j++)
  27. {
  28. //Compare names of the item, with the next item
  29. //If the name of the current item is "bigger" than the next item, swap the two item
  30. if(item[x][i].name.compareToIgnoreCase(item[x][j].name) > 0) {
  31. temp = item[x][i]; //temp storage for current item
  32. item[x][i] = item[x][j]; //Swap the current with the next item
  33. item[x][j] = temp; //Let the next item be the current item that was swapped
  34. }
  35. //If the name are the same, compare current strength of item with the current strength of the next item
  36. //If the strength of the current item is larger than the next item, swap the two item
  37. else if((item[x][i].name.compareToIgnoreCase(item[x][j].name) == 0) && (item[x][i].cur > item[x][j].cur)) {
  38. temp = item[x][i]; //temp storage for the current item
  39. item[x][i] = item[x][j]; //swap the current item with the next item
  40. item[x][j] = temp; //Let the next item be the current item was swapped
  41. }
  42. }
  43. }
  44. x++; //increment to the next bag after going through all the items in the previous bag
  45. }
  46.  
  47. sorted = bag_function.single_bag(item, index, num_bag);
  48.  
  49. merge_sort(sorted);
  50.  
  51. //sort the bag whole inventory
  52. //sorted = sorting.InventorySort(item, index, num_bag);
  53.  
  54. return sorted;
  55.  
  56. }
  57.  
  58. /**
  59. * Merge sort the array by dividing and conquering, splitting the array in half and then sorting the left
  60. * and the right, and after both have been sorted, merge together to sort
  61. * @param items the array containing the items
  62. */
  63. public void merge_sort(Node[] items) {
  64. if(items.length >= 2) {
  65. Node[] left = new Node[items.length/2]; //left half of the array from the middle
  66. Node[] right = new Node[items.length - items.length / 2]; //right half of the array from the middle
  67.  
  68. //The left half of the array
  69. for(int i = 0; i < left.length; i++) {
  70. left[i] = items[i];//store the left half of the array
  71. }
  72.  
  73. //the right half of the array
  74. for(int i = 0; i < right.length; i++) {
  75. right[i] = items[i + items.length / 2]; //store the right half of the array
  76. }
  77.  
  78. merge_sort(left); //sort the left half
  79. merge_sort(right); //sort the right half
  80. merge(items, left, right); //Merge the two halves together and sort
  81.  
  82. }
  83. }
  84.  
  85. /**
  86. * Comparing the two halves of the array and storing it into one array after sorting
  87. * both halves of the array of items
  88. * @param items the array containing all the items
  89. * @param left the left half of the items
  90. * @param right the right half of the items
  91. */
  92. public void merge(Node[] items, Node[] left, Node[] right) {
  93. int a = 0;
  94. int b = 0;
  95.  
  96. for(int i = 0; i < items.length; i++) {
  97. //If the left item comes alphabetically before the left item
  98. if(b >= right.length || (a < left.length && left[a].name.compareToIgnoreCase(right[b].name) < 0)) {
  99. items[i] = left[a]; //Store the left item into the array
  100. a++; //increment the left half
  101. }
  102. //If the left item is the same as the right item, but the left item's strength is less than the right item
  103. else if(b >= right.length || (a < left.length && left[a].name.compareToIgnoreCase(right[b].name) == 0 && left[a].cur < right[b].cur)) {
  104. items[i] = left[a]; //store the left item into the array
  105. a++; //increment the left half
  106. }
  107. else {
  108. items[i] = right[b]; //store the right item
  109. b++; //increment the right half
  110. }
  111. }
  112. }
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement