Welcome to Dream.In.Code
Become a Java Expert!

Join 150,385 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,148 people online right now. Registration is fast and FREE... Join Now!




Insertion sort in Java

 
Reply to this topicStart new topic

Insertion sort in Java

zvezdas
14 Feb, 2008 - 02:22 PM
Post #1

New D.I.C Head
*

Joined: 14 Feb, 2008
Posts: 3

I am having problems with understanding the insertion sort this is the homework that i have to do.
public static <E extends Comparable<? super E>> int insertionSort (List<E> list, int start, int end)

This method sorts the elements in a subrange of a list using insertion sort. The elements from index start to index end (inclusive) are sorted, other elements in the list are unchanged. The method returns the number of comparisons that were made during the sorting operation.

Your code will be similar to the code in figure 8.2 on page 306 of the book, except for three changes: First, the elements are in a list, not an array, so to access the elements you will use list.get(index) and list.set(index, element). Second, you will be sorting a sublist, so you will not start or stop your loops with the same indices as in figure 8.2. Instead, you will adapt the loops to use the start and end method parameters. Third, you need to count and return the number of element comparisons that are made during the sort. Only count comparisons between elements, do not count comparisons involving indices or other primitives.

Note that this method uses the generic type variable E to specify the element type, and that it guarantees that objects of type E are comparable. You do not need to do anything special in your method, just use type E whenever you declare variables to hold list elements. (In the figure in the book they named their type parameter AnyType instead of E.)

And this is what i got so far. i don't know what i am doing wrong.
package assignment04;

import java.util.List;

public class insertion
{
public static <E extends Comparable<? super E>> int insertionSort (List<E> list, int start, int end)
{
for(start = 1; start < list.get(index); start++)
{
E tmp = list.set(index, element);
int j = start;

for(; j > 0 && tmp.compareTo(a[j - 1]) < 0; j--)
a[ j ] = a[ j - 1];
a[ j ] = tmp;
}

}
}

User is offlineProfile CardPM
+Quote Post

Paul_Bunyan
RE: Insertion Sort In Java
14 Feb, 2008 - 05:43 PM
Post #2

New D.I.C Head
*

Joined: 14 Feb, 2008
Posts: 2

QUOTE(zvezdas @ 14 Feb, 2008 - 03:22 PM) *

I am having problems with understanding the insertion sort this is the homework that i have to do.
public static <E extends Comparable<? super E>> int insertionSort (List<E> list, int start, int end)

This method sorts the elements in a subrange of a list using insertion sort. The elements from index start to index end (inclusive) are sorted, other elements in the list are unchanged. The method returns the number of comparisons that were made during the sorting operation.

Your code will be similar to the code in figure 8.2 on page 306 of the book, except for three changes: First, the elements are in a list, not an array, so to access the elements you will use list.get(index) and list.set(index, element). Second, you will be sorting a sublist, so you will not start or stop your loops with the same indices as in figure 8.2. Instead, you will adapt the loops to use the start and end method parameters. Third, you need to count and return the number of element comparisons that are made during the sort. Only count comparisons between elements, do not count comparisons involving indices or other primitives.

Note that this method uses the generic type variable E to specify the element type, and that it guarantees that objects of type E are comparable. You do not need to do anything special in your method, just use type E whenever you declare variables to hold list elements. (In the figure in the book they named their type parameter AnyType instead of E.)

And this is what i got so far. i don't know what i am doing wrong.
package assignment04;

import java.util.List;

public class insertion
{
public static <E extends Comparable<? super E>> int insertionSort (List<E> list, int start, int end)
{
for(start = 1; start < list.get(index); start++)
{
E tmp = list.set(index, element);
int j = start;

for(; j > 0 && tmp.compareTo(a[j - 1]) < 0; j--)
a[ j ] = a[ j - 1];
a[ j ] = tmp;
}

}
}



Hey, are you CS2420? Looks like it, I think I'm in your class and am having the exact same problems.

And the deadline is fast apraoching...

sad.gif
User is offlineProfile CardPM
+Quote Post

zvezdas
RE: Insertion Sort In Java
14 Feb, 2008 - 05:49 PM
Post #3

New D.I.C Head
*

Joined: 14 Feb, 2008
Posts: 3

[
[/quote]


Hey, are you CS2420? Looks like it, I think I'm in your class and am having the exact same problems.

And the deadline is fast apraoching...

sad.gif
[/quote]
Yea, have you done the graphs?
User is offlineProfile CardPM
+Quote Post

Paul_Bunyan
RE: Insertion Sort In Java
14 Feb, 2008 - 05:56 PM
Post #4

New D.I.C Head
*

Joined: 14 Feb, 2008
Posts: 2

QUOTE(zvezdas @ 14 Feb, 2008 - 06:49 PM) *


Hey, are you CS2420? Looks like it, I think I'm in your class and am having the exact same problems.

And the deadline is fast apraoching...

sad.gif

...

Yea, have you done the graphs?



Not yet, still trying to sort out the code. I'm stumbling on getting the array examples to work right with the Lists. The coding is supposed to be the easy part, haha. Have you made any of the other methods?

This post has been edited by Paul_Bunyan: 14 Feb, 2008 - 05:57 PM
User is offlineProfile CardPM
+Quote Post

zvezdas
RE: Insertion Sort In Java
14 Feb, 2008 - 06:10 PM
Post #5

New D.I.C Head
*

Joined: 14 Feb, 2008
Posts: 3

No, i didn't i am having the same problem like you are. I don't get how to do the test code for the methods to actually see the time. And its a lot of points to loose for this one assignment.
User is offlineProfile CardPM
+Quote Post

bhandari
RE: Insertion Sort In Java
15 Feb, 2008 - 04:47 AM
Post #6

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
QUOTE
so you will not start or stop your loops with the same indices as in figure 8.2.


Atleast post the relevant portion.Where the hell is figure 8.2?

Anyway refer to the snippets below:
snippet 1
snippet 2
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:53PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month