15
Sorting Algorithms Keyang He Discrete Mathematics

Sorting Algorithms Keyang He Discrete Mathematics

Embed Size (px)

Citation preview

Page 1: Sorting Algorithms Keyang He Discrete Mathematics

Sorting Algorithms

Keyang He

Discrete Mathematics

Page 2: Sorting Algorithms Keyang He Discrete Mathematics

Basic Concepts

• Algorithm – a specific set of instructions for carrying out a procedure or solving a problem, usually with the requirement that the procedure terminate at some point.

• Sorting – the rearrangement of numbers in a list into their correct lexicographic order.

Page 3: Sorting Algorithms Keyang He Discrete Mathematics

Example

• 1, 7, 5, 3, 9

• 1, 3, 5, 7, 9

• 13, 25, 63, 73, 12, 2, 6, 90, 111, 242, 14, 12425, 8, 79, 64, 842, 678931, 53332

Page 4: Sorting Algorithms Keyang He Discrete Mathematics

Bubble Sort

• From n = 1, compare an and an+1

• If an > an+1, switch the order of an and an+1

• Continue this process until all an < an+1

Page 5: Sorting Algorithms Keyang He Discrete Mathematics

Bubble Sort

• 1, 7, 5, 3, 9

• 1, 7, 5, 3, 9

• 1, 5, 3, 7, 9

• 1, 5, 7, 3, 9

• 1, 5, 3, 7, 9

• 1, 3, 5, 7, 9

• 1, 5, 3, 7, 9

• 1, 3, 5, 7, 9

Page 6: Sorting Algorithms Keyang He Discrete Mathematics

Time Complexity Analysis

• Input Size – the size of the input

• Basic Operation – instruction or group of instructions such that the total work done by the algorithm is roughly proportional to the number of times this instruction or group of instructions is done

Page 7: Sorting Algorithms Keyang He Discrete Mathematics

Time Complexity Analysis

• Time Complexity Analysis – the determination of how many times the basic operation is done for each value of the input size

Page 8: Sorting Algorithms Keyang He Discrete Mathematics

Big-O Notation

• Big-O Notation – describes the limiting behavior of a function when the argument tends towards a particular value or infinity

Page 9: Sorting Algorithms Keyang He Discrete Mathematics

Big-O Notation

Page 10: Sorting Algorithms Keyang He Discrete Mathematics

Merge Sort

• Invented by John von Neumann in 1945

• Algorithm1) Divide the unsorted list into n sublists, each

containing 1 element.2) Repeatedly merge sublists to produce new

sublists until there is only 1 sublist remaining. This will be the sorted list.

Page 11: Sorting Algorithms Keyang He Discrete Mathematics

Merge Sort

Page 12: Sorting Algorithms Keyang He Discrete Mathematics

Sorting Algorithms ComparisonSorting Algorithms Average Time Complexity

Bubble Sort O(n2)

Merge Sort O(n log(n))

Page 13: Sorting Algorithms Keyang He Discrete Mathematics

Key-comparison Algorithms

• It has been shown that no key-comparison algorithm can perform better than O(n log(n)).

• A few special case algorithms can sort certain data sets faster than O(n log(n)). These algorithms are not based on comparing the items being sorted and rely on tricks.

Page 14: Sorting Algorithms Keyang He Discrete Mathematics

Non Key-comparison Algorithms

• Example: 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1,….. a) Count the number of 0’s and 1’s

Let n = the number of 0’sLet m = the number of 1’s

b) The new array will consist of n 0’s followed by m 1’s

Page 15: Sorting Algorithms Keyang He Discrete Mathematics

References

• http://mathworld.wolfram.com/Sorting.html• http://mathworld.wolfram.com/

Algorithm.html• http://www.cprogramming.com/tutorial/

computersciencetheory/sortcomp.html