Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Number of Iterations is (N 1)
1 42 1 35 1 12 1 12 1 5
2 35 2 12 2 35 2 5 2 12
3 12 3 42 3 5 3 35 3 35
4 77 4 5 4 42 4 42 4 42
5 5 5 77 5 77 5 77 5 77
for (K = 1 to J) do /* this for loop bubbles-up the next largest integer into the (J+1) position progressively*/ { if (USL[K] > USL[K + 1]) then { Swap (USL[K], USL[K + 1]); did-swap := true; In the worst-case, } the number of } comparisons is N/2! J := J 1; } return (USL[]); }
for (K = 1 to J) do /* this for loop bubbles-up the next largest integer into the (J+1) position progressively*/ { if (USL[K] > USL[K + 1]) then { Swap (USL[K], USL[K + 1]); did-swap := true; In the best-case, the } the number number of comparisons of } comparisons is (N-1)! is N! J := J 1; } return (USL[]); }
Insertion Sort
23
1
17
2
45
3
18
4
12
5
22
6
17
1 2
45
3
18
4
12
5
22
6
23
1 2
45
1 2 3
18
4
12
5
22
6
17
1
23
2
18
1 2 3 4
12
5
22
6
17
1
23
2
45
3 4
5 6
18
1 2 3 4
12
5
22
6
17
1
23
2
45
3 4
5 6
12
1 2 3 4 5
22
6
17
1
18
2
23
3
45
4
5 6
12
1 2 3 4 5
22
6
17
1
18
2
23
3
45
4
5 6
22
1 2 3 4 5 6
12
1
17
2
18
3
23
4
45
5 6
22
1 2 3 4 5 6
12
1
17
2
18
3
23
4
45
5 6
12
1
17
2
18
3
22
4
23
5
45
6
Insertion Sort
Sorted-List[] := Insertion-Sort (USL[], N);
{ USL[1:N] is the unsorted list of integers */ int array Temp-SL[1:N]; int I, J, K; Temp-SL[1] := USL[1]; for (I = 2 to N) do {/* insert element USL[I] into Temp-SL[] */
In the worst-case, the number of iterations is (N-1)!