Sorting - PowerPoint PPT Presentation

1 / 113
About This Presentation
Title:

Sorting

Description:

Loops through input and 'selects' smallest/largest value and swaps with current ... http://cg.scs.carleton.ca/~morin/misc/sortalg/ Sorting. 112. Summary Slide 1 ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 114
Provided by: BMorr1
Category:
Tags: carleton | sorting

less

Transcript and Presenter's Notes

Title: Sorting


1
Sorting Recursion
  • Briana B. Morrison
  • Adapted from Alan Eugenio
  • William J. Collins

2
Topics
  • Review
  • Insertion Sort
  • Selection Sort
  • Bubble Sort
  • This term
  • Tree Sort
  • Heap Sort
  • Radix Sort
  • New
  • Merge Sort
  • Quick Sort

3
(No Transcript)
4
(No Transcript)
5
(No Transcript)
6
(No Transcript)
7
(No Transcript)
8
(No Transcript)
9
(No Transcript)
10
(No Transcript)
11
(No Transcript)
12
(No Transcript)
13
(No Transcript)
14
Selection Sort
  • Loops through input and selects
    smallest/largest value and swaps with current
    location
  • Worst case O(n2)
  • Average case O(n2)

15
(No Transcript)
16
(No Transcript)
17
(No Transcript)
18
(No Transcript)
19
(No Transcript)
20
(No Transcript)
21
Bubble Sort
  • Loops through input doing adjacent comparisons
  • Biggest/smallest element bubble to top/bottom
  • Worst case O(n2)
  • Average case O(n2)

22
(No Transcript)
23
(No Transcript)
24
(No Transcript)
25
(No Transcript)
26
(No Transcript)
27
(No Transcript)
28
(No Transcript)
29
(No Transcript)
30
(No Transcript)
31
(No Transcript)
32
(No Transcript)
33
(No Transcript)
34
(No Transcript)
35
(No Transcript)
36
(No Transcript)
37
(No Transcript)
38
(No Transcript)
39
(No Transcript)
40
(No Transcript)
41
(No Transcript)
42
(No Transcript)
43
(No Transcript)
44
(No Transcript)
45
(No Transcript)
46
(No Transcript)
47
(No Transcript)
48
  FOR TREE SORT, EACH INSERTION
TAKES LOGARITHMIC TIME AT MOST, SO FOR
n INSERTIONS worstTime(n) IS O(n log n).
therefore averageTime(n) IS O(n log
n)    
 
49
(No Transcript)
50
(No Transcript)
51
(No Transcript)
52
(No Transcript)
53
(No Transcript)
54
Heapify done.
55
Sort done.
56
(No Transcript)
57
Radix Sort
  • Recall that Radix Sort uses a vector of queues to
    distribute and then collect from least
    significant position to most significant
    position.
  • Performance is O(nd) where d is the length of
    the key ( of digits)
  • Remember that Radix Sort IS NOT a comparison
    based sort.

58
Divide Conquer Sorts
  • Divide and conquer is a problem solving technique
    that uses recursion
  • Divide problem into smaller problems that lead to
    recursive step or stopping condition
  • Combine solutions to solve original
  • Usually have 2 or more recursive calls that
    involve disjoint collections

59
Mergesort vs. Quicksort
  • Mergesort
  • Divides collection to smallest solvable problem
  • Combines solutions (this is where the work is)
  • Quicksort
  • Divides collection by a pivot (this is where the
    work is)
  • Combines solutions

60
(No Transcript)
61
(No Transcript)
62
The Merge Algorithm Example
  • The merge algorithm takes a sequence of elements
    in a vector v having index range first, last).
    The sequence consists of two ordered sublists
    separated by an intermediate index, called mid.

63
The Merge Algorithm (Cont)

64
The Merge Algorithm (Cont)
65
The Merge Algorithm (Cont)
66
(No Transcript)
67
(No Transcript)
68
(No Transcript)
69
(No Transcript)
70
(No Transcript)
71
(No Transcript)
72
(No Transcript)
73
(No Transcript)
74
(No Transcript)
75
(No Transcript)
76
Partitioning Merging in mergeSort()
77
Function calls in mergeSort()
78
(No Transcript)
79
(No Transcript)
80
(No Transcript)
81
(No Transcript)
82
(No Transcript)
83
(No Transcript)
84
(No Transcript)
85
(No Transcript)
86
Quicksort Example
  • The quicksort algorithm uses a series of
    recursive calls to partition a list into smaller
    and smaller sublists about a value called the
    pivot.
  • Example Let v be a vector containing 10 integer
    values
  • v 800, 150, 300, 650, 550, 500, 400, 350,
    450, 900
  • Easy ways to choose the pivot
  • First value
  • Middle value
  • Last value

87
Quicksort Example
  • Choose pivot as middle value, swap with first
    value.
  • Example Let v be a vector containing 10 integer
    values
  • v 800, 150, 300, 650, 550, 500, 400, 350,
    450, 900

88
Quicksort Example (Cont)
89
Quicksort Example (Cont)
90
Quicksort Example (Cont)
91
  • Now swap the pivot with the value pointed to by
    scanDown
  • Then generate the recursive calls

92
Quicksort Example (Cont)
93
Quicksort Example (Cont)
94
Quicksort Example (Cont)
95
Quicksort Example (Cont)
96
(No Transcript)
97
(No Transcript)
98
(No Transcript)
99
(No Transcript)
100
(No Transcript)
101
(No Transcript)
102
(No Transcript)
103
(No Transcript)
104
(No Transcript)
105
(No Transcript)
106
Lets See Them Run
  • // load the vectors with the same sequence of
    100000
  • // random numbers in the range 0 to 999999
  • for(i0i lt VECTORSIZEi)
  • rndNum rnd.random(1000000)
  • v1.push_back(rndNum)
  • v2.push_back(rndNum)
  • v3.push_back(rndNum)
  • v4.push_back(rndNum)
  • timeSort(v1,HEAPSORT,"Heap sort")
  • timeSort(v2,MERGESORT,"Merge sort")
  • timeSort(v3,QUICKSORT,"Quick sort")
  • timeSort(v4,INSERTIONSORT,"Insertion sort")

107
(No Transcript)
108
(No Transcript)
109
(No Transcript)
110
(No Transcript)
111
Sorting Animations
  • http//www.cs.hope.edu/alganim/animator/Animator.
    html
  • www2.dcc.ufmg.br/dorgival/applets/SortingPoints/S
    ortingPoints.html
  • http//cg.scs.carleton.ca/morin/misc/sortalg/

112
Summary Slide 1
- Divide-and-Conquer Algorithms - splits a
problem into subproblems and works on each
part separately - Two type of
divide-and-conquer strategy 1) mergesort
algorithm - Split the range of
elements to be sorted in half, sort each
half, and then merge the sorted sublists
together. - running time O(n
log2n), requires the use of an auxiliary
vector to perform the merge steps.
113
Summary Slide 2
2) quicksort algorithm - uses a
partitioning strategy that finds the final
location of a pivot element within an
interval first,last). - The
pivot splits the interval into two parts, first,
pivotIndex), pivotIndex, last). All elements
in the lower interval have values ? pivot
and all elements in the upper interval have
values ? pivot. - running time
O(n log2n) - worst case of O(n2),
highly unlikely to occur
Write a Comment
User Comments (0)
About PowerShow.com