Sorting - PowerPoint PPT Presentation

About This Presentation
Title:

Sorting

Description:

Main idea is to keep the keys in two list: sorted and unsorted. ... Example. 11/26/09. BR. 12. Performance of Merge and Quick sorts. Merge sort is an O(n log n) ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 13
Provided by: kumarm8
Learn more at: https://cse.buffalo.edu
Category:
Tags: keys | sorting

less

Transcript and Presenter's Notes

Title: Sorting


1
Sorting
  • B.Ramamurthy

2
Introduction
  • The problem of sorting an initially unsorted
    collection of keys to produce an ordered
    collection is one of the richest in computer
    science.
  • The richness derives from the fact that there are
    a number of ways of solving the sorting problem.
  • It is a fascinating operation that provides a
    model for investigating many problem in Computer
    Science.

3
Topics for Discussion
  • Sorting Basics
  • Priority queue methods
  • Insertion based methods
  • Divide and conquer methods
  • Summary

4
Comparison-based Sorting
  • Most sorting methods decide how to rearrange the
    keys into order by first comparing the values of
    the keys.These methods are called
    comparison-based methods.
  • Examples Selection sort, insertion sort, merge
    sort, quick sort, heap sort, shell sort and tree
    sort.
  • The minimum average number of comparisons
    required to sort n keys using a comparison-based
    sorting method is proportional to n log n.

5
Priority Queue Sorting Methods
  • Using a tree heap and delete, repeat.
  • Selection sort A realization of PQ method
    unsorted sub-array of keys represents the PQ.

6
Selection Sort
  • Number of comparisons (n-1)(n-2)2 1
    n(n-1)/2, or O(n )
  • Number of exchanges O(n)
  • Therefore it is a O(n ) algorithm.

2
2
7
Heap Sort
  • The PQ part of the method is represented by a
    heap.
  • This implies that every time PQ part has be
    re-heaped when a key is moved. This is because
    the PQ may loose its heap property when the root
    is removed.

8
Heap Sort Performance
  • Heaping takes O(n)
  • Remove key, move and sift up O(n log n)
  • Since the latter is dominant for a large n,
  • Heap sort is an O(n log n) algorithm.

9
Insertion-based methods
  • Main idea is to keep the keys in two list sorted
    and unsorted. Sorting involves removing an
    element from the unsorted list and placing it in
    the sorted list in the right order.
  • Especially useful when the complete list is not
    available apriori.
  • Insertion sort (O(n ) ) and tree sort (O (n log
    n)) are examples.

10
Divide and Conquer methods
  • Main idea
  • 1. Divide the initially given unsorted array into
    two sub-arrays
  • 2. Sort the two subarrays
  • 3. Combine the two sorted subarrays into the
    overall solution.
  • Merge sort and Quick sort are examples of this
    method. Lets look at these with a sample list.

11
Example
12
Performance of Merge and Quick sorts
  • Merge sort is an O(n log n)
  • Quick sort in average case is O(n log n) but is
    O(n ) in the worst case.

2
Write a Comment
User Comments (0)
About PowerShow.com