Results 1 to 5 of 5
Thread: Shell Sort errors HELP!
- 12-09-2011, 05:01 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
Shell Sort errors HELP!
So I am trying to create a shell sort that will use designated increments to sort an array int numbers. everything seems to be running smoothly except for it does not use the final value in the increments, the 1, which is needed to fully sort the array. for some odd reason it uses the first value, the 45, twice. i put a println statement in to check if this was in fact the issue and it turns out that it was using the 45 twice. i must use a list of given increments. can anyone spot my error here? i can't seem to find the thing anywhere.
Java Code:public static void main(String[] args) { int[] sequence = {45,3,2,1}; int[] list = {5, 9, 7, 12, 24, 36, 99, 22, 67, 11, 77, 44}; shell(list,sequence); System.out.println(Arrays.toString(list)); } //shell sort is here public static void shell(int[] a, int[] sequence) { int counter2 = 0; int counter = 0; int increment =sequence[counter]; while (counter < 4) { System.out.println(increment); for (int i = increment; i < a.length; i++) { int j = i; int temp = a[i]; while (j >= increment && a[j - increment] > temp) { a[j] = a[j - increment]; j = j - increment; } a[j] = temp; } increment = sequence[counter++]; } }
- 12-09-2011, 05:10 AM #2
Re: Shell Sort errors HELP!
Hmmmm!Java Code:increment = sequence[counter++];
- 12-09-2011, 05:34 AM #3
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
Re: Shell Sort errors HELP!
so i'm assuming it's in the right place as i got it caught in an endless loop a few different ways.

am i not increasing the counter correctly?
- 12-09-2011, 05:44 AM #4
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
Re: Shell Sort errors HELP!
HA! GOT IT! or this would work. good old for loops.
Java Code:public static void shell(int[] a, int[] sequence) { for(int u=0; u<4; u++) { int increment =sequence[u]; System.out.println(increment); for (int i = increment; i < a.length; i++) { int j = i; int temp = a[i]; while (j >= increment && a[j - increment] > temp) { a[j] = a[j - increment]; j = j - increment; } a[j] = temp; } } }
just out of curiosity though. what would have needed to change to make it work the other way? Thanks for the help.
- 12-11-2011, 10:24 PM #5
Similar Threads
-
Creating a new Shell. The parent Shell shouldn't lose the focus ...
By anouri in forum SWT / JFaceReplies: 1Last Post: 10-10-2011, 04:51 PM -
shell sort
By fam2315 in forum New To JavaReplies: 11Last Post: 08-24-2011, 12:32 AM -
Hibbard's Shell Sort
By biglandphil in forum New To JavaReplies: 2Last Post: 11-12-2009, 08:38 PM -
How to sort a list using Bubble sort algorithm
By Java Tip in forum AlgorithmsReplies: 3Last Post: 04-29-2008, 08:04 PM -
Shell Sort in Java
By Java Tip in forum AlgorithmsReplies: 0Last Post: 04-15-2008, 07:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks