Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package edu.wmich.cs3310.A1;
- public class MultiMergeSort {
- /**
- * Takes the array of items and sorts them first by bag alphabetically
- * and then by current strength if there are duplicate items, afterwards
- * merge the array of items as a whole and sort them alphabetically and then
- * by current strength if there are duplicate items
- * @param item the list of items
- * @param index the number of items
- * @param num_bag the number of bags
- * @return sorted1 the items sorted
- */
- public Node[] multi_Sort(Node[][] item, int index, int num_bag){
- Sort sorting = new Sort();
- Bag bag_function = new Bag();
- Node[] sorted = new Node[index];
- Node temp;
- //Go through each bag individually
- for(int x = 0; x < num_bag;) {
- for(int i = 0; i < 24; i++) {
- for(int j = i + 1; j < 25; j++)
- {
- //Compare names of the item, with the next item
- //If the name of the current item is "bigger" than the next item, swap the two item
- if(item[x][i].name.compareToIgnoreCase(item[x][j].name) > 0) {
- temp = item[x][i]; //temp storage for current item
- item[x][i] = item[x][j]; //Swap the current with the next item
- item[x][j] = temp; //Let the next item be the current item that was swapped
- }
- //If the name are the same, compare current strength of item with the current strength of the next item
- //If the strength of the current item is larger than the next item, swap the two item
- else if((item[x][i].name.compareToIgnoreCase(item[x][j].name) == 0) && (item[x][i].cur > item[x][j].cur)) {
- temp = item[x][i]; //temp storage for the current item
- item[x][i] = item[x][j]; //swap the current item with the next item
- item[x][j] = temp; //Let the next item be the current item was swapped
- }
- }
- }
- x++; //increment to the next bag after going through all the items in the previous bag
- }
- sorted = bag_function.single_bag(item, index, num_bag);
- merge_sort(sorted);
- //sort the bag whole inventory
- //sorted = sorting.InventorySort(item, index, num_bag);
- return sorted;
- }
- /**
- * Merge sort the array by dividing and conquering, splitting the array in half and then sorting the left
- * and the right, and after both have been sorted, merge together to sort
- * @param items the array containing the items
- */
- public void merge_sort(Node[] items) {
- if(items.length >= 2) {
- Node[] left = new Node[items.length/2]; //left half of the array from the middle
- Node[] right = new Node[items.length - items.length / 2]; //right half of the array from the middle
- //The left half of the array
- for(int i = 0; i < left.length; i++) {
- left[i] = items[i];//store the left half of the array
- }
- //the right half of the array
- for(int i = 0; i < right.length; i++) {
- right[i] = items[i + items.length / 2]; //store the right half of the array
- }
- merge_sort(left); //sort the left half
- merge_sort(right); //sort the right half
- merge(items, left, right); //Merge the two halves together and sort
- }
- }
- /**
- * Comparing the two halves of the array and storing it into one array after sorting
- * both halves of the array of items
- * @param items the array containing all the items
- * @param left the left half of the items
- * @param right the right half of the items
- */
- public void merge(Node[] items, Node[] left, Node[] right) {
- int a = 0;
- int b = 0;
- for(int i = 0; i < items.length; i++) {
- //If the left item comes alphabetically before the left item
- if(b >= right.length || (a < left.length && left[a].name.compareToIgnoreCase(right[b].name) < 0)) {
- items[i] = left[a]; //Store the left item into the array
- a++; //increment the left half
- }
- //If the left item is the same as the right item, but the left item's strength is less than the right item
- else if(b >= right.length || (a < left.length && left[a].name.compareToIgnoreCase(right[b].name) == 0 && left[a].cur < right[b].cur)) {
- items[i] = left[a]; //store the left item into the array
- a++; //increment the left half
- }
- else {
- items[i] = right[b]; //store the right item
- b++; //increment the right half
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement