Dsa24 11
Dsa24 11
Sorting Algorithms
2
van Gogh’s
The Starry
Night
No 1 No 2
Da Vinci’s Mona Lisa Vermeer’s Girl with
a Pearl Earring No 4
§ Sorting problem
What is an algorithm?
§ Selection sort An algorithm is a finite
§ Review of straight selection sort sequence of well-
defined, computer-
§ Heap sort
implementable
§ Exchange sort instructions, typically to
solve a class of
§ Review of straight bubble sort
problems or to perform
§ Quick sort a computation.
§ Insertion sort
§ Review of straight insertion sort
§ Shell sort
General Sort Concepts Data can be
any type
Linear
based
Tree
based
convertible • Heap
• BST – AVL tree
• M-way tree – B-tree
Algorithm Complexity of Simple Sorting
Comparisons moves
(worst case) (worst case)
§ The actual steps of processing can be less than that of the worst
case as given above; e.g. if a pass with no exchanges in bubble
sort, the procedure can be terminated immediately.
§ Some sorting algorithms can be more efficient than others on
nearly sorted data.
9
Selection sort
§ Selection sort in ascending order
§ Select the smallest element from
the array and swap with the
element at the 1st position.
§ Select the second smallest
element from the rest of the
array and swap with the element
at the 2nd position
§ Keep doing it until finishing the
whole array.
§ The complexity of selection sort is
O(n2).
https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
11
Heap
§ A heap (max/min heap) is a binary tree with the following
properties:
§ The tree is complete or nearly complete
§ The key value of root node of the tree is greater/less than
or equal to the key value in each of its descendants
§ All the subtrees are also heaps
£ £
13
Heap Sort 23 56 45 8 78 32
Heap sort process
Step 1: Convert the array of unsorted data
into a heap by repeatedly using reheapUP.
Step 2: Exchange the root (the largest
number) with the last element.
Step 3: Reduce the size of the array by 1
and reheapDown the new array.
Step 4: Do the same with the new array.
78 56
32 56 32 45
8 23 45 8 23
45 32
32 23 8 23
8 45 56 78
14
Heap Sort
void heapSort(int list[], int last){
int sorted, holdData, walker;
//Create a heap
for (walker = 1; walker <= last; walker++)
reheapUp (list, walker);
https://www.cs.usfca.edu/~galles/visualization/Comparison
Sort.html
Quick Sort
§ Divide and Conquer:
§ Partition the original list into two partitions with a pivot: all
elements in the left partition are less than the pivot and all
elements in the right partition are greater than or equal to the
pivot.
§ Recursively apply the approach to each partition until the whole
list is sorted.
³
Quick Sort
§ Select an element P, called the pivot, from the list. The pivot can be
selected arbitrarily but better to be in the middle and easy to calculate
(e.g. the median value of the left, the right, and the middle element).
§ Arrange the list into two partitions: the elements less than P are put in
the left partition; the elements greater than or equal to P are put in
the right partition.
Left partition P right partition
The partition is done by using two pointers that move from each side towards
the middle. Once the left pointer finds an element larger than P and the right
point finds an element smaller than P, swap the data in the pointers. Once
two pointers meet, place P in between by a swap.
62 74 14 97 87 22 21 85 76 45 84 78
Quick Sort: pivot and partition
Compare the first, middle
and last to determine a
pivot (using bubble sort)
22<62<78, thus 62 is
the pivot, put in the first
position temporarily
>
>
Divide and
conquer for
quick sort
>
Whole Process of Quick Sort
first last
middle
The way of
choosing pivots:
the median of
first, middle and
last elements
Recursive
processing
https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
24
Complexity of Quicksort
t a ff e c ts
b a d p iv o
A e n c y
eff i c i
Comparison of Sorting Algorithms
Data Size Bubble Sort Selection Insertion Shell Sort Quick Sort
10,000 2,198 943 615 45 0
15,000 4,895 2,159 1,378 55 10
20,000 8,985 3,883 2,478 99 11
25,000 13,898 6,037 3,895 156 5
30,000 23,356 11,270 6,773 213 27
35,000 15,045 8,304 280 44
40,000 12,912 356 51
50,000 578 43
100,000 2,127 109
500,000 560
1,000,000 1,775
sorted unsorted
Insertion Sort Example
Insertion Sort Example
void insertionSort (int list[], int last) {
for (int i =1; i <= last; i++) { //start from the second element
int hold = list[i];
int walker = i - 1;
while ( walker>=0 && hold < list[walker] ) {
list[walker+1] = list[walker];
walker = walker - 1;
}
list[walker+1]=hold;
}
} // insertionSort
sorted unsorted
Problems of insertion sort
§ If a small item is on the far right, to move the item
to its proper place on the left, all the intervening
items (between where it is and where it should be)
must be shifted one space right. This is close to n
copies, just for one item. The average number of
item moves are n/2. Therefore the performance of
insertion sort is O(n2).
https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
Shell Sort
§ Given a sequence of unsorted values, the Shell sort will sort separate k sub-
sequences, where k is an integer greater than 2, but no more than half of the size
of the array, called the increment, each segment is sorted using insertion sort
method
n=16
s1 s2 s3 s4 s1 s2 s3 s4 s1 s2 s3 s4 s1 s2 s3 s4
k=4
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Shell Sort Example
77 21
62 80
14 25
9 70
30 55
before
21 77
62 80
14 25
n=10, k=5: five segments; two elements in one segment 9 70
30 55
after
Still inserting one by one but doing it in groups
Every second in a group
Shell Sort example (Cont.)
• Data were randomly generated. The complexity of Shell sort relies on the
• Ran on Pentium III 800 laptop. choice of increments but something close
• Time unit is millisecond. to O(n3/2) to O(n5/3) in average.
37
Summary
§ Search algorithms may require sorted data
§ Several sorting algorithms available
§ Selection sort, insertion sort, Shellsort, quicksort and
heapsort
§ Complexity of sorting algorithms:
Reading
§ Read textbook Chapter 10
§ Visualization of sorting algorithms:
§ https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.ht
ml
§ http://www.sorting-algorithms.com
§ https://www.toptal.com/developers/sorting-algorithms
§ http://www.youtube.com/watch?v=kPRA0W1kECg
§ Download example code from vUWS and read it.
§ Complete your solution for Practical 9. All work must be checked
within this week. There is no further practical sessions for checking
your practical work after this week
§ You must demonstrate your assignment 2 solution in next week
(Week 13) to your tutor. No demonstration no marks.