Friday 15 February 2013

java - Modified bubble sort, but not working correctly -



java - Modified bubble sort, but not working correctly -

the task modify bubble sort it’s bidirectional.

this means "in" index first carry largest item left right, when reaches "out", reverse , carry smallest item right left.

the bubblesort() method normal way presented in example.

my code in method bidibubblesort().

for reason when ran programme gave me output sorted, not correctly.

i manually did each step on piece paper pencil don't know i'm overlooking.

unsorted: 7 6 5 4 3 2 1

and

sorted: 6 4 2 1 3 5 7

class arraybub { private long[] a; // ref array private int nelems; // number of info items // -------------------------------------------------------------- public arraybub(int max) // constructor { = new long[max]; // create array nelems = 0; // no items yet } // -------------------------------------------------------------- public void insert(long value) // set element array { a[nelems] = value; // insert nelems++; // increment size } // -------------------------------------------------------------- public void display() // displays array contents { for(int j=0; j<nelems; j++) // each element, system.out.print(a[j] + " "); // display system.out.println(""); } // origin of code ------------------------------------------------------- public void bidibubblesort() { int out, x, y; int in = 0; for(x=0, out=nelems-1; out>x; out--, x++) // outer loop (backward) for(in=x; in<out+1; in++) // inner loop (forward) if( in<out ) if( a[in] > a[in+1] ) // out of order? swap(in, in+1); // swap them else // (in==out) for(y=out-1; y>x; y--) // reverse if( a[y] < a[y-1] ) swap(y, y-1); } // end of code ------------------------------------------------------------- public void bubblesort() { int out, in; for(out=nelems-1; out>1; out--) // outer loop (backward) for(in=0; in<out; in++) // inner loop (forward) if( a[in] > a[in+1] ) // out of order? swap(in, in+1); // swap them } // end bubblesort() private void swap(int one, int two) { long temp = a[one]; a[one] = a[two]; a[two] = temp; } // -------------------------------------------------------------- } // end class arraybub class bubblesortapp { public static void main(string[] args) { int maxsize = 100; // array size arraybub arr; // reference array arr = new arraybub(maxsize); // create array arr.insert(7); // insert 7 items arr.insert(6); arr.insert(5); arr.insert(4); arr.insert(3); arr.insert(2); arr.insert(1); arr.display(); // display items arr.bidibubblesort(); // bidirectional bubble sort arr.display(); // display them 1 time again } // end main() } // end class bubblesortapp

the primary purpose of bidirectional bubble sort or cocktail sort solve problem of lowest values (aka turtles) @ end of unsorted list move origin of list faster conventional bubble sort.

keeping objective in mind, need understand bidirectional bubble sort sorts both ends. ie., beginning end of list, end origin of list every pass.

your current implementation seems unsuccessfully tweak conventional bubble sort. advice forget implementation of bubble sort , code above stated objective w.r.to bubble sort in mind.

to start off, below hint:

decide exit of outer loop dynamically status check if list has been sorted or not. not rely on counter on outer loop conventional bubble sort does.

if still not able solve it, take peek @ wikipedia article , implement: http://en.wikipedia.org/wiki/cocktail_sort. only @ if not able solve it!

java sorting bubble-sort

No comments:

Post a Comment