CSI-402
DATA STRUCTURE AND ALGORITHMS
SORTING ALGORITHMS
W H AT I S SORT I NG AL G OR I T HM ?
• Sorting algorithms are a set of instructions that take an array or list
as an input and arrange the items into a particular order.
• Sorts are most common in numerical or a form of alphabetical order
and can be in ascending (A-Z, 0-9) or descending (Z-A, 9-0) order.
2
I MPORTANCE OF SORT I NG AL G OR IT H MS
• They can often reduce the complexity of a problem and sorting
algorithms are very important in computer science.
• These algorithms have direct applications in searching algorithms,
database algorithms, divide and conquer methods, data structure
algorithms, and many more.
3
COMMON SORT I NG AL G OR I TH MS
• Insertion sort
• Selection sort
• Bubble sort
• Divide and Conquer
• Merge sort
• Quick sort
4
SORT I NG CR I T E RI A
• The two main criteria to judge which sorting algorithm is better
than the other are:
• Time taken to sort the given data.
• Memory space required to do so
5
I NSE RT I ON SORT
• A simple sorting algorithm that works similar to the way you
sort playing cards in your hands.
• The array is virtually split into a sorted and an unsorted part.
• Values from the unsorted part are picked and placed at the
correct position in the sorted part.
6
I NSE RT I ON SORT
To insert 12, we need to make room for it by moving first 36 and then 24.
7
I NSE RT I ON SORT
8
I NSE RT I ON SORT
9
I NSE RT I ON SORT
Sorted Unsorted
23 78 45 8 32 56 Original List
After pass 1
23 78 45 8 32 56
23 45 78 8 32 56 After pass 2
After pass 3
8 23 45 78 32 56
After pass 4
8 23 32 45 78 56
After pass 5
8 23 32 45 56 78 10
I NSE RT I ON SORT AL G OR I T H M
11
SE L E CT ION SORT
• Selection sort is a simple and efficient sorting algorithm that works
by repeatedly selecting the smallest (or largest) element.
• This algorithm finds location of smallest element in unsorted
portion of list
• And moves it to the top of the unsorted portion of list.
• The first time, we locate the smallest item in the entire list.
• The second time, we locate smallest item in list starting from
second element in the list,
12
SE L E CT ION SORT
13
SE L E CT ION SORT
14
SE L E CT ION SORT
Sorted Unsorted
23 78 45 8 32 56 Original List
After pass 1
8 78 45 23 32 56
8 23 45 78 32 56 After pass 2
After pass 3
8 23 32 78 45 56
After pass 4
8 23 32 45 78 56
After pass 5
8 23 32 45 56 78
15
SE L E CT ION SORT A L G OR I TH M
16
B UB B L E SORT
• It is most straightforward sorting algorithm that functions by
repeatedly switching nearby elements if they are in the wrong order.
• Large datasets should not be used with this approach due to its high
average and worst-case time complexity.
17
B UB B L E SORT
• Suppose list[0]...list[n - 1] is a list of n elements, indexed 0 to n – 1
Bubble sort algorithm:
– In a series of n - 1 iterations, compare successive elements,
list[index] and list[index + 1]
– If list[index] is greater than list[index + 1] then swap them
18
B UB B L E SORT
19
B UB B L E SORT
20
B UB B L E SORT AL G OR I T HM
for i n-1 down to 1 do
for j 0 to i-1 do
if (s[j] > s[j+1] )
swap( s[j], s[j+1] )
21
DI V I DE AND CONQUE R
Recursive in structure
⬧ Divide the problem into sub-problems that are similar to the
original but smaller in size
⬧ Conquer the sub-problems by solving them recursively. If they
are small enough, just solve them in a straightforward manner.
⬧ Combine solutions to create a solution to original problem
• Given the collection of n elements to sort and perform the sort in
three steps
• Divide step: split the collection S into two subsets, S1 and S2
• Recursion step: sort S1 and S2 separately
• Conquer step: combine the two lists into one sorted list
22
DI V I DE AND CONQUE R
a problem of size n
subproblem 1 subproblem 2
of size n/2 of size n/2
a solution to a solution to
subproblem 1 subproblem 2
a solution to
the original problem
23
DI V I DE AND CONQUE R
• Divide and conquer can be sued successfully in parallel computation;
• Derive efficient parallel algorithms by using divide and conquer
algorithm.
Two algorithms adopt this divide-and-conquer strategy
Merge sort
• Divide step is trivial – just split the list into two equal parts
• Work is carried out in the conquer step by merging two
sorted lists
Quick sort
• Work is carried out in the divide step using a pivot element
• Conquer step is trivial
24
ME R G E SORT
• Sorting Problem: Sort a sequence of n elements into ascending
order.
Divide: Divide the n-element sequence to be sorted into two
subsequences of n/2 elements each
Conquer: Sort two subsequences recursively using merge sort.
Combine: Merge two sorted subsequences to produce the
sorted answer.
25
ME R G E SORT
To mergesort an array:
⬧ split the array in half
⬧ mergesort those smaller arrays
⬧ merge the two sorted arrays
Note it does option 1
⬧ split is easy but combining them at the end needs work
26
ME R G ING
The key to Merge Sort is merging two sorted lists into one, such that if
you have two lists X (x1x2…xm) and Y(y1y2…yn) the
resulting list is Z(z1z2…zm+n)
Example:
L1 = { 3 8 9 } L2 = { 1 5 7 }
merge(L1, L2) = { 1 3 5 7 8 9 }
27
ME R G E SORT E XAMP L E
28
ME R G E SORT AL G OR ITHM
Algorithm mergeSort(S, C)
Input sequence S with n elements, comparator C
Output sequence S sorted according to C
if S.size() > 1
(S1, S2) partition(S, n/2)
mergeSort(S1, C)
mergeSort(S2, C)
S merge(S1, S2)
29
QUI CK SORT
Pick a number randomly and start arranging all numbers less &
greater/equal than the picked number on left and right respectively and
do the same until you sort all array.
30
QUI CK SORT
To understand quick-sort, look at a high-level description of algorithm
1) Divide: If sequence S has 2 or more elements, select an element x from
S to be your pivot. Any arbitrary element like last will do. Remove all
the elements of S and divide them into 3 sequences:
L, holds S’s elements less than x
E, holds S’s elements equal to x
G, holds S’s elements greater than x
2) Recurse: Recursively sort L and G
3) Conquer: Finally, to put elements back into S in order, first inserts
elements of L, then those of E, and those of G.
31
I DE A OF QUI CK SORT
1) Select: pick an element
2) Divide: rearrange elements so that x goes
to its final position E
2) Recurse and Conquer: recursively
sort
32
QUI CK SORT T R E E
33
QUI CK SORT T R E E
34
QUI CK SORT T R E E
35
QUI CK SORT T R E E
36
QUI CK SORT T R E E
37
QUI CK SORT T R E E
38
QUI CK SORT T R E E
39
QUI CK SORT T R E E
40
QUI CK SORT T R E E
41
QUI CK SORT T R E E
42
QUI CK SORT T R E E
43
QUI CK SORT T R E E
44
QU I C K SORT T R E E
45
END