Data Structures and Algorithms

1 / 95

Data Structures and Algorithms - PowerPoint PPT Presentation

Data Structures and Algorithms

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. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

1. Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert Moyer, Montgomery County Community College

2. Sorting means . . . • The values stored in an array have keys of a type for which the relational operators are defined. (We also assume unique keys.) • Sorting rearranges the elements into either ascending or descending order within the array. (We’ll use ascending order.)

3. Straight Selection Sort values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] Divides the array into two parts: already sorted, and not yet sorted. On each pass, finds the smallest of the unsorted elements, and swaps it into its correct place,thereby increasing the number of sorted elements by one. 36 24 10 6 12

4. Selection Sort: Pass One values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] 36 24 10 6 12 U N S O R T E D

5. SORTED Selection Sort: End Pass One values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] 6 24 10 36 12 U N S O R T E D

6. SORTED Selection Sort: Pass Two values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] 6 24 10 36 12 U N S O R T E D

7. SORTED Selection Sort: End Pass Two values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] 6 10 24 36 12 U N S O R T E D

8. SORTED Selection Sort: Pass Three values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] 6 10 24 36 12 U N S O R T E D

9. UNSORTED Selection Sort: End Pass Three values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] S O R T E D 6 10 12 36 24

10. UNSORTED Selection Sort: Pass Four values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] S O R T E D 6 10 12 36 24

11. Selection Sort: End Pass Four values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] 6 10 12 24 36 S O R T E D

12. Selection Sort: How many comparisons? values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] 6 10 12 24 36 4 compares for values[0] 3 compares for values[1] 2 compares for values[2] 1 compare for values[3] = 4 + 3 + 2 + 1

13. For selection sort in general • The number of comparisons when the array contains N elements is Sum = (N-1) + (N-2) + . . . + 2 + 1

14. Notice that . . . Sum = (N-1) + (N-2) + . . . + 2 + 1 + Sum = 1 + 2 + . . . + (N-2) + (N-1) 2* Sum = N + N + . . . + N + N 2 * Sum = N * (N-1) Sum = N * (N-1) 2

15. For selection sort in general • The number of comparisons when the array contains N elements is Sum = (N-1) + (N-2) + . . . + 2 + 1 Sum = N * (N-1) /2 Sum = .5 N2 - .5 N Sum = O(N2)

16. template <class ItemType > int MinIndex ( ItemType values [ ] , int start , int end ) // Post: Function value = index of the smallest value in // values [start] . . values [end]. { int indexOfMin = start ; for ( int index = start + 1 ; index <= end ; index++ ) if ( values [ index ] < values [ indexOfMin ] ) indexOfMin = index ; return indexOfMin; } 16

17. template <class ItemType > void SelectionSort ( ItemType values [ ] , int numValues ) // Post: Sorts array values[0 . . numValues-1 ] into ascending // order by key { int endIndex = numValues - 1 ; for ( int current = 0 ; current < endIndex ; current++ ) Swap ( values [ current ] , values [ MinIndex ( values, current, endIndex ) ] ) ; } 17

18. Bubble Sort values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] Compares neighboring pairs of array elements, starting with the last array element, and swaps neighbors whenever they are not in correct order. On each pass, this causes the smallest element to “bubble up” to its correct place in the array. 36 24 10 6 12

19. template <class ItemType > void BubbleUp ( ItemType values [ ] , int start , int end ) // Post: Neighboring elements that were out of order have been // swapped between values [start] and values [end], // beginning at values [end]. { for ( int index = end ; index > start ; index-- ) if (values [ index ] < values [ index - 1 ] ) Swap ( values [ index ], values [ index - 1 ] ) ; } 19

20. template <class ItemType > void BubbleSort ( ItemType values [ ] , int numValues ) // Post: Sorts array values[0 . . numValues-1 ] into ascending // order by key { int current = 0 ; while ( current < numValues - 1 ) BubbleUp ( values , current , numValues - 1 ) ; current++ ; } 20

21. Insertion Sort values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] One by one, each as yet unsorted array element is inserted into its proper place with respect to the already sorted elements. On each pass, this causes the number of already sorted elements to increase by one. 36 24 10 6 12

22. 24 10 6 Insertion Sort Works like someone who “inserts” one more card at a time into a hand of cards that are already sorted. To insert 12, we need to make room for it by moving first 36 and then 24. 36 12

23. 24 10 6 Insertion Sort Works like someone who “inserts” one more card at a time into a hand of cards that are already sorted. To insert 12, we need to make room for it by moving first 36 and then 24. 36 12

24. 24 36 Insertion Sort Works like someone who “inserts” one more card at a time into a hand of cards that are already sorted. To insert 12, we need to make room for it by moving first 36 and then 24. 10 6 12

25. 24 36 Insertion Sort Works like someone who “inserts” one more card at a time into a hand of cards that are already sorted. To insert 12, we need to make room for it by moving first 36 and then 24. 12 10 6

