Insertion Sort - PowerPoint PPT Presentation

1 / 49
About This Presentation
Title:

Insertion Sort

Description:

6 do A[i 1]A[i] 7 i i-1. 8 A[i 1] key. Insertion Sort (j=6) ... 6 do A[i 1]A[i] 7 ii-1. Insertion Sort. 6. 1. 5. 4. 3. 2. Insertion Sort. 6. 1. 3. 5. 4. 2. i=4 ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 50
Provided by: jeh96
Category:
Tags: doa | insertion | sort

less

Transcript and Presenter's Notes

Title: Insertion Sort


1
Insertion Sort
  • Input A sequence of n numbers or
  • An array A
  • e.g. A
  • Output A permutation of the input seq. in
  • increasing order.
  • lengthA n, in the e.g., lengthA6

3
5
1
6
4
2
2
Insertion Sort
3
5
1
6
4
2
3
Insertion Sort
5
2
4
Insertion Sort
5
2
5
Insertion Sort
2
5
6
Insertion Sort
2
4
5
7
Insertion Sort
2
4
5
8
Insertion Sort
2
5
4
9
Insertion Sort
2
5
4
10
Insertion Sort
2
1
5
4
11
Insertion Sort
2
1
5
4
12
Insertion Sort
2
5
1
4
13
Insertion Sort
2
5
1
4
14
Insertion Sort
2
5
4
1
15
Insertion Sort
2
5
4
1
16
Insertion Sort
1
5
4
2
17
Insertion Sort
1
6
5
4
2
18
Insertion Sort
1
6
5
4
2
19
Insertion Sort
3
1
6
5
4
2
20
Insertion Sort
3
1
6
5
4
2
21
Insertion Sort
6
1
3
5
4
2
22
Insertion Sort
6
1
3
5
4
2
23
Insertion Sort
6
1
5
3
4
2
24
Insertion Sort
6
1
5
3
4
2
25
Insertion Sort
6
1
5
4
3
2
26
Insertion Sort
6
1
5
4
3
2
27
Insertion Sort
6
1
5
4
3
2
28
Insertion Sort
  • Input A sequence of n numbers or
  • An array A
  • e.g. A
  • Output A permutation of input seq.
  • lengthA n, in the e.g. lengthA6

3
5
1
6
4
2
29
Insertion Sort
  • Insertion-Sort (A)
  • 1 for j ? 2 to lengthA
  • 2 do key ?Aj
  • 3 ? Insert Aj into the sorted
  • sequence A1,, j-1
  • 4 i ? j-1
  • 5 while igt0 and Aigtkey
  • 6 do Ai1?Ai
  • 7 i? i-1
  • 8 Ai1 ?key

30
Insertion Sort (j6)
3
1
6
5
4
2
Key
2 do key ?Aj
31
Insertion Sort (j6)
3
key
i5
1
6
5
4
2
4 i ? j-1
32
Insertion Sort
3
key
i5
i4
6
1
5
4
2
6
5 while igt0 and Aigtkey
6 do Ai1?Ai
7 i?i-1
33
Insertion Sort
3
key
i4
i3
6
1
5
4
2
6
5
5 while igt0 and Aigtkey
6 do Ai1?Ai
7 i?i-1
34
Insertion Sort
3
key
i3
i2
6
1
5
4
2
5
4
5 while igt0 and Aigtkey
6 do Ai1?Ai
7 i?i-1
35
Insertion Sort
3
key
i2
6
1
4
2
5
4
5 while igt0 and Aigtkey 6
do Ai1?Ai 7
i?i-1
8 Ai1?key
36
Insertion Sort
6
1
5
4
3
2
37
(No Transcript)
38
(No Transcript)
39
Insertion Sort
3
key
i4
6
1
5
4
2
6
5 while igt0 and Aigtkey 6
do Ai1?Ai 7
i?i-1
40
Insertion Sort
3
key
i4
6
1
5
4
2
6
5 while igt0 and Aigtkey 6
do Ai1?Ai 7
i?i-1
41
Insertion Sort
3
key
i3
6
1
5
4
2
5
5 while igt0 and Aigtkey 6
do Ai1?Ai 7
i?i-1
42
Insertion Sort
3
key
i2
6
1
5
4
2
4
5 while igt0 and Aigtkey 6
do Ai1?Ai 7
i?i-1 8 Ai1 ? key
43
Insertion Sort
6
1
5
4
3
2
44
Merge Sort
45
Merge
2
7
5
4
1
8
6
3
1
7
5
4
2
8
6
3
1
4
3
2
5
8
7
6
46
  • Input Two sorted arrays L1,...,n1 and
    R1,..., n2
  • Output A permutation A1,, n1n2 of L ? R in
  • increasing order.
  • i ? 1
  • j ? 1
  • for k ? 1 to n1n2
  • do if Li lt Rj
  • then Ak ? Li
  • i ? i1
  • else Ak ? Rj
  • j ? j1

47
  • L1,3 and R2,4
  • i ? 1
  • j ? 1
  • for k ? 1 to n1n2
  • do if Li lt Ri
  • then Ak ? Li
  • i ? i1
  • else Ak ? Rj
  • j ? j1

A
A 1,
A 1,2,
A 1,2,3,
i1
j1
k1
k2
k3
k4
L11lt R12
L23gtR12
L23ltR24
L3?? R24
A11
A33
i2
i3
A22
j2
48
L1,3,1 and R2,4,1
  • L1,3 and R2,4
  • Ln11Ã 1
  • Rn21Ã 1
  • i ? 1
  • j ? 1
  • for k ?1 to n1n2
  • do if Lilt Rj
  • then Akà Li
  • i à i1
  • else Akà Rj
  • jà j1

A
A 1,
A 1,2,
A 1,2,3,
A 1,2,3,4
i1
j1
k1
k2
k3
k4
L11lt R12
L23gtR12
L23ltR24
L3?gt R24
A11
A33
i2
i3
A22
A44
j2
j3
49
  • 7 n/8
  • 3 n/4
  • 1 n/2

1 2 3 4 5 6 7 8
2 4 5 7
1 3 6 8
2 5
4 7
1 6
3 8
5
7
4
2
1
3
8
6
Write a Comment
User Comments (0)
About PowerShow.com