150 likes | 331 Views
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.
E N D
CompSci 105 SS 2005 Principles of Computer Science Lecture 18: Selection Sort, Quicksort & Merge Sort Lecturer: Santokh Singh
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
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
0 1 2 3 4 5 6 7 C O M P U T E R Java Code, Textbook, p. 386-7
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
Algorithm Efficiency Analysing Search Algorithms Limits of Big O Analysis Sorting Selection Sort Merge Sort Algorithm Analysis Quick Sort Algorithm Analysis
Merging Sorted Lists (L11.4) 0 1 2 3 C M O P 0 1 2 3 E R T U
Merge Sort 0 1 2 3 4 5 6 7 C O M P U T E R Analysis, Textbook, p. 393-398
C M E O P R Merge Sort U T Analysis, Textbook, p. 393-398
4 2 2 2 1 1 1 1 1 1 Merge Sort 8 items 4 2 1 1 Analysis, Textbook, p. 393-398
Algorithm Efficiency Analysing Search Algorithms Limits of Big O Analysis Sorting Selection Sort Merge Sort Algorithm Analysis Quick Sort Algorithm Analysis
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
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; }
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 }