Sorting Searching Ch 9 Chapter 9 Sorting 1

  • Slides: 21
Download presentation
Sorting & Searching Ch. # 9 Chapter 9: Sorting 1

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

Chapter Outline Ø What is sorting and complexity of sorting Ø Different types of

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 Chapter 9: Sorting 2

What is Sorting and review of complexity? Sorting: an operation that segregates items into

What is Sorting and review of complexity? Sorting: an operation that segregates items into groups according to specified criterion. A={3162134590} A={0112334569} 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: Sorting 3

Complexity in general, measures the algorithms efficiency in internal factors such as the time

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: Sorting 4

Selection Sort • A relatively easy to understand algorithm • Sorts an array in

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) Chapter 9: Sorting 5

Selection Sort Example 35 65 30 60 20 20 65 30 60 35 20

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

Insertion Sort • Based on technique of card players to arrange a hand •

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 Chapter 9: Sorting 7

Insertion Sort • Place ith item in proper position: • temp = data[i] •

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 Chapter 9: Sorting 8

Insertion Sort Example Chapter 9: Sorting 9

Insertion Sort Example Chapter 9: Sorting 9

Analysis of Insertion Sort • Maximum number of comparisons: O(n 2) • In the

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 Chapter 9: Sorting 10

Merge Sort • A merge is a common data processing operation: • Performed on

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 Chapter 9: Sorting 11

Merge Algorithm (Two Sequences) Merging two sequences: 1. Access the first item from both

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 Chapter 10: Sorting 12

Picture of Merge Chapter 9: Sorting 13

Picture of Merge Chapter 9: Sorting 13

Analysis of Merge • Two input sequences, total length n elements • Must move

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) Chapter 10: Sorting 14

Heapsort • Merge sort time is O(n log n) • But requires (temporarily) n

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 Chapter 10: Sorting 15

Heapsort Picture Chapter 10: Sorting 16

Heapsort Picture Chapter 10: Sorting 16

Heapsort Picture (2) Chapter 10: Sorting 17

Heapsort Picture (2) Chapter 10: Sorting 17

Algorithm for In-Place Heapsort • Build heap starting from unsorted array • While the

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 Chapter 10: Sorting 18

Quicksort • Developed in 1962 by C. A. R. Hoare • Given a pivot

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) Chapter 10: Sorting 19

Quicksort Example Chapter 10: Sorting 20

Quicksort Example Chapter 10: Sorting 20

Chapter Summary (2) Chapter 10: Sorting 21

Chapter Summary (2) Chapter 10: Sorting 21