Computer Science II - PowerPoint PPT Presentation

1 / 71
About This Presentation
Title:

Computer Science II

Description:

Bubble Sort. Exchange sorting algorithm ... Each pass through the array bubbles the next largest element up to the slot it should occupy ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 72
Provided by: joe49
Category:

less

Transcript and Presenter's Notes

Title: Computer Science II


1
Computer Science II
  • Sorting

2
Some Sorting Methods
  • Insertion sort
  • Bubble sort
  • Quicksort
  • Merge sort

3
Insertion Sort
  • Begins with an unsorted sequence of elements
    Ss0,s1,...,sn
  • Adds each of these elements, one at a time, to a
    new sequence S, searching through the new list
    to determine the place to insert the new element

4
Insertion SortHow to do it
C
A
D
B
5
Insertion SortHow to do it
C
A
D
B
  • Insert C

C
A
D
B
6
Insertion SortHow to do it
C
A
D
B
  • Insert C
  • Insert A, moving C down

C
A
D
B
A
C
D
B
7
Insertion SortHow to do it
C
A
D
B
  • Insert C
  • Insert A, moving C down
  • Insert D

C
A
D
B
A
C
D
B
A
C
D
B
8
Insertion SortHow to do it
C
A
D
B
  • Insert C
  • Insert A, moving C down
  • Insert D
  • Insert B, moving C and D down

C
A
D
B
A
C
D
B
A
C
D
B
A
B
C
D
9
Bubble Sort
  • Exchange sorting algorithm
  • Moves larger elements up through array, by
    swapping adjacent elements that are out of
    order
  • Each pass through the array bubbles the next
    largest element up to the slot it should occupy

10
Bubble SortHow to do it
C
A
D
B
11
Bubble SortHow to do it
C
A
D
B
A
C
B
D
  • Swap C-A and D-B

12
Bubble SortHow to do it
C
A
D
B
A
C
B
D
  • Swap C-A and D-B
  • Swap C-B

A
B
C
D
13
Bubble SortHow to do it
C
A
D
B
A
C
B
D
  • Swap C-A and D-B
  • Swap C-B
  • No swap

A
B
C
D
A
B
C
D
14
Bubble SortHow to do it
C
A
D
B
A
C
B
D
  • Swap C-A and D-B
  • Swap C-B
  • No swap
  • Last element, so were done!

A
B
C
D
A
B
C
D
A
B
C
D
15
Quicksort
  • Exchange sorting algorithm
  • Divide and conquer!
  • A each stage, choose a pivot element, and
  • Divide the remaining elements into those less
    than the pivot, and those greater than the pivot

16
Quicksort(A,p,r)
  • 1. if p lt r
  • 2. then q lt- Partition(A,p,r)
  • 3. Quicksort(A,p,q)
  • Quicksort(A,q1,r)
  • Yes, this is recursive!

17
Partition(A,p,r)
  • x lt- Ap
  • i lt- p-1
  • j lt- r1
  • repeat
  • repeat
  • j lt- j - 1
  • until Aj lt x
  • repeat
  • i lt- i 1
  • until Ai gt x
  • if iltj
  • then Ai lt-gt Aj
  • until i gt j
  • return j