26. template <class ItemType > void InsertItem ( ItemType values [ ] , int start , int end ) // Post: Elements between values [start] and values [end] // have been sorted into ascending order by key. { bool finished = false ; int current = end ; bool moreToSearch = ( current != start ) ; while ( moreToSearch && !finished ) { if (values [ current ] < values [ current - 1 ] ) { Swap ( values [ current ], values [ current - 1 ] ) ; current-- ; moreToSearch = ( current != start ); } else finished = true ; } } 26

27. template <class ItemType > void InsertionSort ( ItemType values [ ] , int numValues ) // Post: Sorts array values[0 . . numValues-1 ] into ascending // order by key { for ( int count = 0 ; count < numValues ; count++ ) InsertItem ( values , 0 , count ) ; } 27

28. Sorting Algorithms and Average Case Number of Comparisons Simple Sorts • Straight Selection Sort • Bubble Sort • Insertion Sort More Complex Sorts • Quick Sort • Merge Sort • Heap Sort O(N2) O(N*log N) 28

29. Recall that . . . A heap is a binary tree that satisfies these special SHAPE and ORDER properties: • Its shape must be a complete binary tree. • For each node in the heap, the value stored in that node is greater than or equal to the value in each of its children.

30. 70 The largest element in a heap is always found in the root node root 12 60 40 30 8 10

31. 70 0 30 4 10 6 The heap can be stored in an array values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 70 60 12 40 30 8 10 12 2 60 1 40 3 8 5

32. Heap Sort Approach • First, make the unsorted array into a heap by satisfying the order property. Then repeat the steps below until there are no more unsorted elements. • Take the root (maximum) element off the heap by swapping it into its correct place in the array at the end of the unsorted elements. • Reheap the remaining unsorted elements. (This puts the next-largest element into the root position).

33. 70 0 30 4 10 6 After creating the original heap values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 70 60 12 40 30 8 10 12 2 60 1 40 3 8 5

34. 70 0 30 4 10 6 Swap root element into last place in unsorted array values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 70 60 12 40 30 8 10 12 2 60 1 40 3 8 5

35. 10 0 30 4 After swapping root element into its place values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 10 60 12 40 30 8 70 12 2 60 1 40 3 8 5 70 6 NO NEED TO CONSIDER AGAIN

36. 60 0 30 4 After reheaping remaining unsorted elements values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 60 40 12 10 30 8 70 12 2 40 1 10 3 8 5 70 6

37. 60 0 30 4 Swap root element into last place in unsorted array values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 60 40 12 10 30 8 70 12 2 40 1 10 3 8 5 70 6

38. 8 0 30 4 NO NEED TO CONSIDER AGAIN After swapping root element into its place values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 8 40 12 10 30 60 70 12 2 40 1 10 3 60 5 70 6

39. 40 0 6 4 After reheaping remaining unsorted elements values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 40 30 12 10 6 60 70 30 1 12 2 10 3 60 5 70 6

40. 40 0 6 4 Swap root element into last place in unsorted array values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 40 30 12 10 6 60 70 30 1 12 2 10 3 60 5 70 6

41. 6 0 NO NEED TO CONSIDER AGAIN After swapping root element into its place values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 6 30 12 10 40 60 70 30 1 12 2 10 3 60 5 70 6 40 4

42. 30 0 After reheaping remaining unsorted elements values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 30 10 12 6 40 60 70 10 1 12 2 6 3 60 5 70 6 40 4

43. 30 0 Swap root element into last place in unsorted array values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 30 10 12 6 40 60 70 10 1 12 2 6 3 60 5 70 6 40 4

44. 6 0 NO NEED TO CONSIDER AGAIN After swapping root element into its place values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 6 10 12 30 40 60 70 10 1 12 2 30 3 60 5 70 6 40 4

45. 12 0 After reheaping remaining unsorted elements values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 12 10 6 30 40 60 70 10 1 6 2 30 3 60 5 70 6 40 4

46. 12 0 Swap root element into last place in unsorted array values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 12 10 6 30 40 60 70 10 1 6 2 30 3 60 5 70 6 40 4

47. 6 0 NO NEED TO CONSIDER AGAIN After swapping root element into its place values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 6 10 12 30 40 60 70 10 1 12 2 30 3 60 5 70 6 40 4

48. 10 0 After reheaping remaining unsorted elements values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 10 6 12 30 40 60 70 6 1 12 2 30 3 60 5 70 6 40 4

49. 10 0 Swap root element into last place in unsorted array values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 10 6 12 30 40 60 70 6 1 12 2 30 3 60 5 70 6 40 4

50. After swapping root element into its place values root [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 6 10 12 30 40 60 70 6 0 10 1 12 2 30 3 60 5 70 6 40 4 ALL ELEMENTS ARE SORTED
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}