COMP6598 - Week 9 - Sorting
COMP6598 - Week 9 - Sorting
Introduction to Programming
Sorting
Week 9
Session 13
Acknowledgement
These slides have been adapted from
Introduction to Java Programming. 9ed. D.
Liang. 2013. Chapter 25
Learning Outcomes
• LO 4: Apply basic sorting algorithms in
solving problem
Lecture Outline :
• Sorting Definition
• Bubble Sort
• Selection Sort
• Insertion Sort
• Merge Sort
• Exercise
SORTING DEFINITION
Sorting Definition
• The data to be sorted might be integers, doubles, characters, or
objects
• There are three reasons to study sorting algorithms.
– sorting algorithms illustrate many creative approaches to problem solving,
and these approaches can be applied to solve other problems.
– sorting algorithms are good for practicing fundamental programming
techniques using selection statements, loops, methods, and arrays.
– sorting algorithms are excellent examples to demonstrate algorithm
performance.
• Sorting Order :
– Ascending : sorting from small to big
– Descending : sorting from big to small
Sorting Algorithm
• Basic sorting algorithm:
– Bubble Sort
– Insertion Sort
– Selection Sort
• Advanced sorting algorithm:
– Merge Sort
– Quick Sort
– Bucket Sort
– Shell Sort
– Radix Sort
– External Sort
BUBBLE SORT
Bubble Sort
• The bubble sort algorithm makes several passes through the array.
• Steps :
– On each pass, successive neighboring pairs are compared.
– If a pair is in decreasing order, its values are swapped; otherwise, the
values remain unchanged (ascending)
– After the first pass, the last element becomes the largest in the array.
– After the second pass, the second-to-last element becomes the second
largest in the array. This process is continued until all elements are sorted
• Ascending , At round:
– 1, array at 1 (index 0) is a smallest value
– 2, array at 2 (index 1) is a second smallest value
– n-1, array at n (index n-1) is a biggest value
• Number of round = n-1
Bubble Sort
Algorithm in Java
Sample Bubble Sort
Algorithm in Java
Improved Bubble Sort
Algorithm
Simulation of Bubble Sort
(ascending)
• Before Sorting : 5 6 1 -2 3 2
• Pass 1
5 6 1 -2 3 2 compare 5 and 6
5 6 1 -2 3 2 compare 6 and 1, swap
5 1 6 -2 3 2 compare 6 and -2, swap
5 1 -2 6 3 2 compare 6 and 3, swap
5 1 -2 3 6 2 compare 6 and 2, swap
• Result of Pass 1
5 1 -2 3 2 6
Simulation of Bubble Sort
• Pass 2
5 1 -2 3 2 6 compare 5 and 1, swap
1 5 -2 3 2 6 compare 5 and -2, swap
1 -2 5 3 2 6 compare 5 and 3, swap
1 -2 3 5 2 6 compare 5 and 2, swap
• Result of Pass 2
1 -2 3 2 5 6
Simulation of Bubble Sort
• Pass 3
1 -2 3 2 5 6 compare 1 and -2, swap
-2 1 3 2 5 6 compare 1 and 3
-2 1 3 2 5 6 compare 3 and 2, swap
• Result of Pass 3
-2 1 2 3 5 6
Simulation of Bubble Sort
• Pass 4
-2 1 2 3 5 6 compare -2 and 1
-2 1 2 3 5 6 compare 1 and 2
• Result of Pass 4
-2 1 2 3 5 6
Simulation of Bubble Sort
• Pass 5
-2 1 2 3 5 6 compare -2 and 1
• Result of Pass 5
-2 1 2 3 5 6
• After Sorting -2 1 2 3 5 6
SELECTION SORTING
Selection Sort
• Selection sort finds the smallest number (ascending
order) in the list and swaps it with the first element. It
then finds the smallest number remaining and swaps it
with the second element, and so on, until only a single
number remains
• At round:
– 1, array at 1 (index 0) is a small value
– 2, array at 2 (index 1) is a second small value
– n-1, array at n (index n-1) is a biggest value
• Number of round = n-1
Selection Sort
Algorithm in Java
Selection Sort Simulation
• Before Sebelum Sorting: 5 6 1 -2 3 2
• 1st Pass :
currentMin = 5 , currentMinIndex = 0
5 6 1 -2 3 2 currentMin > list[1] : 5 > 6 , currentMin= 5, currentMinIndex= 0
5 6 1 -2 3 2 currentMin > list[2] : 5 > 1, currentMin= 1, currentMinIndex = 2
5 6 1 -2 3 2 currentMin > list[3] : 1 > -2, currentMin= -2, currentMinIndex=3
5 6 1 -2 3 2 currentMin > list[4] : -2 > 3, currentMin= -2, currentMinIndex=3
5 6 1 -2 3 2 currentMin > list[5] : -2 > 2, currentMin = -2, currentMinIndex=3
5 6 1 -2 3 2 tukar list[currentMinIndex] dengan list[0]
-2 6 1 5 3 2 hasil putaran 1
Selection Sort Simulation
• 2nd Pass :
currentMin = 6, currentMinIndex = 1
-2 6 1 5 3 2 currentMin > list[2] : 6 > 1, currentMin=1, currentMinIndex=2
-2 6 1 5 3 2 currentMin > list[3] : 1 > 5, currentMin=1, currentMinIndex=2
-2 6 1 5 3 2 currentMin > list[4] : 1 > 3, currentMin=1, currentMinIndex=2
-2 6 1 5 3 2 currentMin > list[5] : 1 > 2, currentMin=1, currentMinIndex=2
-2 6 1 5 3 2 tukar list[currentMinIndex] dengan list[1]
-2 1 6 5 3 2 hasil putaran 2
Selection Sort Simulation
• 3rd Pass
currentMin = 6, currentMinIndex = 2
-2 1 6 5 3 2 currentMin > list[3] : 6 > 5, currentMin=5, currentMinIndex=3
-2 1 6 5 3 2 currentMin > list[4] : 5 > 3, currentMin=3, currentMinIndex=4
-2 1 6 5 3 2 currentMin > list[5] : 3 > 2, currentMin=2, currentMinIndex=5
-2 1 6 5 3 2 tukar list[currentMinIndex] dengan list[2]
-2 1 2 5 3 6 hasil putaran 3
Selection Sort Simulation
• 4th Pass
currentMin = 5, currentMinIndex = 3
-2 1 2 5 3 6 currentMin > list[4] : 5 > 3, currentMin=3, currentMinIndex=4
-2 1 2 5 3 6 currentMin > list[5] : 3 > 6, currentMin=3, currentMinIndex=4
-2 1 2 5 3 6 tukar list[currentMinIndex] dengan list[3]
-2 1 2 3 5 6 hasil putaran 4
Selection Sort Simulation
• 5th Pass
currentMin = 5, currentMinIndex = 4
-2 1 2 3 5 6 currentMin > list[5] : 5 > 6, currentMin=5, currentMinIndex=4
-2 1 2 3 5 6 tidak ada pertukaran
-2 1 2 3 5 6 hasil putaran 5
• Hasil sorting : -2 1 2 3 5 6
INSERTION SORTING
Insertion Sort
• The insertion-sort algorithm sorts a list of values by
repeatedly inserting a new element into a sorted
sublist until the whole list is sorted.
• Every round did not produce the biggest value or
the smallest value.
• Number of round = n-1
Insertion algorithm in
JAVA
Insertion Sort Simulation
• Before Sorting :
50 6 1 -2 3 2 Initially, the sorted sublist
contains the first element in
• Pass 1: the list. Save 6 to a temporary
variable currentElement
[0] [1] [2] [3] [4] [5]
50 6 1 -2 3 2
Split
2 3 2 5 6 1 -2 3
Split
2 3 2 5 6 1 -2 3
Split
2 3 2 5 6 1 -2 3
merge
2 3 2 5 1 6 -2 3
merge
2 2 3 5 -2 1 3 6
merge
-2 1 2 2 3 3 5 6
EXERCISE
Exercise
1. Build the simulation of :
a) Bubble Sort
b) Insertion Sort
c) Selection Sort
d) Merge Sort
Using this following list of numbers :
20, -10, 20. 99. 201, 67, 89
2. According to the simulation above, write down the result of the third
round of each sorting algorithm.
REFERENCES
References
• Introduction to Java Programming. 9ed. Liang. 2013. Chapter 25
• Bubble Sort Algorithm in Java.
• http://www.geekpedia.com/tutorial272_Bubble-Sort-Algorithm-in-Java.html
• Merge Sort Algorithm in Java :
• http://www.vogella.com/tutorials/JavaAlgorithmsMergesort/article.html
• https://www.cs.cmu.edu/~
adamchik/15-121/lectures/Sorting%20Algorithms/code/MergeSort.java
• http://www.java2novice.com/java-sorting-algorithms/merge-sort/
• Insertion Sort Algorithm in Java
• http://
courses.cs.washington.edu/courses/cse373/01wi/slides/Measurement/sld010.ht
m
• http://www.java2novice.com/java-interview-programs/insertion-sort/
• http://en.wikipedia.org/wiki/Insertion_sort
• Selection Sort Algorithm in Java
• http://www.java2novice.com/java-sorting-algorithms/selection-sort/
• http://www.algolist.net/Algorithms/Sorting/Selection_sort
• http://en.wikipedia.org/wiki/Selection_sort
Thank You