Chapter 3 - Simple Sorting.Fall2009

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

Info icon This preview shows pages 1–9. 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
Image of page 1

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

View Full Document Right Arrow Icon
2/47 Major Topics Introductory Remarks Bubble Sort Selection Sort Insertion Sort Sorting Objects Comparing Sorts
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.
Image of page 3

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

View Full Document Right 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…
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.
Image of page 5

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

View Full Document Right 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.
Image of page 6
7/47 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? { long temp = a[one]; a[one] = a[two]; a[two] = temp; } } // end clas s ArrayBub // Normally s hould als o contain find() and delete() methods too… Dis cus s es pecially the parameters ;.!!!
Image of page 7

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

View Full Document Right Arrow Icon
8/47 clas s BubbleSortApp { public s tatic void main(String[] args ) { int maxSize = 100; // array size // note; merely s etting up a s ize ArrayBub arr; // reference to an object arr = new ArrayBub(maxSize); // creates object of type ArrayBub and feeds parameter to the // cons tructor which s ets the s ize of the array. .
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern