sorting bubble-sort anim

74
Sorting Algorithms 1. Selection 2. Bubble 3. Insertion 4. Merge 5. Quick 6. Shell

Upload: fajar-zain

Post on 11-May-2015

893 views

Category:

Education


1 download

TRANSCRIPT

Page 1: Sorting   bubble-sort anim

Sorting Algorithms

1. Selection2. Bubble3. Insertion4. Merge5. Quick6. Shell

Page 2: Sorting   bubble-sort anim

An Animated Example

674523 14 6 3398 42

0 1 2 3 4 5 6 7

pos_akhir

index

6

N 8 did_swap true

Page 3: Sorting   bubble-sort anim

An Animated Example

674523 14 6 3398 42

pos_akhir

index6

0

N 8 did_swap false

0 1 2 3 4 5 6 7

Page 4: Sorting   bubble-sort anim

An Animated Example

674523 14 6 3398 42

pos_akhir

index6

0

N 8

Swap

did_swap false

0 1 2 3 4 5 6 7

Page 5: Sorting   bubble-sort anim

An Animated Example

674598 14 6 3323 42

pos_akhir

index

6

0

N 8

Swap

did_swap true

0 1 2 3 4 5 6 7

Page 6: Sorting   bubble-sort anim

An Animated Example

674598 14 6 3323 42

pos_akhir

index

6

1

N 8 did_swap true

0 1 2 3 4 5 6 7

Page 7: Sorting   bubble-sort anim

An Animated Example

674598 14 6 3323 42

pos_akhir

index

6

1

N 8

Swap

did_swap true

0 1 2 3 4 5 6 7

Page 8: Sorting   bubble-sort anim

An Animated Example

679845 14 6 3323 42

pos_akhir

index

6

1

N 8

Swap

did_swap true

0 1 2 3 4 5 6 7

Page 9: Sorting   bubble-sort anim

An Animated Example

679845 14 6 3323 42

pos_akhir

index

6

2

N 8 did_swap true

0 1 2 3 4 5 6 7

Page 10: Sorting   bubble-sort anim

An Animated Example

679845 14 6 3323 42

pos_akhir

index6

2

N 8

Swap

did_swap true

0 1 2 3 4 5 6 7

Page 11: Sorting   bubble-sort anim

An Animated Example

671445 98 6 3323 42

pos_akhir

index

6

2

N 8

Swap

did_swap true

0 1 2 3 4 5 6 7

Page 12: Sorting   bubble-sort anim

An Animated Example

671445 98 6 3323 42

pos_akhir

index

6

3

N 8 did_swap true

0 1 2 3 4 5 6 7

Page 13: Sorting   bubble-sort anim

An Animated Example

671445 98 6 3323 42

pos_akhir

index

6

3

N 8

Swap

did_swap true

0 1 2 3 4 5 6 7

Page 14: Sorting   bubble-sort anim

An Animated Example

671445 6 98 3323 42

pos_akhir

index

6

3

N 8

Swap

did_swap true

0 1 2 3 4 5 6 7

Page 15: Sorting   bubble-sort anim

An Animated Example

671445 6 98 3323 42

pos_akhir

index6

4

N 8 did_swap true

0 1 2 3 4 5 6 7

Page 16: Sorting   bubble-sort anim

An Animated Example

671445 6 98 3323 42

pos_akhir

index

6

4

N 8

Swap

did_swap true

0 1 2 3 4 5 6 7

Page 17: Sorting   bubble-sort anim

An Animated Example

981445 6 67 3323 42

pos_akhir

index

6

4

N 8

Swap

did_swap true

0 1 2 3 4 5 6 7

Page 18: Sorting   bubble-sort anim

An Animated Example

981445 6 67 3323 42

pos_akhir

index6

5

N 8 did_swap true

0 1 2 3 4 5 6 7

Page 19: Sorting   bubble-sort anim

An Animated Example

981445 6 67 3323 42

pos_akhir

index

6

5

N 8

Swap

did_swap true