18
Paritition( )Example 1
A
F
J
D
E
G
C
H
1 2 3 4 5 6 7 8
0
9
Start
C
X
19
Paritition( )Example 1
A
F
J
D
E
G
C
H
1 2 3 4 5 6 7 8
0
9
J loop
C
X
20
Paritition( )Example 1
A
F
J
D
E
G
C
H
1 2 3 4 5 6 7 8
0
9
J loop
C
X
21
Paritition( )Example 1
A
F
J
D
E
G
C
H
1 2 3 4 5 6 7 8
0
9
J loop
C
X
22
Paritition( )Example 1
A
F
J
D
E
G
C
H
1 2 3 4 5 6 7 8
0
9
J loop
C
X
23
Paritition( )Example 1
A
F
J
D
E
G
C
H
1 2 3 4 5 6 7 8
0
9
J loop
C
X
24
Paritition( )Example 1
A
F
J
D
E
G
C
H
1 2 3 4 5 6 7 8
0
9
J loop
C
X
25
Paritition( )Example 1
A
F
J
D
E
G
C
H
1 2 3 4 5 6 7 8
0
9
I loop
C
X
26
Paritition( )Example 1
C
F
J
D
E
G
A
H
1 2 3 4 5 6 7 8
0
9
Swap
C
X
27
Paritition( )Example 1
C
F
J
D
E
G
A
H
1 2 3 4 5 6 7 8
0
9
J loop
C
X
28
Paritition( )Example 1
C
F
J
D
E
G
A
H
1 2 3 4 5 6 7 8
0
9
I loop
C
X
29
Paritition( )Example 1
G
F
J
D
E
C
A
H
1 2 3 4 5 6 7 8
0
9
Swap
C
X
30
Paritition( )Example 1
G
F
J
D
E
C
A
H
1 2 3 4 5 6 7 8
0
9
J Loop
C
X
31
Paritition( )Example 1
G
F
J
D
E
C
A
H
1 2 3 4 5 6 7 8
0
9
I Loop
C
X
32
Paritition( )Example 1
G
F
J
D
E
C
A
H
1 2 3 4 5 6 7 8
0
9
Done!
C
X
33
Partition(A,p,r)
  • x lt- Ar
  • i lt- p
  • j lt- r -1
  • repeat
  • do while (i lt j) and (Ai lt x)
  • do i lt- i 1
  • while (i lt j) and (Aj gt x)
  • do j lt- j 1
  • if i lt j
  • then Ai lt-gt Aj
  • until i gt j

34
Paritition( )Example 2
A
F
J
H
E
G
C
D
1 2 3 4 5 6 7 8
Start
X
35
Paritition( )Example 2
A
F
J
H
E
G
C
D
1 2 3 4 5 6 7 8
Hide pivot
D
X
36
Paritition( )Example 2
A
F
J
H
E
G
C
D
1 2 3 4 5 6 7 8
I loop
D
X
37
Paritition( )Example 2
A
F
J
H
E
G
C
D
1 2 3 4 5 6 7 8
J loop
D
X
38
Paritition( )Example 2
A
F
J
H
E
G
C
D
1 2 3 4 5 6 7 8
J loop
D
X
39
Paritition( )Example 2
A
F
J
H
E
G
C
D
1 2 3 4 5 6 7 8
J loop
D
X
40
Paritition( )Example 2
A
F
J
H
E
G
C
D
1 2 3 4 5 6 7 8
J loop
D
X
41
Paritition( )Example 2
G
F
J
H
E
A
C
D
1 2 3 4 5 6 7 8
Swap
D
X
42
Paritition( )Example 2
G
F
J
H
E
A
C
D
1 2 3 4 5 6 7 8
I loop
D
X
43
Paritition( )Example 2
G
F
J
H
E
A
C
D
1 2 3 4 5 6 7 8
J loop
D
X
44
Paritition( )Example 2
G
D
J
H
E
A
C
F
1 2 3 4 5 6 7 8
Return pivot
D
X
45
Paritition( )Example 2
G
D
J
H
E
A
C
F
1 2 3 4 5 6 7 8
Done!
D
X
46
Partition ( )
  • How did they the two examples differ?
  • Which example was better?
  • No. 1?
  • No.2
  • Define better

47
Merge Sort
  • Divide and Conquer
  • Continually merges together large and larger
    sequences

48
Merge SortHow to do it
Stack 0
Stack 1
Stack 2
5
3
2
6
1
8
7
4
  • Start with three stacks
  • Place all of the elements in stack 0

49
Merge SortHow to do it
Stack 0
Stack 1
Stack 2
5
3
2
6
1
8
7
4
  • Divide elements, one at time, between stacks 1
    and 2

