You are on page 1of 6

Outline

Simple Sorting Algorithms 1 Bubble Sort


“Data Structures and Algorithms”
2 Selection Sort

Lê Hồng Phương1
3 Insertion Sort
1 Data Science Laboratory,

Vietnam National University, Hanoi


<phuonglh@hus.edu.vn> 4 Shuffling

5 Exercises

Lê Hồng Phương (DSL, VNU) Introduction 1 / 24 Lê Hồng Phương (DSL, VNU) Introduction 2 / 24

Sorting Sorting Algorithms

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

Bubble sort is a simple sorting algorithm. void bubbleSort(int a[], int n) {


How does it work? int i, j;
Start at the beginning of the array, compares the first two elements
and if the first is greater than the second, it swaps them. for (i = (n - 1); i > 0; i--) {
Continue doing this for each pair of adjacent elements to the end of for (j = 1; j <= i; j++) {
the array.
if (a[j - 1] > a[j]) {
Starts again with the first two elements, repeating until no swaps
have occurred on the last pass. swap(a[j-1], a[j]);
}
In the worst case, how many swaps this algorithm needs to sort an
}
array of n elements?
}
vs sô phần tử n cần: (n-1).n }
(n-1) + (n-2)+....+2+1= ______
2 Examples: a = {5, 1, 4, 2, 8}; a = {25, 17, 31, 13, 2}

Lê Hồng Phương (DSL, VNU) Introduction 5 / 24 Lê Hồng Phương (DSL, VNU) Introduction 6 / 24

Bubble Sort Selection Sort

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

void selectionSort(int a[], int n) { void selectionSort(int a[], int n) {


int i, j; int i, j;

for (i = 0; i < n - 1; i++) { for (i = 0; i < n - 1; i++) {


1) find j: a[j] = min{a[i+1]...a[n-1]} for (j = i+1; j < n; j++) {
2) swap(a[i],a[j]); if (a[i] > a[j]) {
} swap(a[i],a[j]);
} }
}
}
}

What is the difference between the two implementations?

Lê Hồng Phương (DSL, VNU) Introduction 9 / 24 Lê Hồng Phương (DSL, VNU) Introduction 10 / 24

Insertion Sort Insertion Sort

A simple insertion sort: O(n2 )


Insertion sort is a simple sorting algorithm.
Iterate through array until an out-of-order element found
Like bubble sort, it is mostly efficient for small data sets and is
often used as part of more sophisticated algorithms. Insert out-of-order element into correct location
How does it work? Repeat until end of array reached
Take elements from the array one by one Split into two functions for ease-of-use
Insert them to their correct position
shiftElement()
Insertion is expensive because it requires shifting all following
insertionSort()
elements over by one.

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

Insertion Sort Insertion Sort

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

Problem Generate a random real number for each array entry


Rearrange array so that result is a uniformly random permutation. Sort the array
 

2 3 4 5 6 7 8 9 10 
2, 3, 4, 5, 6, 7, 8, 9, 10 ⇒ 8, 6, 9, 7, 2, 4, 10, 5, 3
0.800 0.970 0.915 0.964 0.157 0.485 0.141 0.421 0.957
 

8 6 9 7 2 4 10 5 3 
0.141 0.157 0.421 0.485 0.800 0.915 0.957 0.964 0.970

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

War Story (Microsoft) War Story (Microsoft)

Solution? Implement shuffle sort by making comparator always return


Microsoft antitrust probe by EU. Microsoft agreed to provide
a random answer.
a randomized ballot screen for users to select browser in Windows
7. public int compareTo(Browser b) {
http://www.browserchoice.eu/ double r = Math.random();
if (r < 0.5) return -1;
if (r > 0.5) return +1;
return 0;
}

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)

How We Learned to Cheat at Online Poker: A Study in Software


In iteration i, pick integer j between 0 and i uniformly at random;
Security: http://www.datamation.com/entdev/article.php/616221/
Swap a[i] and a[j];

public void shuffle(Object[] a) {


int n = a.length;
for (int i = 0; i < n; i++) {
int j = new Random().nextInt(i+1);
swap(a[i], a[j]);
}
}

Lê Hồng Phương (DSL, VNU) Introduction 21 / 24 Lê Hồng Phương (DSL, VNU) Introduction 22 / 24

War Story (Online Poker) Exercises

Shuffling algorithm in use:


Set up Eclipse IDE for Java programming
for i := 1 to 52 do Implement simple sorting algorithms:
begin Bubble sort
j := random(51) + 1; Selection sort (two implementations)
swap(card[i], card[j]); Insertion sort
end; Use generic data type for sorting arbitrary types
Test the sorting algorithms on various data sets (ints, doubles,
Bug 1. Random number j never 52 ⇒ 52-nd card can’t end up in strings, students, cards. . . )
52-th place.
Bug 2. Shuffle not uniform (should be between 1 and i).
Bug 3. random() uses 32-bit seed ⇒ 232 possible shuffles.
Bug 4. Seed = milliseconds since midnight ⇒ 86.4 million
shuffles.

Lê Hồng Phương (DSL, VNU) Introduction 23 / 24 Lê Hồng Phương (DSL, VNU) Introduction 24 / 24

You might also like