0 1 2 3 4 5 6 7

Page 20: Sorting   bubble-sort anim

An Animated Example

331445 6 67 9823 42

pos_akhir

index

6

5

N 8

Swap

did_swap true

0 1 2 3 4 5 6 7

Page 21: Sorting   bubble-sort anim

An Animated Example

331445 6 67 9823 42

pos_akhir

index

6

6

N 8 did_swap true

0 1 2 3 4 5 6 7

Page 22: Sorting   bubble-sort anim

An Animated Example

331445 6 67 9823 42

pos_akhir

index6

6

N 8

Swap

did_swap true

0 1 2 3 4 5 6 7

Page 23: Sorting   bubble-sort anim

An Animated Example

331445 6 67 4223 98

pos_akhir

index6

6

N 8

Swap

did_swap true

0 1 2 3 4 5 6 7

Page 24: Sorting   bubble-sort anim

After First Pass of Outer Loop

331445 6 67 4223 98

pos_akhir

index6

7

N 8

Finished first “Bubble Up”

did_swap true

0 1 2 3 4 5 6 7

Page 25: Sorting   bubble-sort anim

The Second “Bubble Up”

331445 6 67 4223 98

pos_akhir

index

5

0

N 8 did_swap false

0 1 2 3 4 5 6 7

Page 26: Sorting   bubble-sort anim

The Second “Bubble Up”

331445 6 67 4223 98

pos_akhir

index5

0

N 8 did_swap false

No Swap

0 1 2 3 4 5 6 7

Page 27: Sorting   bubble-sort anim

The Second “Bubble Up”

331445 6 67 4223 98

pos_akhir

index

5

1

N 8 did_swap false

0 1 2 3 4 5 6 7

Page 28: Sorting   bubble-sort anim

The Second “Bubble Up”

331445 6 67 4223 98

pos_akhir

index5

1

N 8 did_swap false

Swap

0 1 2 3 4 5 6 7

Page 29: Sorting   bubble-sort anim

The Second “Bubble Up”

334514 6 67 4223 98

pos_akhir

index5

1

N 8 did_swap true

Swap

0 1 2 3 4 5 6 7

Page 30: Sorting   bubble-sort anim

The Second “Bubble Up”

334514 6 67 4223 98

pos_akhir

index

5

2

N 8 did_swap true

0 1 2 3 4 5 6 7

Page 31: Sorting   bubble-sort anim

The Second “Bubble Up”

334514 6 67 4223 98

pos_akhir

index

5

2

N 8 did_swap true

Swap

0 1 2 3 4 5 6 7

Page 32: Sorting   bubble-sort anim

The Second “Bubble Up”

33614 45 67 4223 98

pos_akhir

index5

2

N 8 did_swap true

Swap

0 1 2 3 4 5 6 7

Page 33: Sorting   bubble-sort anim

The Second “Bubble Up”

33614 45 67 4223 98

pos_akhir

index5

3

N 8 did_swap true

0 1 2 3 4 5 6 7

Page 34: Sorting   bubble-sort anim

The Second “Bubble Up”

33614 45 67 4223 98

pos_akhir

index

5

3

N 8 did_swap true

No Swap

0 1 2 3 4 5 6 7

Page 35: Sorting   bubble-sort anim

The Second “Bubble Up”

33614 45 67 4223 98

pos_akhir

index5

4

N 8 did_swap true

0 1 2 3 4 5 6 7

Page 36: Sorting   bubble-sort anim

The Second “Bubble Up”

33614 45 67 4223 98

pos_akhir

index

5

4

N 8 did_swap true

Swap

0 1 2 3 4 5 6 7

Page 37: Sorting   bubble-sort anim

The Second “Bubble Up”

67614 45 33 4223 98

pos_akhir

index5

4

N 8 did_swap true

Swap

0 1 2 3 4 5 6 7

Page 38: Sorting   bubble-sort anim

The Second “Bubble Up”

67614 45 33 4223 98

pos_akhir

index5

5

N 8 did_swap true

