Professional Documents
Culture Documents
Why sorting?
consider to be the most fundamental problem in
the study of algorithms
the need to sort information is inherent in an
application
Algorithms often use sorting as a key subroutine
Sorting classes
internal sorting algorithms
Sorting Algorithms
Insertion sort
Selection sort
Bubble sort (in exam!)
Sorting Algorithms
Comparison Sort
Merge sort
Heapsort, and
Quicksort
Stable
Quick View
Quick View
Sorting Algorithms
Mergesort
Quicksort
Heapsort
Quicksort
works recursively, by first selecting a random
pivot value from the array
Then it partitions the array into elements that are
less than and greater than the pivot
recursively sorts each part
Quicksort
This is considered an in-place sorting algorithm,
since it uses no other array storage
This algorithm is in the expected case, and in the
worst case.
Quicksort
Quicksort
Quicksort
Quicksort
Quicksort
Quicksort
Quicksort
Heapsort
based on a nice data structure, called a heap
supports the operations of inserting a key, and
deleting the element with the smallest key value
A heap can be built for n keys in time
the minimum key can be extracted in time
in-place sorting algorithm, but it is not stable
Heapsort
Heaps
Maintaining the heap property
Building a heap
The Heapsort algorithm
Heapsort
Heaps
Heapsort
Root is at A[i]
Heapsort
Order Property
min-heap
max-heap
Heapsort
Maintaining heap order:
Heapsort
Heapsort
Theorem:
Any comparison-based sorting algorithm has
worst-case running time