0% found this document useful (0 votes)
51 views

COMP6598 - Week 9 - Sorting

The document summarizes several sorting algorithms: bubble sort, selection sort, insertion sort, and merge sort. It provides descriptions of how each algorithm works, examples of pseudocode implementations, and walkthroughs of example sorts. The key aspects covered are that bubble sort makes multiple passes through the array swapping neighboring elements, selection sort finds the minimum element on each pass and selection sort inserts elements into the sorted portion of the array. Merge sort divides the array in half and recursively sorts and merges the halves.

Uploaded by

Ketut Suanta
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

COMP6598 - Week 9 - Sorting

The document summarizes several sorting algorithms: bubble sort, selection sort, insertion sort, and merge sort. It provides descriptions of how each algorithm works, examples of pseudocode implementations, and walkthroughs of example sorts. The key aspects covered are that bubble sort makes multiple passes through the array swapping neighboring elements, selection sort finds the minimum element on each pass and selection sort inserts elements into the sorted portion of the array. Merge sort divides the array in half and recursively sorts and merges the halves.

Uploaded by

Ketut Suanta
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

COMP6598

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

[0] [1] [2] [3] [4] [5] Move list[0] to list[1]


50 1 -2 3 2

[0] [1] [2] [3] [4] [5] Assign currentElement to list[0]


6 50 1 -2 3 2
Insertion Sort Simulation
Pass 2
Save 1 to to a temporary
[0] [1] [2] [3] [4] [5] variable currentElement
6 50 1 -2 3 2

[0] [1] [2] [3] [4] [5] Move list[1] to list[2]


6 50 -2 3 2

[0] [1] [2] [3] [4] [5] Move list[0] to list[1]


6 50 -2 3 2

[0] [1] [2] [3] [4] [5]


Assign currentElement to list[0]
1 6 50 -2 3 2
Insertion Sort Simulation
Pass 3
[0] [1] [2] [3] [4] [5] Save -2 to to a temporary
1 6 50 -2 3 2 variable currentElement

[0] [1] [2] [3] [4] [5]


Move list[2] to list[3]
1 6 50 3 2

[0] [1] [2] [3] [4] [5] Move list[1] to list[2]


1 6 50 3 2

[0] [1] [2] [3] [4] [5] Move list[0] to list[1]


1 6 50 3 2

[0] [1] [2] [3] [4] [5] Assign currentElement to list[0]


-2 1 6 50 3 2
Insertion Sort Simulation
Pass 4
[0] [1] [2] [3] [4] [5] Save 3 to to a temporary
-2 1 6 50 3 2 variable currentElement

[0] [1] [2] [3] [4] [5]


Move list[3] to list[4]
-2 1 6 50 2

[0] [1] [2] [3] [4] [5] Move list[2] to list[3]


-2 1 6 50 2

[0] [1] [2] [3] [4] [5] Assign currentElement to list[2]


-2 1 3 6 50 2
Insertion Sort Simulation
Pass 5
[0] [1] [2] [3] [4] [5] Save 2 to to a temporary
-2 1 3 6 50 2 variable currentElement

[0] [1] [2] [3] [4] [5]


Move list[4] to list[5]
-2 1 3 6 50

[0] [1] [2] [3] [4] [5] Move list[3] to list[4]


-2 1 3 6 50
[0] [1] [2] [3] [4] [5] Move list[2] to list[3]
-2 1 3 6 50

[0] [1] [2] [3] [4] [5]


Assign currentElement to list[2]
-2 1 2 3 6 50
MERGE SORT
Merge Sort
• The merge sort algorithm can be described recursively as follows: The
algorithm divides the array into two halves and applies a merge sort on
each half recursively .After the two halves are sorted, merge them.
• Algorithm of Merge Sort
public static void mergeSort(int[] list) {
if (list.length > 1) {
mergeSort(list[0 ... list.length / 2]);
mergeSort(list[list.length / 2 + 1 ... list.length]);
merge list[0 ... list.length / 2] with
list[list.length / 2 + 1 ... list.length];
}
}
Merge Sort Algorithm in
JAVA
Merge sort algorithm in
JAVA
Merge Sort algorithm in
JAVA
Merge sort Simulation
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

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

You might also like