Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2004 Pearson Addison-Wesley. All rights reserved March 10, 2006 Sorting ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:

Similar presentations


Presentation on theme: "© 2004 Pearson Addison-Wesley. All rights reserved March 10, 2006 Sorting ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:"— Presentation transcript:

1 © 2004 Pearson Addison-Wesley. All rights reserved March 10, 2006 Sorting ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor: Alexander Stoytchev

2 © 2004 Pearson Addison-Wesley. All rights reserved Midterm 2: Kildee Hall, room 125

3 © 2004 Pearson Addison-Wesley. All rights reserved Tuesday Mar 21 @ 6:30 pm

4 © 2004 Pearson Addison-Wesley. All rights reserved Midterm Format Covers Sections 1-5 & 7 Also, Searching and Sorting Format same as Midterm 1

5 © 2004 Pearson Addison-Wesley. All rights reserved Midterm Format True/False(10 x 1p each = 10p) Short answer( 5 x 2p each = 10p) Code Snippets( 5 x 3p each = 15p) Other Stuff( 3 x 5p each = 15p) Program 1(15p) Program 2(15p) Program 3(15p) Program 4(20p) TOTAL (130p)

6 © 2004 Pearson Addison-Wesley. All rights reserved Midterm Format You don’t need to get all 130 points to get an A 100 is a 100 You must get at least 65 points in order to pass this exam

7 © 2004 Pearson Addison-Wesley. All rights reserved Quick review of last lecture

8 © 2004 Pearson Addison-Wesley. All rights reserved Arrays in Java Java represents 2D arrays as an array of arrays! In other words, a 2D integer array is really a 1D array of references to 1D integer arrays. The concept generalizes to N-dimensions

9 © 2004 Pearson Addison-Wesley. All rights reserved Anatomy of a 2D Array [http://www.willamette.edu/~gorr/classes/cs231/lectures/chapter9/arrays2d.htm]

10 © 2004 Pearson Addison-Wesley. All rights reserved Example of a regular 2D array [http://livedocs.macromedia.com/coldfusion/5.0/Developing_ColdFusion_Applications/arrayStruct2.htm] Note: In Java the first index should be 0 not 1!

11 © 2004 Pearson Addison-Wesley. All rights reserved Example of a Ragged Array [http://livedocs.macromedia.com/coldfusion/5.0/Developing_ColdFusion_Applications/arrayStruct2.htm] Note: In Java the first index should be 0 not 1!

12 © 2004 Pearson Addison-Wesley. All rights reserved Example: Ragged2Darray.java

13 © 2004 Pearson Addison-Wesley. All rights reserved Binary Search At each step it splits the remaining array elements into two groups Therefore, it is faster than the linear search Works only on an already SORTED array Thus, there is a performance penalty for sorting the array [http://web.ics.purdue.edu/~cs154/lectures/lecture011.htm]

14 © 2004 Pearson Addison-Wesley. All rights reserved [http://web.ics.purdue.edu/~cs154/lectures/lecture011.htm] Example: Successful Binary Search

15 © 2004 Pearson Addison-Wesley. All rights reserved Example: BinarySearch.java

16 © 2004 Pearson Addison-Wesley. All rights reserved

17 Analysis of Searching Methods For an array of size n Sequential Search (Average-Case) n/2 Sequential Search (Worst-Case) n Binary Search (Average-Case) log(n)/2 Binary Search (Worst-Case) log(n)

18 Sorting Not in the Textbook

19 © 2004 Pearson Addison-Wesley. All rights reserved Insertion Sort [http://web.ics.purdue.edu/~cs154/lectures/lecture010.htm] 0

20 © 2004 Pearson Addison-Wesley. All rights reserved [http://web.ics.purdue.edu/~cs154/lectures/lecture010.htm] Example: Insertion Sort

21 © 2004 Pearson Addison-Wesley. All rights reserved Animations of Sorting Algoritms http://maven.smith.edu/~thiebaut/java/sort/demo.html http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html

22 © 2004 Pearson Addison-Wesley. All rights reserved Animations for Insertion Sort [http://maven.smith.edu/~thiebaut/java/sort/demo.html]

23 © 2004 Pearson Addison-Wesley. All rights reserved Example: InsertionSort.java

24 © 2004 Pearson Addison-Wesley. All rights reserved Selection Sort [http://web.ics.purdue.edu/~cs154/lectures/lecture010.htm]

25 © 2004 Pearson Addison-Wesley. All rights reserved Example: Selection Sort [http://web.ics.purdue.edu/~cs154/lectures/lecture010.htm]

26 © 2004 Pearson Addison-Wesley. All rights reserved Example: SelectionSort.java

27 © 2004 Pearson Addison-Wesley. All rights reserved Bubble Sort [http://web.ics.purdue.edu/~cs154/lectures/lecture010.htm] 0

28 © 2004 Pearson Addison-Wesley. All rights reserved [http://web.ics.purdue.edu/~cs154/lectures/lecture010.htm] Example: Bubble Sort

29 © 2004 Pearson Addison-Wesley. All rights reserved Example: BubbleSort.java

30 © 2004 Pearson Addison-Wesley. All rights reserved Analysis: all three run in O(n 2 ) time [http://linux.wku.edu/~lamonml/algor/sort/sort.html]

31 © 2004 Pearson Addison-Wesley. All rights reserved Analysis There are faster sorting algorithms  Heap sort  Quick sort  Merge Sort We will not cover those but feel free to study them on your own. They run in O(n log n) time.

32 © 2004 Pearson Addison-Wesley. All rights reserved O(n log n) sorting algorithms [http://linux.wku.edu/~lamonml/algor/sort/sort.html]

33 © 2004 Pearson Addison-Wesley. All rights reserved THE END


Download ppt "© 2004 Pearson Addison-Wesley. All rights reserved March 10, 2006 Sorting ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:"

Similar presentations


Ads by Google