50
Merge SortHow to do it
Stack 0
Stack 1
Stack 2
5
3
2
6
7
4
1
1
8
8
2
6
7
5
3
4
  • Divide elements, one at time, between stacks 1
    and 2

51
Merge SortHow to do it
Stack 0
Stack 1
Stack 2
7
4
1
8
2
6
5
3
  • Merge sequences of length one back onto stack 0

52
Merge SortHow to do it
Stack 0
Stack 1
Stack 2
7
4
7
1
8
2
6
7
5
3
4
  • Merge sequences of length one back onto stack 0

53
Merge SortHow to do it
Stack 0
Stack 1
Stack 2
8
1
8
1
2
6
7
5
3
4
  • Merge sequences of length one back onto stack 0

54
Merge SortHow to do it
Stack 0
Stack 1
Stack 2
6
2
8
1
2
6
7
5
3
4
  • Merge sequences of length one back onto stack 0

55
Merge SortHow to do it
Stack 0
Stack 1
Stack 2
5
3
6
2
8
1
7
5
3
4
  • Merge sequences of length one back onto stack 0

56
Merge SortHow to do it
Stack 0
Stack 1
Stack 2
5
3
6
2
8
1
7
4
  • Distribute sequences of two between stacks 1 and 2

57
Merge SortHow to do it
Stack 0
Stack 1
Stack 2
5
3
6
2
8
1
3
7
5
4
  • Distribute sequences of two between stacks 1 and 2

58
Merge SortHow to do it
Stack 0
Stack 1
Stack 2
6
2
8
1
2
3
7
6
5
4
  • Distribute sequences of two between stacks 1 and 2

59
Merge SortHow to do it
Stack 0
Stack 1
Stack 2
1
8
8
1
2
3
7
6
5
4
  • Distribute sequences of two between stacks 1 and 2

60
Merge SortHow to do it
Stack 0
Stack 1
Stack 2
4
1
7
8
2
3
7
6
5
4
  • Distribute sequences of two between stacks 1 and 2

61
Merge SortHow to do it
Stack 0
Stack 1
Stack 2
4
1
7
8
2
3
6
5
  • Merge sequences of two back onto stack 0

62
Merge SortHow to do it
Stack 0
Stack 1
Stack 2
4
1
8
7
8
7
2
3
4
6
5
1
  • Merge sequences of two back onto stack 0

63
Merge SortHow to do it
Stack 0
Stack 1
Stack 2
6
5
3
2
8
7
2
3
4
6
5
1
  • Merge sequences of two back onto stack 0

64
Merge SortHow to do it
Stack 0
Stack 1
Stack 2
6
5
3
2
8
7
4
1
  • Distribute sequences of four between stacks 1 and
    2

65
Merge SortHow to do it
Stack 0
Stack 1
Stack 2
6
5
3
2
8
2
7
3
4
5
1
6
  • Distribute sequences of four between stacks 1 and
    2

66
Merge SortHow to do it
Stack 0
Stack 1
Stack 2
8
2
1
7
3
4
4
5
7
1
6
8
  • Distribute sequences of four between stacks 1 and
    2

67
Merge SortHow to do it
Stack 0
Stack 1
Stack 2
2
1
3
4
5
7
6
8
  • Merge sequences of four back onto stack 0

68
Merge SortHow to do it
Stack 0
Stack 1
Stack 2
6
5
2
1
4
3
4
3
5
7
2
6
8
1
  • Merge sequences of four back onto stack 0

69
Merge SortHow to do it
Stack 0
Stack 1
Stack 2
8
7
6
5
4
3
7
2
8
1
  • Merge sequences of four back onto stack 0

70
Merge SortHow to do it
Stack 0
Stack 1
Stack 2
8
7
6
5
4
3
2
1
  • Done!

71
Try this!
  • Write a program to sort the following sequence of
    numbers using merge sort.
  • 1 7 5 6 4 2 3 0
Write a Comment
User Comments (0)
About PowerShow.com