Lecture 06
Lecture 06
(COMP4120)
Lecture # 6
Sorting Algorithms
Course Instructor
Dr. Aftab Akram
PhD CS
Assistant Professor
Department of Information Sciences, Division of Science &
Technology
University of Education, Lahore
Sorting Problem
• Given a set of records with key
values , the Sorting Problem is to
arrange the records into any order s such that
records have keys obeying the
property .
• In other words, the sorting problem is to arrange a
set of records so that the values of their key fields
are in non-decreasing order.
• A sorting algorithm is said to be stable if it does not
change the relative ordering of records with
identical key values.
Sorting Algorithms
• When comparing two sorting algorithms, the most
straightforward approach would seem to be simply
program both and measure their running times.
• However, such a comparison can be misleading because
the running time for many sorting algorithms depends
on specifics of the input values.
• In particular, the number of records, the size of the keys
and the records, the allowable range of the key values,
and the amount by which the input records are “out of
order” can all greatly affect the relative running times
for sorting algorithms.
Sorting Algorithms
• When analyzing sorting algorithms, it is traditional to
measure the number of comparisons made between
keys.
• This measure is usually closely related to the running
time for the algorithm and has the advantage of being
machine and datatype independent.
• However, in some cases records might be so large that
their physical movement might take a significant
fraction of the total running time.
• If so, it might be appropriate to measure the number of
swap operations performed by the algorithm.
Sorting Algorithms
• In most applications we can assume that all records
and keys are of fixed length, and that a single
comparison or a single swap operation requires a
constant amount of time regardless of which keys
are involved.
• Special cases
• Variable length keys
• Small number of records to be sorted
• Use as little memory as possible
Three Sorting Algorithms
• We will discuss three sorting algorithms:
• Insertion Sort
• Bubble Sort
• Selection Sort
Insertion Sort
• Insertion Sort iterates through a list of records.
• Each record is inserted in turn at the correct
position within a sorted list composed of those
records already processed.
• It is like sorting a pile of telephone bills by date.
• A fairly natural way to do this might be to look at
the first two bills and put them in order.
• Then take the third bill and put it into the right
order with respect to the first two, and so on.
• As you take each bill, you would add it to the sorted
pile that you have already made.
Insertion Sort
Insertion Sort
Insertion Sort
• Two loops are needed to implemented insertion
sort.
• The outer loop scans through the entire array.
• The inner loop compares arrays values, and swap
the values if needed.
• For example, if initially array was sorted from
highest to lowest values, and now we want to sort
it from lowest to highest, then each in each
iteration of outer loop, the number of swaps in
inner loop will be 1 for 1st iteration, 2 for 2nd, and so
on.
• This represents the worst case scenario.
Insertion Sort
• The total number of comparisons will be: