aspose file tools*
The moose likes Beginning Java and the fly likes Linked Lists, Nodes, Quicksort Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Linked Lists, Nodes, Quicksort" Watch "Linked Lists, Nodes, Quicksort" New topic
Author

Linked Lists, Nodes, Quicksort

Charlie Jackson
Greenhorn

Joined: Nov 13, 2008
Posts: 15
Hi everyone,

I am using the above for the first time and I am getting quite lost.

We've been given the following line...

public Node less (int n, Node l)

We have to use recursion to write a method which takes as input a linked list and a number and return a list of elements smaller than that number.

Can anyone help, even with the next line?

If only it was like Filter in Haskell.

Any pointers on linked lists, nodes or quicksort would be great.
Charlie Jackson
Greenhorn

Joined: Nov 13, 2008
Posts: 15
Fixed.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18133
    
  39

We've been given the following line...

public Node less (int n, Node l)

We have to use recursion to write a method which takes as input a linked list and a number and return a list of elements smaller than that number.


I really hate homework assignments that does this. When the instructor gives you part of the code, it tends to cause more confusion than actually help.

You need to understand what the line is. The paragraph is asking you for "a method which takes as input a linked list and a number and return a list of elements". So, based on the line given, and the description, what part of it defines a method, and what defines a linked list?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18133
    
  39

Originally posted by Charlie Jackpot:
Fixed.




No offense, but if you are going to provide a fake name... at least, make a decent attempt at creating one. Please fix your screen name, obviously fake names are not allowed.

Henry
Charlie Jackson
Greenhorn

Joined: Nov 13, 2008
Posts: 15
I think "Node l" refers to the linked list.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18133
    
  39

Originally posted by Charlie Jackpot:
I think "Node l" refers to the linked list.


Okay good. And n is referring to the number to be compared to. With this you can form the method.

Now, did your instructor teach you the algorithm for the quick sort? Can you form the body of the method, from what you were taught?

Henry
Charlie Jackson
Greenhorn

Joined: Nov 13, 2008
Posts: 15
Originally posted by Henry Wong:


Okay good. And n is referring to the number to be compared to. With this you can form the method.

Now, did your instructor teach you the algorithm for the quick sort? Can you form the body of the method, from what you were taught?

Henry


Well, he gave us the quicksort method but used it on an Array.

I am not sure how to use it with Linked Lists.

EDIT:

I found this....

public static void Quicksort(int A[], int f, int l)
{
if (f >= l) return;
int pivot_index = partition(A, f, l);
Quicksort(A, f, pivot_index);
Quicksort(A, pivot_index+1, l);
}
[ November 13, 2008: Message edited by: Charlie Jackson ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18133
    
  39

Originally posted by Charlie Jackson:


Well, he gave us the quicksort method but used it on an Array.

I am not sure how to use it with Linked Lists.


Well, it is the technique that is important. If you understood how it worked with an array, it is easily translated to a linked list. Just take a shot at it, provide the code here, and we can give you hints in the right direction.

Now... Did your instructor provide you with the implementation for Node? Or are you expected to implement your own linked list too?

Henry
Charlie Jackson
Greenhorn

Joined: Nov 13, 2008
Posts: 15
Originally posted by Henry Wong:


Well, it is the technique that is important. If you understood how it worked with an array, it is easily translated to a linked list. Just take a shot at it, provide the code here, and we can give you hints in the right direction.

Now... Did your instructor provide you with the implementation for Node? Or are you expected to implement your own linked list too?

Henry


I don't understand it for Array either

public static void main(String argv[])
{
int A[] = new int[argv.length];
for (int i=0 ; i < argv.length ; i++)
A[i] = Integer.parseInt(argv[i]);
Quicksort(A, 0, argv.length-1);
for (int i=0 ; i < argv.length ; i++)
System.out.print(A[i] + " ");
System.out.println();
}
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18133
    
  39

I don't understand it for Array either


Well, does any of your textbooks go through it? Obviously, you can't code something, if you don't understand the algorithm. There is no black magic here -- you have to understand first.

Henry
Charlie Jackson
Greenhorn

Joined: Nov 13, 2008
Posts: 15
I have my Node implementation.

Can you not talk me through it?

I don't want solutions, maybe just a push in the right direction.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36556
    
  16
There appear to be problems about sorting LinkedLists; the Collections class copies the list into an array.
The Node reference presumably points to the first node in the linked list which has the specified value.
Charlie Jackson
Greenhorn

Joined: Nov 13, 2008
Posts: 15
Node Class:



Sort Class:


This is what I have so far, fairly happy with "less" and "more".

"join" is joining the two lists, any ideas?
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Originally posted by Campbell Ritchie:
There appear to be problems about sorting LinkedLists; the Collections class copies the list into an array.


There aren't problems sorting a LinkedList, the problem is that the algorithm used in Collections.sort() depends on a random access data structure. That's not an issue if a different algorithm is chosen.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36556
    
  16
Aha! So that only applies to that one algorithm?
Charlie Jackson
Greenhorn

Joined: Nov 13, 2008
Posts: 15
I've finished my "sort" class.

I'll post up my final results and my "test" class today.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36556
    
  16
Originally posted by Charlie Jackson:
I've finished my "sort" class.
Well done
Charlie Jackson
Greenhorn

Joined: Nov 13, 2008
Posts: 15
Ok, the solution:


NODE



SORT







Did I post this in the right thread, is this Beginner Java?
[edit]Add code tags. CR[/edit]
[ November 19, 2008: Message edited by: Campbell Ritchie ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Linked Lists, Nodes, Quicksort
 
Similar Threads
Real time application of Inner Class
Collections API
Can nodes contain objects?
Implementing Hashtable and linked list in java
How data is added in a ArrayList