Sorting Analysis - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Sorting Analysis

Description:

the most efficient, other than Radix? On the average, Mergesort is not quite as fast ... Radix sort looks attractive. But what's not represented by it's order of ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 11
Provided by: anni65
Category:

less

Transcript and Presenter's Notes

Title: Sorting Analysis


1
Sorting Analysis
2
Sorts to Consider
  • Selection sort
  • Bubble sort
  • Insertion sort
  • Mergesort
  • Quicksort
  • Radixsort
  • Why do we care about sorting?

3
Selection sort
One of the most intuitive sorts Find the largest
item, and swap it with the item currently in the
last position. Repeat this step on the
unsorted part of the collection. initial array
29 10 14 37 13 after 1st swap 29 10 14 13
37 after 2nd swap 13 10 14 29 37 after 3rd
swap 13 10 14 29 37 after 4th swap 10 13 14
29 37 Algorithm for (last n-1 last gt 1
--last) find the index of the largest
item swap the largest item with the last
item Order of magnitude O(n2)
4
Bubble sort
Not a particularly good algorithm, but
simple. Compare adjacent items and exchange them
if they are out of order. Repeat. The largest
item will eventually bubble to the correct
position. Pass 1 Pass 2 29 10 14 37 13 10 14
29 13 37 10 29 14 37 13 10 14 29 13 37 10 14 29
37 13 10 14 13 29 37 10 14 29 37 13 10 14 29 13
37 Algorithm Champion challenger with a
swap Worst case O(n2) Average case O(n2) Best
case (already sorted) O(n) only if the
algorithm keeps track of the number of swaps
5
Insertion sort
Imagine arranging a hand of cards, where you pick
up one card at a time and insert into its proper
position. For arrays of data, partition the
array into a sorted region and an unsorted
region. Initial Array 29 10 14 37 13 Sorted
Unsorted Action 29 10 14 37 13 Copy 10 29
29 14 37 13 Shift 29 10 29 14 37 13 Insert
10, copy 14 10 29 29 37 13 Shift 29 10 14 29 37
13 Insert 14, copy 37, Insert 37 10 14 29 37
13 Copy 13 10 14 14 29 37 Shift 14, 29, 37 10
13 14 29 37 Insert 13 Algorithm Nested for
loops Average case O(n2)
6
Mergesort
Divide and Conquerrecursive solution. Divide
the collection in half , sort each half, then
merge. Requires the use of a temporary array Ori
ginal array 8 1 4 32 Split 8 1 4 3 2 Sort
1 4 8 2 3 Merge 1 2 3 4 8 Algorithm Merges
ort the first half of the array Mergesort the
second half of the array Merge the sorted
halves Average case O(n logn)
7
Quicksort
Another recursive divide and conquer
sorting solution. Partitions the collection by
establishing a pivot point. On one side of the
pivot are the numbers that are greater than the
pivot and the other side contains the numbers
that are less than the pivot. Original array
5 3 6 7 4 Establish 5 as the pivot 5 3 6 7 4
5 3 6 7 4 5 3 6 7 4 5 3 6 7
4 5 3 4 6 7 3 4 5 6 7 pivot inserted
Algorithm Select pivot and partition Quicksort(
S1 region of A) Quicksort(S2 region of A)
8
Radix sort
Uses the idea of forming groups and then
combining them. The sort treats each data item
as a character string. Original Collection ABC,
XYZ, BWZ, AAC, RLT, JBX, RDT Grouped by
rightmost letter (ABC, AAC) (RLT, RDT) (JBX)
(XYZ, BWZ) Next (AAC) (ABC, JBX) (RDT) (RLT)
(BWZ) (XYZ) Last AAC, ABC, BWZ, JBX, RDT, RLT,
XYZ Algorithm Nested for loop Order of
magnitude O(n)
9
Sorting Growth Rate Summary
Algorithm Worst Case Average Case Selection
sort n2 n2 Bubble sort n2 n2 Insertion
sort n2 n2 Mergesort n logn nlogn Quicksort n2
nlogn Radix sort n n
Why not always use Mergesorton paper its the
most efficient, other than Radix? On the
average, Mergesort is not quite as fast as
quicksort. Mergesort also requires extra
storage equal to the size of the array to
be sorted.
10
Why bother with Selection sort, Bubble sort, and
Insertion sort? They can suffice for smaller
data sets. Radix sort looks attractive. But
whats not represented by its order of magnitude
is the number of buckets needed to be created.
For sorting numerical data, there are 10 buckets.
For sorting string data, there are 26 buckets.
Radix is not appropriate when using a more
complex sort key.
Write a Comment
User Comments (0)
About PowerShow.com