0 1 2 3 4 5 6 7

Page 39: Sorting   bubble-sort anim

The Second “Bubble Up”

67614 45 33 4223 98

pos_akhir

index5

5

N 8 did_swap true

Swap

0 1 2 3 4 5 6 7

Page 40: Sorting   bubble-sort anim

The Second “Bubble Up”

42614 45 33 6723 98

pos_akhir

index5

5

N 8 did_swap true

Swap

0 1 2 3 4 5 6 7

Page 41: Sorting   bubble-sort anim

After Second Pass of Outer Loop

42614 45 33 6723 98

pos_akhir

index5

6

N 8 did_swap true

Finished second “Bubble Up”

0 1 2 3 4 5 6 7

Page 42: Sorting   bubble-sort anim

The Third “Bubble Up”

42614 45 33 6723 98

pos_akhir

index

4

0

N 8 did_swap false

0 1 2 3 4 5 6 7

Page 43: Sorting   bubble-sort anim

The Third “Bubble Up”

42614 45 33 6723 98

pos_akhir

index

4

0

N 8 did_swap false

Swap

0 1 2 3 4 5 6 7

Page 44: Sorting   bubble-sort anim

The Third “Bubble Up”

42623 45 33 6714 98

pos_akhir

index4

0

N 8 did_swap true

Swap

0 1 2 3 4 5 6 7

Page 45: Sorting   bubble-sort anim

The Third “Bubble Up”

42623 45 33 6714 98

pos_akhir

index

4

1

N 8 did_swap true

0 1 2 3 4 5 6 7

Page 46: Sorting   bubble-sort anim

The Third “Bubble Up”

42623 45 33 6714 98

pos_akhir

index4

1

N 8 did_swap true

Swap

0 1 2 3 4 5 6 7

Page 47: Sorting   bubble-sort anim

The Third “Bubble Up”

42236 45 33 6714 98

pos_akhir

index

4

1

N 8 did_swap true

Swap

0 1 2 3 4 5 6 7

Page 48: Sorting   bubble-sort anim

The Third “Bubble Up”

42236 45 33 6714 98

pos_akhir

index

4

2

N 8 did_swap true

0 1 2 3 4 5 6 7

Page 49: Sorting   bubble-sort anim

The Third “Bubble Up”

42236 45 33 6714 98

pos_akhir

index

4

2

N 8 did_swap true

No Swap

0 1 2 3 4 5 6 7

Page 50: Sorting   bubble-sort anim

The Third “Bubble Up”

42236 45 33 6714 98

pos_akhir

index

4

3

N 8 did_swap true

0 1 2 3 4 5 6 7

Page 51: Sorting   bubble-sort anim

The Third “Bubble Up”

42236 45 33 6714 98

pos_akhir

index4

3

N 8 did_swap true

Swap

0 1 2 3 4 5 6 7

Page 52: Sorting   bubble-sort anim

The Third “Bubble Up”

42236 33 45 6714 98

pos_akhir

index4

3

N 8 did_swap true

Swap

0 1 2 3 4 5 6 7

Page 53: Sorting   bubble-sort anim

The Third “Bubble Up”

42236 33 45 6714 98

pos_akhir

index

4

4

N 8 did_swap true

0 1 2 3 4 5 6 7

Page 54: Sorting   bubble-sort anim

The Third “Bubble Up”

42236 33 45 6714 98

pos_akhir

index4

4

N 8 did_swap true

Swap

0 1 2 3 4 5 6 7

Page 55: Sorting   bubble-sort anim

The Third “Bubble Up”

45236 33 42 6714 98

pos_akhir

index

4

4

N 8 did_swap true

Swap

0 1 2 3 4 5 6 7

Page 56: Sorting   bubble-sort anim

After Third Pass of Outer Loop

45236 33 42 6714 98

pos_akhir

index4

5

N 8 did_swap true

Finished third “Bubble Up”

0 1 2 3 4 5 6 7

Page 57: Sorting   bubble-sort anim

