Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 11-23-2004, 02:32 PM   #1 (permalink)
davidmccabe
Registered User
 
Join Date: Mar 2004
Posts: 7
davidmccabe is on a distinguished road
MergeSort performance

i have recently been given an assignment involving the use of sorting algorithms (mergesort and quicksort). i have been given the code for mergesort and had to create the quicksort. this is what i have come up with.
Code:
public class QuickSortLists
{
	static String spaces = "";
	public static void main(String[] args)
	{
		 UnorderedLinkedList l1 = new UnorderedLinkedList();
		 
		 l1.addToFront(new Integer(67));
		 l1.addToFront(new Integer(12));
		 l1.addToFront(new Integer(81));
		 l1.addToFront(new Integer(29));
		 l1.addToFront(new Integer(56));
		 l1.addToFront(new Integer(11));
		 l1.addToFront(new Integer(8));
		 l1.addToFront(new Integer(49));
		 
		 System.out.println("Sorting " + l1);
		 
		 quickSort(l1);
		 
		 System.out.println(" Sorted List " + l1);
	}
		 
	
	public static void quickSort(UnorderedLinkedList L)
	
	{ UnorderedLinkedList leftList, rightList;
		leftList = new UnorderedLinkedList();
		rightList = new UnorderedLinkedList();
		Integer I1, I2;
		
		Comparable pivot, element;
		pivot = (Comparable) L.first();
		
	    System.out.println("PIVOT IS: " + pivot);
		
		while (!L.isEmpty())
		{
			element = (Comparable) L.removeFirst();
				if(element.compareTo(pivot) < 0)
				    leftList.addToFront(element);
						else rightList.addToFront(element);
							
		}					
			 
			System.out.print("This breaks down into List1 " + leftList);
			System.out.println("  and  List2 " + rightList);
			
			System.out.println("\nSorting " + leftList);
			quickSort(leftList);
			
			System.out.println("\nSorting " + rightList);
			quickSort(rightList);
			
			System.out.print("Merging  " + leftList + " and " + rightList);
			
			while((leftList.size() > 0) && (rightList.size() > 0))
			{ 
				I1 = (Integer) leftList.first();
				I2 = (Integer) rightList.first();
				if(I1.compareTo(I2) < 0)
				  { L.addToRear(leftList.removeFirst());
				  	
				  }

				else
				{
					L.addToRear(rightList.removeFirst());
					
				}

			}
			
			while(leftList.size() > 0)
	
				L.addToRear(leftList.removeFirst());
			
			
			while (rightList.size() > 0)
			  
				 	L.addToRear(rightList.removeFirst());
			
			
		System.out.println("  to give  " + L);
		
	}
}
however tihis code doesnt seem to come up with the correct results. it throws and exception when the leftList is empty, whihc isnt what i wanted. i have tried changing the conditions in the while loop but it doesnt make any difference.

Secondly i have been asked to confirm the theoretical predictions for the performance of the Mergesort technique, using the provided mergesort program. I have no idea what this means and would appreciated it tremendously if someone could help me.
I dont know if you can post attachments in this forum so i put the other classes needed in a zip file located atthis address.

thanks in advance

david
davidmccabe is offline   Reply With Quote
Old 11-23-2004, 05:23 PM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,175
Belisarius is on a distinguished road
On the theoretical performance, I'd count the number of times through the merge you had to go before you achieved the desired end result. Let me look at the code for a bit to see if I can spot what's going wrong.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 11-23-2004, 05:55 PM   #3 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,175
Belisarius is on a distinguished road
Code:
pivot = (Comparable) L.first();
Ever wonder what would happen if L was empty? Bet this is what's causing your exception.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 11-24-2004, 02:05 AM   #4 (permalink)
davidmccabe
Registered User
 
Join Date: Mar 2004
Posts: 7
davidmccabe is on a distinguished road
would i have to use a count variable somewhere to count how many times mergesort is used?

when i try to change anythiong about the comparable pivot, i get errors saying it hasnt been declared properly.

please help
davidmccabe is offline   Reply With Quote
Old 11-24-2004, 07:01 AM   #5 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,175
Belisarius is on a distinguished road
Quote:
would i have to use a count variable somewhere to count how many times mergesort is used?
Yes, you would.

Quote:
when i try to change anythiong about the comparable pivot, i get errors saying it hasnt been declared properly.
Have you considered checking to see if L actually contains anything? If it doesn't, why bother with pivot at all?
__________________
GitS
Belisarius is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please report Code Newbie Website Performance Here sde Lounge 13 09-22-2004 01:24 AM
slow performance roadrunnersport Windows 5 08-03-2004 10:02 PM


All times are GMT -8. The time now is 01:31 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting