Thread: Merge Sort Help
View Single Post
  #1 (permalink)  
Old 01-25-2008, 05:14 PM
Hollywood Hollywood is offline
Member
 
Join Date: Jan 2008
Posts: 5
Hollywood is on a distinguished road
Merge Sort Help
Hello everyone, I am new to the Forum.
I have a homework problem implementing the merge sort using generic list not w/ an array. I didn't see anything online about using it w/ list. I was wondering if you could give me a hand w/ my code. I understand everything untill the actual merging of the list, and just can't seem to get the algorithm. Keep in mind I am sorting the list as is and not copying it into sub-lists as to keep efficiency. Thanks for any of your help.
Code:
package sort; import utilities.containers.*; /** * This class subdivides a list of elements into a subsequences of elements, * then merges the single elements in order to form a single ordered list. */ public class MergeSort <E>{ /** * MergeSort Starting algorithm. * Require: a non-empty list of elements to sort. * Ensure: returns the input list in increasing order. * */ public void mergesort(List <E> toSort){ if (toSort.size()<=1){ /*Do Nothing*/ } else{ int low = 0; int high = toSort.size()-1; sort(toSort,low,high); } } /** * Private sort method used to break down input list into single list elements. * */ private void sort(List <E> toSort,int low,int high){ if(low==high){ /*Do Nothing*/ } else{ int middle = (high+low)/2; sort(toSort,low,middle); sort(toSort,middle,high); merge(toSort,low, middle, high); } } /** * Private merge method used to merge and order two generic lists. */ private void merge(List<E> toSort,int low, int middle, int high){ /** *This is the area I don't get. */ } /** * Swaps two generic elements of a list. */ private void swap(List<E> toSort,int left, int right){ E temp = toSort.get(left); toSort.set(left,toSort.get(right)); toSort.set(right,temp); } }//end of class MergeSort
Reply With Quote
Sponsored Links