0

I've run in to a problem and need your guidance. Basically i managed to create this Bubble Sort method. How can i modify this to Gap Sort, that rather than comparing neighboring elements each time through the list, compares elements that are some number(i) positions apart, where (i) is an integer less than n. For example, the first element would be compared to the (i + 1) element, 2nd element to the (i + 2) element, nth element to the (n-i) element, etc. A single iteration is completed when all of the elements that can be compared, have been compared. On the next iteration, i is reduced by some number greater than 1 and the process continues until i is less than 1

public static void bubbleSort (Comparable[] data, int maxlength){
    int position, scan;
    Comparable temp;

    for (position = maxlength; position >= 1; position--){
        for (scan = 0; scan <= position – 1; scan++){
            if (data[scan].compareTo(data[scan+1]) > 0){
                // Swap the values
                temp = data[scan];
                data[scan] = data[scan + 1];
                data[scan + 1] = temp;
            }
        }
    }
}
Douglas B. Staple
  • 10,510
  • 8
  • 31
  • 58
serge
  • 366
  • 1
  • 4
  • 22
  • Are you looking for a GapSort implementation? check out the second post on this link: http://www.daniweb.com/software-development/java/threads/238791/gap-sort – Federico Vera Apr 03 '12 at 07:18
  • thnx. but can you explain something? "double SF = 1.3;" why is that useful? – serge Apr 03 '12 at 07:28
  • The idea is to start with a big gap, and shrink it on every loop. The SF is something like a "shrinking coeficent" (I assume in the post means something like "shrinking factor") that is in this case around 76% (That means that on every iteration the gap is reduced to a 76% of it's original value). – Federico Vera Apr 03 '12 at 07:33
  • Just as a note, if your objects implement Comparable the easiest and fastest way to sort them is usually Arrays.sort()... – Federico Vera Apr 03 '12 at 07:36
  • cool i get it! thank you – serge Apr 03 '12 at 07:47
  • To close the question I added the comment as an answer. =) – Federico Vera Apr 03 '12 at 07:51

1 Answers1

1

This code (found on http://www.daniweb.com/software-development/java/threads/238791/gap-sort) might help you:

public static void gapSort (Comparable [] data, int size) {  
    int index;
    int gap, top;
    Comparable temp;
    boolean exchanged;

    double SF = 1.3;
    gap = size;

    do {
        exchanged = false;
        gap = (int) (gap / SF);
        if (gap == 0){
            gap = 1;
        }
        for (index = 1; index <= size - gap; index++) {
            if (data [index].compareTo(data [index + gap]) > 0) {
                temp = data [index];
                data [index] = data [index + gap];
                data [index + gap] = temp;
                exchanged = true;
            }
        }
    } while (exchanged || gap > 1);
}

Remember that the easiest way to sort an array of objects that implement the Comparable interface is usually Arrays.Sort()

Federico Vera
  • 1,357
  • 1
  • 12
  • 18