Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.

Similar presentations


Presentation on theme: "Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of."— Presentation transcript:

1 Chapter 9: Sorting1 Sorting & Searching Ch. # 9

2 Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of sorting and its complexity  Selection sort  Insertion sort  Merge sort  Radix sort  Heap sort  Quick sort  Binary search and insertion algorithm

3 What is Sorting and review of complexity? Sorting: an operation that segregates items into groups according to specified criterion. A = { 3 1 6 2 1 3 4 5 9 0 } A = { 0 1 1 2 3 3 4 5 6 9 } Most of the primary sorting algorithms run on different space and time complexity. Time Complexity is defined to be the time the computer takes to run a program (or algorithm in our case). Space complexity is defined to be the amount of memory the computer needs to run a program. Chapter 9: Sorting3

4 Complexity in general, measures the algorithms efficiency in internal factors such as the time needed to run an algorithm. External Factors (not related to complexity): Size of the input of the algorithm Speed of the Computer Quality of the Compiler Chapter 9: Sorting4

5 5 Selection Sort A relatively easy to understand algorithm Sorts an array in passes 1. select the smallest element among data[i]~ data[data.length-1]; 2. swap it with data[i]; 3. if not finishing, repeat 1&2 Efficiency is O(n 2 ), hence called a quadratic sort Performs: O(n 2 ) comparisons O(n) exchanges (swaps)

6 Chapter 9: Sorting6 Selection Sort Example 3565306020 scan 0-5, smallest 20 swap 35 and 20 2065306035scan 1-5, smallest 30 swap 65 and 30 2030656035scan 2-5, smallest 35 swap 65 and 35 2030356065scan 3-5, smallest 60 swap 60 and 60 2030356065done

7 Chapter 9: Sorting7 Insertion Sort Based on technique of card players to arrange a hand Player keeps cards picked up so far in sorted order When the player picks up a new card Makes room for the new card Then inserts it in its proper place

8 Chapter 9: Sorting8 Insertion Sort Place ith item in proper position: temp = data[i] shift those elements data[j] which greater than temp to right by one position place temp in its proper position

9 Chapter 9: Sorting9 Insertion Sort Example

10 Chapter 9: Sorting10 Analysis of Insertion Sort Maximum number of comparisons: O(n 2 ) In the best case, number of comparisons: O(n) # shifts for an insertion = # comparisons - 1 When new value smallest so far, # comparisons A shift in insertion sort moves only one item

11 Chapter 9: Sorting11 Merge Sort A merge is a common data processing operation: Performed on two sequences of data Items in both sequences use same compare To Both sequences in ordered of this compare To Goal: Combine the two sorted sequences in one larger sorted sequence Merge sort merges longer and longer sequences

12 Chapter 10: Sorting12 Merge Algorithm (Two Sequences) Merging two sequences: 1.Access the first item from both sequences 2.While neither sequence is finished 1.Compare the current items of both 2.Copy smaller current item to the output 3.Access next item from that input sequence 3.Copy any remaining from first sequence to output 4.Copy any remaining from second to output

13 Chapter 9: Sorting13 Picture of Merge

14 Chapter 10: Sorting14 Analysis of Merge Two input sequences, total length n elements Must move each element to the output Merge time is O(n) Must store both input and output sequences An array cannot be merged in place Additional space needed: O(n)

15 Chapter 10: Sorting15 Heapsort Merge sort time is O(n log n) But requires (temporarily) n extra storage items Heapsort Works in place: no additional storage Offers same O(n log n) performance Idea (not quite in-place): Insert each element into a priority queue Repeatedly remove from priority queue to array Array slots go from 0 to n-1

16 Chapter 10: Sorting16 Heapsort Picture

17 Chapter 10: Sorting17 Heapsort Picture (2)

18 Chapter 10: Sorting18 Algorithm for In-Place Heapsort Build heap starting from unsorted array While the heap is not empty Remove the first item from the heap: Swap it with the last item Restore the heap property

19 Chapter 10: Sorting19 Quicksort Developed in 1962 by C. A. R. Hoare Given a pivot value: Rearranges array into two parts: Left part  pivot value Right part > pivot value Average case for Quicksort is O(n log n) Worst case is O(n 2 )

20 Chapter 10: Sorting20 Quicksort Example

21 Chapter 10: Sorting21 Chapter Summary (2)


Download ppt "Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of."

Similar presentations


Ads by Google