Title: Heapsort
1Heapsort
include ltiostream.hgt // Provides cout and
cin include ltstdlib.hgt // Provides
EXIT_SUCCESS, size_t // PROTOTYPES of the
functions used in this test program void
heapsort(int data , size_t n) size_t
parent(size_t k) size_t left_child(size_t
k) size_t right_child(size_t k) void
make_heap(int data , size_t n) void
reheapify_down(int data , size_t n) void swap
(int x, int y)
2 int main( ) const char BLANK ' '
const size_t ARRAY_SIZE 10 // No. of elements
in the array to be sorted int
dataARRAY_SIZE // Array of integers
to be sorted int user_input
// Number typed by the user size_t
number_of_elements // How much of the array
is used size_t i //
Array index // Provide some instructions
cout ltlt "Please type up to " ltlt ARRAY_SIZE ltlt "
positive integers." cout ltlt "Indicate the
list's end with a zero." ltlt endl // Read
the input numbers number_of_elements 0
cin gtgt user_input while ((user_input ! 0)
(number_of_elements lt ARRAY_SIZE))
datanumber_of_elements user_input
number_of_elements cin gtgt user_input
// Sort the numbers and print the
result with two blanks after each number
heapsort(data, number_of_elements) cout ltlt
"In sorted order, your numbers are " ltlt endl
for (i 0 i lt number_of_elements i)
cout ltlt datai ltlt BLANK ltlt BLANK cout ltlt
endl return EXIT_SUCCESS
3 void heapsort(int data , size_t n)
size_t unsorted make_heap(data, n)
unsorted n while (unsorted gt 1)
unsorted-- swap(data0,
dataunsorted) reheapify_down(data,
unsorted)
4size_t parent(size_t k) // Library facilities
used stdlib.h return (k-1)/2 size_t
left_child(size_t k) // Library facilities used
stdlib.h return 2k 1 size_t
right_child(size_t k) // Library facilities used
stdlib.h return 2k 2
5void make_heap(int data , size_t n)
size_t i // Index of next element to be added
to heap size_t k // Index of new element as
it is being pushed upward for (i 1 i lt n
i) k i while ((k gt 0)
(datak gt dataparent(k)))
swap(datak, dataparent(k)) k
parent(k)
6void reheapify_down(int data , size_t n)
size_t current // Index of the node
that's moving down size_t big_child_index
// Index of the larger child that's moving down
bool heap_ok false // Will change to true
when the heap is correct current 0 while
((!heap_ok) (left_child(current) lt n))
// Compute the index of the larger child
if (right_child(current) gt n)
// There is no right child, so left child must be
largest big_child_index
left_child(current) else if
(dataleft_child(current) gt dataright_child(curr
ent)) // The left child is the
bigger of the two children
big_child_index left_child(current)
else // The right child is the bigger
of the two children big_child_index
right_child(current) // Larger child is
bigger than the current node? If so, then swap
// the current node with its bigger child
and continue otherwise we are done. if
(datacurrent lt databig_child_index)
swap(datacurrent,
databig_child_index) current
big_child_index else
heap_ok true
7Last slide of Heapsort
void swap (int x, int y) int temp
temp x x y y temp
8Quicksort
include ltiostream.hgt // Provides cout and
cin include ltstdlib.hgt // Provides
EXIT_SUCCESS, size_t // PROTOTYPES of the
functions used in this test program void
quicksort(int a, int first, int last) int
partition(int a, int first, int last) void
swap (int x, int y)
9 int main( ) const char BLANK ' '
const int ARRAY_SIZE 10 // No. of elements in
the array to be sorted int dataARRAY_SIZE
// Array of integers to be sorted int
user_input // Number typed by the
user int number_of_elements // How much
of the array is used int i
// Array index // Provide some
instructions cout ltlt "Please type up to " ltlt
ARRAY_SIZE ltlt " positive integers." cout ltlt
"Indicate the list's end with a zero." ltlt endl
// Read the input numbers
number_of_elements 0 cin gtgt user_input
while ((user_input ! 0) (number_of_elements
lt ARRAY_SIZE)) datanumber_of_elemen
ts user_input number_of_elements
cin gtgt user_input // Sort the
numbers and print the result with two blanks
after each number quicksort (data, 0,
number_of_elements-1) cout ltlt "In sorted
order, your numbers are " ltlt endl for (i
0 i lt number_of_elements i) cout ltlt
datai ltlt BLANK ltlt BLANK cout ltlt endl
return EXIT_SUCCESS
10int partition(int a, int first, int last)
int pivot afirst int lastS1 first
int firstUnknown first 1 while
(firstUnknown lt last) if
(afirstUnknown lt pivot)
lastS1 swap(afirstUnknown,
alastS1) firstUnknown
swap(afirst, alastS1) return
lastS1 void quicksort(int a, int first,
int last) if (first lt last) int
pivotIndex partition(a, first, last)
quicksort(a, first, pivotIndex - 1)
quicksort(a, pivotIndex 1, last)
11Last slide of Quicksort
void swap (int x, int y) int temp
temp x x y y temp
12Big Oh Notation
Definition A theoretical measure of the
execution of an algorithm, usually the time or
memory needed, given the problem size n, which is
usually the number of items. Informally, saying
some equation f(n) O(g(n)) means it is less
than some constant multiple of g(n). The notation
is read, "f of n is big oh of g of n". Formal
Definition f(n) O(g(n)) means there are
positive constants c and k, such that 0 f(n)
cg(n) for all n k. The values of c and k must
be constants and must not depend on n.