11 Sorting
11 Sorting
Sungmin Cha
New York University
10.09.2024
Outline
• Notice
• Sorting Algorithm
– Why sorting is necessary?
– Basic sorting algorithms
Bubble
Selection
Insertion sort
2
Outline
• Notice
• Sorting Algorithm
– Why sorting is necessary?
– Basic sorting algorithms
Bubble
Selection
Insertion sort
3
Notice
• HW1
– Grades will be published!
– The answer code of HW1 Part3 is in Ed Workspace
4
Notice
5
Notice
• HW2 is posted!
– Deadline: 11:59PM 10/27 Sunday
• HW2 Part 1
– Implementing Queue and Stack using an array and a linked list
– The template codes are in Ed Workspace
6
Notice
• HW2 Part 1
– ArrayStack / LinkedStack
Implement a stack using an array and linked list
– ArrayQueue / LinkedQueue
Implement a queue using a circular array and singly circular linked
list
– The template code has a similar form with HW1 Part 3
– Students should submit their code to Gradescope
7
Notice
• HW2 Part 2
– It is about Sorting Algorithms
• HW2 Part 3
– It is about implementing Heap
8
Outline
• Notice
• Sorting Algorithm
– Why sorting is necessary?
– Basic sorting algorithms
Bubble
Selection
Insertion sort
9
Sorting
• Sorting
– Arranging given data in a predefined order
– In the field of computer science,
It is common practice to sort data in either numerical or
lexicographical order.
10
Sorting
• Why is sorting necessary?
11
Sorting
• Why is sorting necessary? – for search!
– Computers typically handle data on the scale of millions or more
– Moreover, searching for data is constantly carried out
Log in to a website, searching an item on Amazon, etc.
12
Sorting
• Why is sorting necessary? – for search!
– Computers typically handle data on the scale of millions or more
– Moreover, searching for data is constantly carried out
Log in to a website, searching an item in Amazon, etc.
Query
15
13
Sorting
• How can the process of searching be conducted?
– Sequential search
Dataset
123 23 1 87 15 48 74
Comparing
15
Query
14
Sorting
• How can the process of searching be conducted?
– Sequential search
Dataset
123 23 1 87 15 48 74
Comparing
15
Query
15
Sorting
• How can the process of searching be conducted?
– Sequential search
Dataset
123 23 1 87 15 48 74
15
Query
16
Sorting
• How can the process of searching be conducted?
– Sequential search
Dataset
123 23 1 87 15 48 74
15
Query
Dataset
123 23 1 87 15 48 74
15
Query
Dataset
123 23 1 87 15 48 74
sorting
1 15 23 48 74 87 123
19
Sorting
• Binary search with the sorted data
Dataset (n-1) // 2
1 15 23 48 74 87 123
15 < 48
15
Query
20
Sorting
• Binary search with the sorted data
Dataset
1 15 23 48 74 87 123
15 == 15: matched!
15
Query
21
Sorting
• Binary search with the sorted data
Dataset
1 15 23 48 74 87 123
15 == 15: matched!
15
Query
22
Sorting
• Which one is more effective?
– To use binary search, the dataset is sorted whenever data is
added and removed
In most cases, querying (q) data overwhelmingly outweighs
insertion(i)/deletions(d) ( q >> i or d)
– (approximately) The total number of operations of each case
Sequential search: q * O(n)
Sorting + binary search: (i + d) * time complexity of sorting + q * O(log n)
23
Sorting
• Which one is more effective?
– (approximately) The total number of operations of each case
Sequential search: q * O(n)
Sorting + binary search: (i + d) * time complexity of sorting + q * O(log n)
q >> i or d
25
Bubble Sort
• Process of bubble sort (ascending order)
– Example: the first iteration
23 12 1 74 15 48 128
26
Bubble Sort
• Process of bubble sort (ascending order)
– Example: the first iteration
23 12 1 74 15 48 128
23 > 12
27
Bubble Sort
• Process of bubble sort (ascending order)
– Example: the first iteration
12 23 1 74 15 48 128
23 > 12
28
Bubble Sort
• Process of bubble sort (ascending order)
– Example: the first iteration
12 23 1 74 15 48 128
23 > 1
29
Bubble Sort
• Process of bubble sort (ascending order)
– Example: the first iteration
12 1 23 74 15 48 128
23 > 1
30
Bubble Sort
• Process of bubble sort (ascending order)
– Example: the first iteration
12 1 23 74 15 48 128
23 < 74
31
Bubble Sort
• Process of bubble sort (ascending order)
– Example: the first iteration
12 1 23 74 15 48 128
74 > 15
32
Bubble Sort
• Process of bubble sort (ascending order)
– Example: the first iteration
12 1 23 15 74 48 128
74 > 15
33
Bubble Sort
• Process of bubble sort (ascending order)
– Example: the first iteration
12 1 23 15 74 48 128
74 > 48
34
Bubble Sort
• Process of bubble sort (ascending order)
– Example: the first iteration
12 1 23 15 48 74 128
74 > 48
35
Bubble Sort
• Process of bubble sort (ascending order)
– Example: the first iteration
12 1 23 15 48 74 128
74 < 128
36
Bubble Sort
• Process of bubble sort (ascending order)
– Example: the first iteration
12 1 23 15 48 74 128
74 < 128
37
Bubble Sort
• Process of bubble sort (ascending order)
– Example: the first iteration
12 1 23 15 48 74 128
12 1 23 15 48 74 128
12 > 1
39
Bubble Sort
• Process of bubble sort (ascending order)
– Example: the second iteration
1 12 23 15 48 74 128
1 > 12
40
Bubble Sort
• Process of bubble sort (ascending order)
– Example: the second iteration
1 12 23 15 48 74 128
12 < 23
41
Bubble Sort
• Process of bubble sort (ascending order)
– Example: the second iteration
1 12 15 23 48 74 128
42
Bubble Sort
• Process of bubble sort (ascending order)
– Example: the third iteration
1 12 15 23 48 74 128
1 < 12
1 12 15 23 48 74 128
43
Bubble Sort
• Pseudo algorithm of bubble sort n-1 iterations
O(1)
44
Bubble Sort
• Pseudo algorithm of bubble sort n-1 iterations
O(1)
45
Bubble Sort
• Time complexity of Bubble sort in the best case
– Example: the first iteration
1 12 15 23 48 74 128
1 < 12
46
Bubble Sort
• Time complexity of Bubble sort in the best case
– Example: the first iteration
1 12 15 23 48 74 128
74 < 128
In the case where no element swaps
occur during the first iteration
47
Bubble Sort
• Time complexity of Bubble sort in the best case
– Example: the first iteration
1 12 15 23 48 74 128
74 < 128
48
Bubble Sort
• Time complexity of Bubble sort in the best case
– Example: the first iteration
1 12 15 23 48 74 128
74 < 128
23 12 1 74 15 128 48
50
Selection Sort
• Process of selection sort (ascending order)
– Locate the largest element in the array A[0, …, n-1] and
swap it with the last element A[n-1] of the array
Swap!
23 12 1 74 15 128 48
51
Selection Sort
• Process of selection sort (ascending order)
– Locate the largest element in the array A[0, …, n-1] and
swap it with the last element A[n-1] of the array
Swap!
23 12 1 74 15 48 128
52
Selection Sort
• Process of selection sort (ascending order)
– Locate the largest element in the array A[0, …, n-2]
23 12 1 74 15 48 128
53
Selection Sort
• Process of selection sort (ascending order)
– Locate the largest element in the array A[0, …, n-2] and
swap it with the last element A[n-2] of the array
Swap!
23 12 1 74 15 48 128
54
Selection Sort
• Process of selection sort (ascending order)
– Locate the largest element in the array A[0, …, n-2] and
swap it with the last element A[n-2] of the array
Swap!
23 12 1 48 15 74 128
55
Selection Sort
• Process of selection sort (ascending order)
– Locate the largest element in the array A[0, …, n-3]
23 12 1 48 15 74 128
56
Selection Sort
• Process of selection sort (ascending order)
– Locate the largest element in the array A[0, …, n-3] and
swap it with the last element A[n-3] of the array
Swap!
23 12 1 48 15 74 128
57
Selection Sort
• Process of selection sort (ascending order)
– Locate the largest element in the array A[0, …, n-3] and
swap it with the last element A[n-3] of the array
Swap!
23 12 1 15 48 74 128
58
Selection Sort
• Process of selection sort (ascending order)
– Locate the largest element in the array A[0, …, n-4]
23 12 1 15 48 74 128
59
Selection Sort
• Process of selection sort (ascending order)
– The final array after sorting is complete
1 12 15 23 48 74 128
60
Selection Sort
• Pseudo algorithm of selection sort
n-1 iterations Number of comparison
: (n-1) + (n-2) + … + 1 = n(n-1)/2
O(1)
61
Selection Sort
• Pseudo algorithm of selection sort
n-1 iterations Number of comparison
: (n-1) + (n-2) + … + 1 = n(n-1)/2
O(1)
62
Selection Sort
• Is there the best case in using selection sort?
1 12 15 23 48 74 128
63
Selection Sort
• Is there the best case in using selection sort?
1 12 15 23 48 74 128
64
Selection Sort
• Is there the best case in using selection sort?
Swap!
1 12 15 23 48 74 128
– No!
It will find the largest element for n-1 times
Namely, time complexity of selection sort is always n^2
65
Thank you!
E-mail: [email protected]
66