Professional Documents
Culture Documents
Lê Hồng Phương1
3 Insertion Sort
1 Data Science Laboratory,
5 Exercises
Lê Hồng Phương (DSL, VNU) Introduction 1 / 24 Lê Hồng Phương (DSL, VNU) Introduction 2 / 24
Sorting is one of the fundamental problems in computer science. There are many sorting algorithms, many of them provide a gentle
A sorting algorithm is an algorithm that puts elements of a list in introduction to a variety of core algorithm concepts.
a certain order. Although many people consider that is is a solved problem, useful
The most-used orders are numerical order and lexicographical new sorting algorithms are still being invented, for example library
order. sort was first published in 2004.
Efficient sorting is important for optimizing the use of other Common and well-known sorting algorithms: bubble sort, selection
algorithms (such as searching and merging algorithms). sort, insertion sort, quicksort, merge sort, heap sort.
The sorting problem has attracted a great deal of research due to
the complexity of solving it efficiently.
Lê Hồng Phương (DSL, VNU) Introduction 3 / 24 Lê Hồng Phương (DSL, VNU) Introduction 4 / 24
Bubble Sort Bubble Sort
Lê Hồng Phương (DSL, VNU) Introduction 5 / 24 Lê Hồng Phương (DSL, VNU) Introduction 6 / 24
Bubble sort is rarely used to sort unordered large data sets Selection sort is an in-place comparison sort.
because of its high time complexity (O(n2 )). It can be used to sort It has the same complexity as bubble sort, making it inefficient on
small data sets. large data sets.
It is also efficiently used on an array that is already sorted except How does it work?
for a very small number of elements. Find the minimum value of the array
If only one element is not in order, bubble sort will take only 2n Swap it with the value in the first position
time. Repeat this process for the remainder of the array
If two elements are not in order, it will take only 3n time.
Selection sort does no more than n swaps and thus is useful when
swapping is very expensive.
Lê Hồng Phương (DSL, VNU) Introduction 7 / 24 Lê Hồng Phương (DSL, VNU) Introduction 8 / 24
Selection Sort – First Implementation Selection Sort – Second Implementation
Lê Hồng Phương (DSL, VNU) Introduction 9 / 24 Lê Hồng Phương (DSL, VNU) Introduction 10 / 24
Lê Hồng Phương (DSL, VNU) Introduction 11 / 24 Lê Hồng Phương (DSL, VNU) Introduction 12 / 24
Shift Elements Shift Elements
Shift elements on the left of a position i to find the correct position for Example: i = 3, a = {17, 25, 31, 18, 2}, iValue = 18.
a[i]:
i 0 1 2 3 4 i 0 1 2 3 4
void shiftElement(int a[], int i) { a[i] 17 25 31 18 2 ⇒ a[i] 17 25 31 31 2
int iValue = a[i]; ↑ ↑
while ((i > 0) && (a[i-1] > iValue)) {
a[i] = a[i-1]; i 0 1 2 3 4 i 0 1 2 3 4
i--; a[i] 17 25 25 31 2 ⇒ a[i] 17 25 25 31 2
} ↑ ↑
a[i] = iValue;
} i 0 1 2 3 4
a[i] 17 18 25 31 2
↑
Run the function with i = 4. What does the array look like?
Lê Hồng Phương (DSL, VNU) Introduction 13 / 24 Lê Hồng Phương (DSL, VNU) Introduction 14 / 24
Simply shift each element of the array if it is not in the correct position.
Explain the algorithm on the following arrays:
void insertionSort(int a[], int n) {
int i; a = {25, 17, 31, 18, 2}
for (i = 1; i < n; i++) { a = {3, 2, 1, 5, 1, 6}
if (a[i] < a[i-1]) {
shiftElement(a, i);
}
}
}
Lê Hồng Phương (DSL, VNU) Introduction 15 / 24 Lê Hồng Phương (DSL, VNU) Introduction 16 / 24
Shuffle an Array Shuffle Sort
Proposition
Shuffle sort produces a uniformly random permutation of the input
array, provided no duplicate values.
Lê Hồng Phương (DSL, VNU) Introduction 17 / 24 Lê Hồng Phương (DSL, VNU) Introduction 18 / 24
Lê Hồng Phương (DSL, VNU) Introduction 19 / 24 Lê Hồng Phương (DSL, VNU) Introduction 20 / 24
Knuth Shuffle War Story (Online Poker)
Lê Hồng Phương (DSL, VNU) Introduction 21 / 24 Lê Hồng Phương (DSL, VNU) Introduction 22 / 24
Lê Hồng Phương (DSL, VNU) Introduction 23 / 24 Lê Hồng Phương (DSL, VNU) Introduction 24 / 24