The Fourth “Bubble Up”

45236 33 42 6714 98

pos_akhir

index

3

0

N 8 did_swap false

0 1 2 3 4 5 6 7

Page 58: Sorting   bubble-sort anim

The Fourth “Bubble Up”

45236 33 42 6714 98

pos_akhir

index

3

0

N 8 did_swap false

Swap

0 1 2 3 4 5 6 7

Page 59: Sorting   bubble-sort anim

The Fourth “Bubble Up”

452314 33 42 676 98

pos_akhir

index

3

0

N 8 did_swap true

Swap

0 1 2 3 4 5 6 7

Page 60: Sorting   bubble-sort anim

The Fourth “Bubble Up”

452314 33 42 676 98

pos_akhir

index

3

1

N 8 did_swap true

0 1 2 3 4 5 6 7

Page 61: Sorting   bubble-sort anim

The Fourth “Bubble Up”

452314 33 42 676 98

pos_akhir

index

3

1

N 8 did_swap true

No Swap

0 1 2 3 4 5 6 7

Page 62: Sorting   bubble-sort anim

The Fourth “Bubble Up”

452314 33 42 676 98

pos_akhir

index

3

2

N 8 did_swap true

0 1 2 3 4 5 6 7

Page 63: Sorting   bubble-sort anim

The Fourth “Bubble Up”

452314 33 42 676 98

pos_akhir

index

3

2

N 8 did_swap true

No Swap

0 1 2 3 4 5 6 7

Page 64: Sorting   bubble-sort anim

The Fourth “Bubble Up”

452314 33 42 676 98

pos_akhir

index3

3

N 8 did_swap true

0 1 2 3 4 5 6 7

Page 65: Sorting   bubble-sort anim

The Fourth “Bubble Up”

452314 33 42 676 98

pos_akhir

index

3

3

N 8 did_swap true

No Swap

0 1 2 3 4 5 6 7

Page 66: Sorting   bubble-sort anim

After Fourth Pass of Outer Loop

452314 33 42 676 98

pos_akhir

index

3

4

N 8 did_swap true

Finished fourth “Bubble Up”

0 1 2 3 4 5 6 7

Page 67: Sorting   bubble-sort anim

The Fifth “Bubble Up”

452314 33 42 676 98

pos_akhir

index

2

0

N 8 did_swap false

0 1 2 3 4 5 6 7

Page 68: Sorting   bubble-sort anim

The Fifth “Bubble Up”

452314 33 42 676 98

pos_akhir

index2

0

N 8 did_swap false

No Swap

0 1 2 3 4 5 6 7

Page 69: Sorting   bubble-sort anim

The Fifth “Bubble Up”

452314 33 42 676 98

pos_akhir

index

2

1

N 8 did_swap false

0 1 2 3 4 5 6 7

Page 70: Sorting   bubble-sort anim

The Fifth “Bubble Up”

452314 33 42 676 98

pos_akhir

index

2

1

N 8 did_swap false

No Swap

0 1 2 3 4 5 6 7

Page 71: Sorting   bubble-sort anim

The Fifth “Bubble Up”

452314 33 42 676 98

pos_akhir

index

2

2

N 8 did_swap false

0 1 2 3 4 5 6 7

Page 72: Sorting   bubble-sort anim

The Fifth “Bubble Up”

452314 33 42 676 98

pos_akhir

index2

2

N 8 did_swap false

No Swap

0 1 2 3 4 5 6 7

Page 73: Sorting   bubble-sort anim

After Fifth Pass of Outer Loop

452314 33 42 676 98

pos_akhir

index2

3

N 8 did_swap false

Finished fifth “Bubble Up”

0 1 2 3 4 5 6 7

Page 74: Sorting   bubble-sort anim

Finished “Early”

452314 33 42 676 98

pos_akhir

index

2

3

N 8 did_swap false

We didn’t do any swapping,so all of the other elementsmust be correctly placed.

We can “skip” the last twopasses of the outer loop.

0 1 2 3 4 5 6 7