1 / 14

Lecture 18: Selection Sort, Quicksort & Merge Sort

CompSci 105 SS 2005 Principles of Computer Science. Lecture 18: Selection Sort, Quicksort & Merge Sort. Lecturer: Santokh Singh. Revision: Comparing Growth Rates. O ( 2 n ). O ( n 3 ). O ( n 2 ). O ( n log n ). Faster Code. Sequential Search. O ( n ). O (log n ). Binary Search.

Download Presentation

Lecture 18: Selection Sort, Quicksort & Merge Sort

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CompSci 105 SS 2005 Principles of Computer Science Lecture 18: Selection Sort, Quicksort & Merge Sort Lecturer: Santokh Singh

  2. Revision: Comparing Growth Rates O(2n) O(n3) O(n2) O(n log n) Faster Code Sequential Search O(n) O(log n) Binary Search O(1) Textbook, p. 378

  3. Selection Sort 0 1 2 3 4 5 6 7 C O M P U T E R • How does this algorithm execute the instructions? Show the steps on the next page. • What is it’s Big O notation? Textbook, p. 385-6

  4. 0 1 2 3 4 5 6 7 C O M P U T E R Java Code, Textbook, p. 386-7

  5. Selection Sort 0 1 2 3 4 5 6 7 C O M P U T E R • What is Bubble Sort? • Is there a more efficient way than using Selection Sort or Bubble Sort? Selection Sort Analysis, Textbook, p. 387-388

  6. Algorithm Efficiency Analysing Search Algorithms Limits of Big O Analysis Sorting Selection Sort Merge Sort Algorithm Analysis Quick Sort Algorithm Analysis

  7. Merging Sorted Lists (L11.4) 0 1 2 3 C M O P 0 1 2 3 E R T U

  8. Merge Sort 0 1 2 3 4 5 6 7 C O M P U T E R Analysis, Textbook, p. 393-398

  9. C M E O P R Merge Sort U T Analysis, Textbook, p. 393-398

  10. 4 2 2 2 1 1 1 1 1 1 Merge Sort 8 items 4 2 1 1 Analysis, Textbook, p. 393-398

  11. Algorithm Efficiency Analysing Search Algorithms Limits of Big O Analysis Sorting Selection Sort Merge Sort Algorithm Analysis Quick Sort Algorithm Analysis

  12. Partitioning (as seen in L8) ≥p p <p 0 1 2 3 4 8 3 9 1 7 3 1 7 8 9 Textbook, p. 399

  13. Assignment 3 private BinaryTreeNode parseTree(){ /*This is the method that helps you to create a binary tree (BinaryTreeNode) data structure. Return null if the TextField where the users put in the expression is empty. Otherwise store the expression into the binary tree data structure. If there are an invalid input i.e. input other than (x,y,z,1,2,3,+,-,*,/) then an error message should be displayed on the command window. */ return null; }

  14. private BinaryTreeNode parseTree(){//recursive method to construct a BinaryTreeNode tree from // textString that was input, e.g. +12. if (textString.length() > 0) { BinaryTreeNode result, left, right; char c = textString.charAt(0); textString = textString.substring(1, textString.length()); //when do we create the children of the tree: if(…) { result = new BinaryTreeNode(“” + c); result.setLeft(…) //recursion… result.setRight(…) //recursion… return result; } //when do we create the leaves of the tree: else if(...) { return new BinaryTreeNode("" + c); } else { // error System.out.println("Parse error: invalid character: " + c); return …; // return null } } else // return null }

More Related