Chapter 3 - Simple Sorting.Fall2009

Chapter 3 - Simple Sorting.Fall2009 - COP 3540 Data...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
1/47 COP 3540 Data Structures with OOP Simple Sorting Chapter 3
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2/47 Major Topics Introductory Remarks Bubble Sort Selection Sort Insertion Sort Sorting Objects Comparing Sorts
Background image of page 2
3/47 Introductory Remarks So many ‘lists’ are better dealt if ordered . Will look at simple sorting first. Note: there are books written on many advanced sorting techniques. e.g. shell sort; quicksort; heapsort; etc. Will start with ‘simple’ sorts. Relatively slow, Easy to understand, and Excellent performance under circumstances. All these are O(n 2 ) sorts.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4/47 Basic idea of these simple sorts: Compare two items Swap or Copy over Some have Move, Copy over, Move… Depending on the specific algorithm…
Background image of page 4
5/47 Bubble Sort Very slow but simple Basic idea: Start at one end of a list - say, an array Visualize left to right (or top to bottom) array of integers… Compare the first items in the first two positions If the first one is larger, swap with the second move to the down one position; otherwise, simply down. At end, the largest item is in the lowest position. Repeat, except for the last position.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6/47 Bubble Sort process After first pass, made n-1 comparisons and between 0 and n-1 swaps . Continue this process. Next time we do not check the last entry (n-1), because we know it is in the right spot. We stop comparing at (n-2) compares. Perhaps you can already see that the number of compares will be (n-1) + (n-2) _ …. (more ahead) You see how it goes. Let’s look at some code. Author’s applets are available, if desired.
Background image of page 6
clas s ArrayBub { private long[] a; // ref to array a private int nElems; // number of data items //-------------------------------------------------------------- public ArrayBub(int max) // cons tructor - s ets s ize of array up via argument pas s ed by client. { a = new long[max]; // create the array nElems = 0; // no items yet } public void ins ert(long value) // put element into array at the end { a[nElems] = value; // insert it nElems++; // increment size } public void dis play() // displays array contents { for(int j=0; j<nElems; j++) // for each element, System.out.print(a[j] + " "); // display it System.out.println(""); } public void bubbleSort() Here’s the real s ort its elf! { int out, in; // note: local variables !! 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 s wap(int one, int two) Note: this method is private . So what does this mean? mean?
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/04/2011 for the course COP 3540 taught by Professor Bobroggio during the Fall '11 term at University of South Florida.

Page1 / 45

Chapter 3 - Simple Sorting.Fall2009 - COP 3540 Data...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online