Title: Zabin Visram Room CS115
1Zabin Visram Room CS115
- Lecture Structure
- Summary of simple sorts
- Complex Sorts
- Merge Sort
2Summary of simple sorts
- Bubble sort (or exchange sort) is generally not a
good practical method of sorting. - For random data in an array
- Selection sort requires fewer moves than bubble
sort but a constant number of comparisons. - Thus if the elements are large and costly to move
but have small sort keys then selection sort is
likely to give best performance.
3Summary of simple sorts
- If the elements are easy to move - but have
complex sort keys - additional comparisons
involved in selection become significant, - then
insertion is likely to be better. - You can compare insertion sort and selection
sort and bubble sort by demonstration - http//www.cosc.canterbury.ac.nz/people/mukundan/
dsal/appldsal.html - In a linked list the data are not moved while
sorting and so insertion sort, which minimises
the number of comparisons, is preferable.
4Merge Sort
- MergeSort is a sorting algorithm which produces a
sorted sequence by sorting its two halves and
merging them. - MergeSort algorithm is based on a divide and
conquer strategy. - First the sequence to be sorted is decomposed
into two halves (Divide). - Each half is sorted independently (Conquer).
- Then the two sorted halves are merged to a sorted
sequence (Combine)
5Divide Conquer Strategy
- The divide conquer strategy for solving a
problem consists of three steps - 1) Divide the problem is decomposed into
subproblems - 2) Conquer the subproblems are solved
- 3) Combine - the solutions of the subproblem are
recombined to the solution of the original problem
Divide Conquer Combine
Merge(n)
Mergesort(n/2)
Mergesort(n/2)
Merge sort(n)
6Merge Sort Divide Conquer Algorithm
- The divide conquer technique is used to sort a
list. - The algorithm partitions the lists into two
sublists,sorts the sublists, and then combines
the sorted sublists into one sorted list
7Merge Sort overview
- consider the idea of subdividing the sorting of a
long list into sorting shorter lists - and then put these together in a way that keeps
the combined list still sorted. - The essential idea of merge sort is to make
repeated use of a method which merges two lists
(each of which is already sorted into ascending
order) into a third list in ascending order.
80
3
5
7
9
0
2
4
6
2
New Merged array
3
3
5
7
9
0
2
4
6
3
5
7
9
0
2
4
6
9Brief overview of method
- The basic merge method compares the first items
in the two (already sorted) lists and places the
lower of the two in the new list. - One of the original lists now has a new lowest
value and this is again compared with the lowest
of the other list, and the lower of the two
placed next in the new list. - This process is repeated until one of the lists
is exhausted. - Then the remaining values from the other list
are placed in order in the new list and the merge
is completed. - It is a divide and conquer algorithm has a
recursive version
10- The merge sort chops the list into two sublists
of nearly equal size - For example
- List 35 28 18 45 62 48 30 38
- The merge sort algorithm partitions this list
into two sublists - First sublist 35 28 18 45
- Second sublist 62 48 30 38
- The two sublists are sorted using the same
algorithm (i.e merge sort) - First sublist 18 28 35 45
- Second sublist 30 38 48 62
- Next the merge sort algorithm combines that is
merges the two sorted sublists into one sorted
list
11Merge Sort Algorithm - RECURSION
35 28 18 45 62 48 30 38
split
If the list is of a size greater than 1 1)
Divide the list into two sublists 2) Merge sort
the first sublist 3) Merge sort the second
sublist 4) Merge the first sublist and the second
sublist Thus we are using recursion to
implement the merge sort algorithm
35 28 18 45
62 48 30 38
split
split
35 28
18 45
62 48
30 38
split
split
split
split
35
28
18
45
62
48
30
38
merge
merge
18 45
28 35
merge
merge
30 38
48 62
merge
18 28 35 45
merge
30 38 48 62
merge
18 28 30 35 38 45 48 62
12Merge Sort Array based
- to sort an array we can divide the array into
two subarrays of equal length - And finally merge the two subarrays
13Another example or Merge Sort
split
Values are recursively split into unsorted lists
that are then recursively merged into ascending
order
0 1 58 3 42 4
40 2 1 43 3 65
40 2 1 43 3 65 0 1 58 3 42 4
40 2 1 43 3 65 0 1 58 3 42 4
2
65
3
1
-1
58
42
4
Merge
40 1 2 43 3 65 0 1 58 3 4 42
1 2 40 3 43 65 1 0 58 3 4 42
1 2 3 40 43 65 1 0 3 4 42 58
-1 0 1 2 3 3 4 40 42 43 58 65
14Task in pairs -Trace the steps that a merge
sort takes when sorting the following array into
ascending order9 6 2 4 8 7 5 3
15- Answer
- 9 6 2 4 8 7 5 3
- 9 6 2 4 8 7 5 3
- 9 6 2 4 8 7 5 3
- 6 9 2 4 7 8 3 5
- 2 4 6 9 3 5 7 8
- 2 3 4 5 6 7 8 9
16Demonstration
- http//www.cosc.canterbury.ac.nz/people/mukundan/d
sal/MSort.html