160 likes | 328 Views
Sorting Algorithms. Discrete Mathematics. Keyang He. 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 .
E N D
Sorting Algorithms Discrete Mathematics Keyang He
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.
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
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
Bubble Sort • 1, 7, 5, 3, 9 • 1, 3, 5, 7, 9 • 1, 7, 5, 3, 9 • 1, 3, 5, 7, 9 • 1, 5, 7, 3, 9 • 1, 5, 3, 7, 9 • 1, 5, 3, 7, 9 • 1, 5, 3, 7, 9
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
Time Complexity Analysis • Time Complexity Analysis – the determination of how many times the basic operation is done for each value of the input size
Big-O Notation • Big-O Notation –describes the limiting behavior of a function when the argument tends towards a particular value or infinity
Merge Sort • Invented by John von Neumann in 1945 • Algorithm • Divide the unsorted list into n sublists, each containing 1 element. • Repeatedly merge sublists to produce new sublists until there is only 1 sublist remaining. This will be the sorted list.
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.
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’s Let m = the number of 1’s b) The new array will consist of n 0’s followed by m 1’s
References • http://mathworld.wolfram.com/Sorting.html • http://mathworld.wolfram.com/Algorithm.html • http://www.cprogramming.com/tutorial/computersciencetheory/sortcomp.html