Results 1 to 5 of 5
  1. #1
    MDK231 is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default 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++];
        }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Shell Sort errors HELP!

    Java Code:
    increment = sequence[counter++];
    Hmmmm!

  3. #3
    MDK231 is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default 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?

  4. #4
    MDK231 is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default 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.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Shell Sort errors HELP!

    Your problem was that you were incrementing counter after you retrieved a value from the array. That is why it performed the sort with the first increment twice. So if post increment was wrong......

Similar Threads

  1. Replies: 1
    Last Post: 10-10-2011, 04:51 PM
  2. shell sort
    By fam2315 in forum New To Java
    Replies: 11
    Last Post: 08-24-2011, 12:32 AM
  3. Hibbard's Shell Sort
    By biglandphil in forum New To Java
    Replies: 2
    Last Post: 11-12-2009, 08:38 PM
  4. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  5. Shell Sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 07:44 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •