DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2008

    How to make this sorting Algorithm work

    Hi, I'm pretty new to java and I'm trying to work on this sorting algorithm question.

    here's the main bit:
    public static void chooseSongs(ArrayList<Song> l, int k)  {
     		int []tmpArray = new int[a.length];
     		mergeSort(a,tmpArray,0,a.length - 1);
    	private static void mergeSort(int[]a, int[]tmpArray, int left, int right) {
    		if( left < right) {
    			int centre = (left + right) /2;
    			mergeSort(a,tmpArray, left, centre);
    			mergeSort(a.tmpArray, centre + 1, right);
    			merge(a, tmpArray, left, centre + 1, right);
    	private static void merge(int[]a, int[]tmpArray, int leftPos, int rightPos, int rightEnd){
    		int leftEnd = rightPos - 1;
    		int tmpPos = leftPos;
    		int leftBeg = leftPos;
    		while(leftPos <= leftEnd && rightPos <= rightEnd){
    			if(a[leftPos] < a[rightPos]){
    				tmpArray[tmpPos++] = a[leftPos++];
    				tmpArray[tmpPos++] = a[rightPos++];
    		while(leftPos <= leftEnd){
    			tmpArray[tmpPos++] = a[leftPos++];
    		while(rightPos <= rightEnd){
    			tmpArray[tmpPos++] =a[rightPos++];
    		for(tmpPos = leftBeg; tmpPos <= rightEnd; tmpPos++){
    			a[tmpPos] = tmpArray{tmpPos};
    ummm it looks long and tedious but it's basically just the merge sort algorithm I've been given.

    The code is using arraylist but to make it to work I'll have to change it toarrays. Because I'm a newbie and my java skill is poor I don't really know what to do. Any help would be great.

    ps: I've attached the code for the whole thing if the above looks confusing (this application is suppose to sort a list of songname, artist and time into order)
    Attached Files Attached Files
    Last edited by Hack; 08-14-2008 at 01:13 PM. Reason: Added Code Tags

  2. #2
    Join Date
    Jul 2005
    SW MO, USA
    Try debugging it by adding println() statements at key points and watch how control flows. use a short list of records to sort.

Similar Threads

  1. Replies: 1
    Last Post: 09-01-2001, 12:22 PM
  2. Replies: 1
    Last Post: 11-23-2000, 08:51 AM
  3. STILL Can't make buttons WORK!!
    By superwebmonkey in forum Java
    Replies: 2
    Last Post: 05-13-2000, 09:28 PM
  4. Can't make buttons work!!
    By superwebmonkey in forum Java
    Replies: 1
    Last Post: 05-12-2000, 02:01 PM
  5. CrossBrowser DHTML table sorting problem
    By Joel Matto in forum Web
    Replies: 0
    Last Post: 05-02-2000, 03:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.