1 Data Structures ad Algorithms Part 1.4 Werer Nutt
2 DSA, Part 1: Itroductio, syllabus, orgaisatio Algorithms Recursio (priciple, trace, factorial, Fiboacci) Sortig (bubble, isertio, selectio)
3 Sortig Sortig is a classical ad importat algorithmic problem. For which operatios is sortig eeded? Which systems implemet sortig? We look at sortig arrays (i cotrast to files, which restrict radom access) A key costrait are the restrictios o the space: i-place sortig algorithms (o extra RAM). The ru-time compariso is based o the umber of comparisos (C) ad the umber of movemets (M).
4 Sortig Sortig is a classical ad importat algorithmic problem. For which operatios is sortig eeded? Which systems implemet sortig? We look at sortig arrays (i cotrast to files, which restrict radom access) A key costrait are the restrictios o the space: i-place sortig algorithms (o extra RAM). The ru-time compariso is based o the umber of comparisos (C) ad the umber of movemets (M).
5 Sortig Simple sortig methods use roughly * comparisos Isertio sort Selectio sort Bubble sort Fast sortig methods use roughly * log comparisos Merge sort Heap sort Quicksort What s the poit of studyig those simple methods?
6 Example 2: Sortig INPUT sequece of umbers OUTPUT a permutatio of the iput sequece of umbers a 1, a 2, a 3,.,a Sort b 1, b 2, b 3,., b 2 5 4 10 7 2 4 5 7 10 Correctess (requiremets for the output) For ay give iput the algorithm halts with the output: b 1 b 2 b 3. b b 1, b 2, b 3,., b is a permutatio of a 1, a 2, a 3,.,a
7 Isertio Sort A 3 4 6 8 9 7 2 5 1 1 j i Strategy Start with oe sorted card. Isert a usorted card at the correct positio i the sorted part. Cotiue util all usorted cards are iserted/sorted. 44 55 12 42 94 18 06 67 44 55 12 42 94 18 06 67 12 44 55 42 94 18 06 67 12 42 44 55 94 18 06 67 12 42 44 55 94 18 06 67 12 18 42 44 55 94 06 67 06 12 18 42 44 55 94 67 06 12 18 42 44 55 67 94
8 Isertio Sort/2 INPUT: A[1..] a array of itegers OUTPUT: permutatio of A s.t. A[1] A[2]... A[] for j := 2 to do // A[1..j-1] sorted key := A[j]; i := j-1; while i > 0 ad A[i] > key do A[i+1] := A[i]; i--; A[i+1] := key The umber of comparisos durig the jth iteratio is at least 1: C mi = = - 1 at most j-1: C max = = (* - )/2 1 j 1
9 Isertio Sort/2 INPUT: A[1..] a array of itegers OUTPUT: permutatio of A s.t. A[1] A[2]... A[] for j := 2 to do // A[1..j-1] sorted key := A[j]; i := j-1; while i > 0 ad A[i] > key do A[i+1] := A[i]; i--; A[i+1] := key The umber of comparisos durig the jth iteratio is at least 1: C mi = = - 1 at most j-1: C max = j 1 = (* - )/2 1
10 Isertio Sort/3 The umber of comparisos durig the jth iteratio is: j/2 average: C avg = j/2= (* + 2)/4 The umber of movemets is Ci+1: 2 M mi = = 2*(-1), M avg = = (* + 5-6)/4 j/2 1 M max = = (* + - 2)/2 j
11 Selectio Sort A 1 2 3 4 5 7 8 9 6 1 j i Strategy Start empty haded. Elarge the sorted part by swappig the first elemet of the usorted part with the smallest elemet of the usorted part. Cotiue util the usorted part cosists of oe elemet oly. 44 55 12 42 94 18 06 67 06 55 12 42 94 18 44 67 06 12 55 42 94 18 44 67 06 12 18 42 94 55 44 67 06 12 18 42 94 55 44 67 06 12 18 42 44 55 94 67 06 12 18 42 44 55 94 67 06 12 18 42 44 55 67 94
12 Selectio Sort/2 INPUT: A[1..] a array of itegers OUTPUT: a permutatio of A such that A[1] A[2] A[] for j := 1 to -1 do // A[1..j-1] sorted ad miimum key := A[j]; ptr := j for i := j+1 to do if A[i] < key the key := A[i]; ptr := i; A[ptr] := A[j]; A[j] := key The umber of comparisos is idepedet of the origial orderig (this is a less atural behavior tha isertio sort): C = 1 1 j=1 j = k=1 k = (* - )/2
13 Selectio Sort/3 The umber of movemets is: M mi = 1 j=1 3 = 3*(-1) M max = 1 j=1 -j+3 = (* )/2 + 3*(-1)
14 Bubble Sort A 1 2 3 4 5 7 9 8 6 1 j Strategy Start from the back ad compare pairs of adjacet elemets. Swap the elemets if the larger comes before the smaller. I each step the smallest elemet of the usorted part is moved to the begiig of the usorted part ad the sorted part grows by oe. 44 55 12 42 94 18 06 67 06 44 55 12 42 94 18 67 06 12 44 55 18 42 94 67 06 12 18 44 55 42 67 94 06 12 18 42 44 55 67 94 06 12 18 42 44 55 67 94 06 12 18 42 44 55 67 94 06 12 18 42 44 55 67 94
15 Bubble Sort/2 INPUT: A[1..] a array of itegers OUTPUT: permutatio of A s.t. A[1] A[2] A[] for j := 2 to do // A[1..j-2] sorted ad miimum for i := to j do if A[i-1] > A[i] the key := A[i-1]; A[i-1] := A[i]; A[i]:= key The umber of comparisos is idepedet of the origial orderig: C = j 1 = (* - )/2
16 Bubble Sort/3 The umber of movemets is: M mi = 0 3 j 1 M max = = 3**( - 1)/2 3 j 1 / 2 M avg = = 3**( - 1)/4
17 Properties of a Sortig Algorithm Efficiet: has low (worst case) rutime I place: eeds (almost) o additioal space (fixed umber of scalar variables) Adaptive: performs little work if the array is already (mostly) sorted Stable: does ot chage the order of elemets with equal key values Olie: ca sort data as it receives them
18 Sortig Algorithms: Properties Which algorithm has which property? Isertio Sort Selectio Sort Bubble Sort Adaptive Stable Olie
19 Summary Precise problem specificatio is crucial. Precisely specify Iput ad Output. Pseudocode, Java, C, is largely equivalet for our purposes. Recursio: procedure/fuctio that calls itself. Sortig: importat problem with classic solutios.