Sorting Algorithms
1. Selection
2. Bubble
3. Insertion
4. Merge
5. Quick
6. Shell
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
An Animated Example
674523 14 6 3398 42
pos_akhir
index
6
0
N 8 did_swap false
0 1 2 3 4 5 6 7
An Animated Example
674523 14 6 3398 42
pos_akhir
index
6
0
N 8
Swap
did_swap false
0 1 2 3 4 5 6 7
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
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
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
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
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
An Animated Example
679845 14 6 3323 42
pos_akhir
index
6
2
N 8
Swap
did_swap true
0 1 2 3 4 5 6 7
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
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
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
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
An Animated Example
671445 6 98 3323 42
pos_akhir
index
6
4
N 8 did_swap true
0 1 2 3 4 5 6 7
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
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
An Animated Example
981445 6 67 3323 42
pos_akhir
index
6
5
N 8 did_swap true
0 1 2 3 4 5 6 7
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
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
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
An Animated Example
331445 6 67 9823 42
pos_akhir
index
6
6
N 8
Swap
did_swap true
0 1 2 3 4 5 6 7
An Animated Example
331445 6 67 4223 98
pos_akhir
index
6
6
N 8
Swap
did_swap true
0 1 2 3 4 5 6 7
After First Pass of Outer Loop
331445 6 67 4223 98
pos_akhir
index
6
7
N 8
Finished first “Bubble Up”
did_swap true
0 1 2 3 4 5 6 7
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
The Second “Bubble Up”
331445 6 67 4223 98
pos_akhir
index
5
0
N 8 did_swap false
No Swap
0 1 2 3 4 5 6 7
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
The Second “Bubble Up”
331445 6 67 4223 98
pos_akhir
index
5
1
N 8 did_swap false
Swap
0 1 2 3 4 5 6 7
The Second “Bubble Up”
334514 6 67 4223 98
pos_akhir
index
5
1
N 8 did_swap true
Swap
0 1 2 3 4 5 6 7
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
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
The Second “Bubble Up”
33614 45 67 4223 98
pos_akhir
index
5
2
N 8 did_swap true
Swap
0 1 2 3 4 5 6 7
The Second “Bubble Up”
33614 45 67 4223 98
pos_akhir
index
5
3
N 8 did_swap true
0 1 2 3 4 5 6 7
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
The Second “Bubble Up”
33614 45 67 4223 98
pos_akhir
index
5
4
N 8 did_swap true
0 1 2 3 4 5 6 7
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
The Second “Bubble Up”
67614 45 33 4223 98
pos_akhir
index
5
4
N 8 did_swap true
Swap
0 1 2 3 4 5 6 7
The Second “Bubble Up”
67614 45 33 4223 98
pos_akhir
index
5
5
N 8 did_swap true
0 1 2 3 4 5 6 7
The Second “Bubble Up”
67614 45 33 4223 98
pos_akhir
index
5
5
N 8 did_swap true
Swap
0 1 2 3 4 5 6 7
The Second “Bubble Up”
42614 45 33 6723 98
pos_akhir
index
5
5
N 8 did_swap true
Swap
0 1 2 3 4 5 6 7
After Second Pass of Outer
Loop
42614 45 33 6723 98
pos_akhir
index
5
6
N 8 did_swap true
Finished second “Bubble Up”
0 1 2 3 4 5 6 7
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
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
The Third “Bubble Up”
42623 45 33 6714 98
pos_akhir
index
4
0
N 8 did_swap true
Swap
0 1 2 3 4 5 6 7
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
The Third “Bubble Up”
42623 45 33 6714 98
pos_akhir
index
4
1
N 8 did_swap true
Swap
0 1 2 3 4 5 6 7
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
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
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
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
The Third “Bubble Up”
42236 45 33 6714 98
pos_akhir
index
4
3
N 8 did_swap true
Swap
0 1 2 3 4 5 6 7
The Third “Bubble Up”
42236 33 45 6714 98
pos_akhir
index
4
3
N 8 did_swap true
Swap
0 1 2 3 4 5 6 7
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
The Third “Bubble Up”
42236 33 45 6714 98
pos_akhir
index
4
4
N 8 did_swap true
Swap
0 1 2 3 4 5 6 7
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
After Third Pass of Outer Loop
45236 33 42 6714 98
pos_akhir
index
4
5
N 8 did_swap true
Finished third “Bubble Up”
0 1 2 3 4 5 6 7
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
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
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
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
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
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
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
The Fourth “Bubble Up”
452314 33 42 676 98
pos_akhir
index
3
3
N 8 did_swap true
0 1 2 3 4 5 6 7
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
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
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
The Fifth “Bubble Up”
452314 33 42 676 98
pos_akhir
index
2
0
N 8 did_swap false
No Swap
0 1 2 3 4 5 6 7
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
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
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
The Fifth “Bubble Up”
452314 33 42 676 98
pos_akhir
index
2
2
N 8 did_swap false
No Swap
0 1 2 3 4 5 6 7
After Fifth Pass of Outer Loop
452314 33 42 676 98
pos_akhir
index
2
3
N 8 did_swap false
Finished fifth “Bubble Up”
0 1 2 3 4 5 6 7
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 elements
must be correctly placed.
We can “skip” the last two
passes of the outer loop.
0 1 2 3 4 5 6 7

Sorting bubble-sort anim

  • 1.
    Sorting Algorithms 1. Selection 2.Bubble 3. Insertion 4. Merge 5. Quick 6. Shell
  • 2.
    An Animated Example 67452314 6 3398 42 0 1 2 3 4 5 6 7 pos_akhir index 6 N 8 did_swap true
  • 3.
    An Animated Example 67452314 6 3398 42 pos_akhir index 6 0 N 8 did_swap false 0 1 2 3 4 5 6 7
  • 4.
    An Animated Example 67452314 6 3398 42 pos_akhir index 6 0 N 8 Swap did_swap false 0 1 2 3 4 5 6 7
  • 5.
    An Animated Example 67459814 6 3323 42 pos_akhir index 6 0 N 8 Swap did_swap true 0 1 2 3 4 5 6 7
  • 6.
    An Animated Example 67459814 6 3323 42 pos_akhir index 6 1 N 8 did_swap true 0 1 2 3 4 5 6 7
  • 7.
    An Animated Example 67459814 6 3323 42 pos_akhir index 6 1 N 8 Swap did_swap true 0 1 2 3 4 5 6 7
  • 8.
    An Animated Example 67984514 6 3323 42 pos_akhir index 6 1 N 8 Swap did_swap true 0 1 2 3 4 5 6 7
  • 9.
    An Animated Example 67984514 6 3323 42 pos_akhir index 6 2 N 8 did_swap true 0 1 2 3 4 5 6 7
  • 10.
    An Animated Example 67984514 6 3323 42 pos_akhir index 6 2 N 8 Swap did_swap true 0 1 2 3 4 5 6 7
  • 11.
    An Animated Example 67144598 6 3323 42 pos_akhir index 6 2 N 8 Swap did_swap true 0 1 2 3 4 5 6 7
  • 12.
    An Animated Example 67144598 6 3323 42 pos_akhir index 6 3 N 8 did_swap true 0 1 2 3 4 5 6 7
  • 13.
    An Animated Example 67144598 6 3323 42 pos_akhir index 6 3 N 8 Swap did_swap true 0 1 2 3 4 5 6 7
  • 14.
    An Animated Example 6714456 98 3323 42 pos_akhir index 6 3 N 8 Swap did_swap true 0 1 2 3 4 5 6 7
  • 15.
    An Animated Example 6714456 98 3323 42 pos_akhir index 6 4 N 8 did_swap true 0 1 2 3 4 5 6 7
  • 16.
    An Animated Example 6714456 98 3323 42 pos_akhir index 6 4 N 8 Swap did_swap true 0 1 2 3 4 5 6 7
  • 17.
    An Animated Example 9814456 67 3323 42 pos_akhir index 6 4 N 8 Swap did_swap true 0 1 2 3 4 5 6 7
  • 18.
    An Animated Example 9814456 67 3323 42 pos_akhir index 6 5 N 8 did_swap true 0 1 2 3 4 5 6 7
  • 19.
    An Animated Example 9814456 67 3323 42 pos_akhir index 6 5 N 8 Swap did_swap true 0 1 2 3 4 5 6 7
  • 20.
    An Animated Example 3314456 67 9823 42 pos_akhir index 6 5 N 8 Swap did_swap true 0 1 2 3 4 5 6 7
  • 21.
    An Animated Example 3314456 67 9823 42 pos_akhir index 6 6 N 8 did_swap true 0 1 2 3 4 5 6 7
  • 22.
    An Animated Example 3314456 67 9823 42 pos_akhir index 6 6 N 8 Swap did_swap true 0 1 2 3 4 5 6 7
  • 23.
    An Animated Example 3314456 67 4223 98 pos_akhir index 6 6 N 8 Swap did_swap true 0 1 2 3 4 5 6 7
  • 24.
    After First Passof Outer Loop 331445 6 67 4223 98 pos_akhir index 6 7 N 8 Finished first “Bubble Up” did_swap true 0 1 2 3 4 5 6 7
  • 25.
    The Second “BubbleUp” 331445 6 67 4223 98 pos_akhir index 5 0 N 8 did_swap false 0 1 2 3 4 5 6 7
  • 26.
    The Second “BubbleUp” 331445 6 67 4223 98 pos_akhir index 5 0 N 8 did_swap false No Swap 0 1 2 3 4 5 6 7
  • 27.
    The Second “BubbleUp” 331445 6 67 4223 98 pos_akhir index 5 1 N 8 did_swap false 0 1 2 3 4 5 6 7
  • 28.
    The Second “BubbleUp” 331445 6 67 4223 98 pos_akhir index 5 1 N 8 did_swap false Swap 0 1 2 3 4 5 6 7
  • 29.
    The Second “BubbleUp” 334514 6 67 4223 98 pos_akhir index 5 1 N 8 did_swap true Swap 0 1 2 3 4 5 6 7
  • 30.
    The Second “BubbleUp” 334514 6 67 4223 98 pos_akhir index 5 2 N 8 did_swap true 0 1 2 3 4 5 6 7
  • 31.
    The Second “BubbleUp” 334514 6 67 4223 98 pos_akhir index 5 2 N 8 did_swap true Swap 0 1 2 3 4 5 6 7
  • 32.
    The Second “BubbleUp” 33614 45 67 4223 98 pos_akhir index 5 2 N 8 did_swap true Swap 0 1 2 3 4 5 6 7
  • 33.
    The Second “BubbleUp” 33614 45 67 4223 98 pos_akhir index 5 3 N 8 did_swap true 0 1 2 3 4 5 6 7
  • 34.
    The Second “BubbleUp” 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
  • 35.
    The Second “BubbleUp” 33614 45 67 4223 98 pos_akhir index 5 4 N 8 did_swap true 0 1 2 3 4 5 6 7
  • 36.
    The Second “BubbleUp” 33614 45 67 4223 98 pos_akhir index 5 4 N 8 did_swap true Swap 0 1 2 3 4 5 6 7
  • 37.
    The Second “BubbleUp” 67614 45 33 4223 98 pos_akhir index 5 4 N 8 did_swap true Swap 0 1 2 3 4 5 6 7
  • 38.
    The Second “BubbleUp” 67614 45 33 4223 98 pos_akhir index 5 5 N 8 did_swap true 0 1 2 3 4 5 6 7
  • 39.
    The Second “BubbleUp” 67614 45 33 4223 98 pos_akhir index 5 5 N 8 did_swap true Swap 0 1 2 3 4 5 6 7
  • 40.
    The Second “BubbleUp” 42614 45 33 6723 98 pos_akhir index 5 5 N 8 did_swap true Swap 0 1 2 3 4 5 6 7
  • 41.
    After Second Passof Outer Loop 42614 45 33 6723 98 pos_akhir index 5 6 N 8 did_swap true Finished second “Bubble Up” 0 1 2 3 4 5 6 7
  • 42.
    The Third “BubbleUp” 42614 45 33 6723 98 pos_akhir index 4 0 N 8 did_swap false 0 1 2 3 4 5 6 7
  • 43.
    The Third “BubbleUp” 42614 45 33 6723 98 pos_akhir index 4 0 N 8 did_swap false Swap 0 1 2 3 4 5 6 7
  • 44.
    The Third “BubbleUp” 42623 45 33 6714 98 pos_akhir index 4 0 N 8 did_swap true Swap 0 1 2 3 4 5 6 7
  • 45.
    The Third “BubbleUp” 42623 45 33 6714 98 pos_akhir index 4 1 N 8 did_swap true 0 1 2 3 4 5 6 7
  • 46.
    The Third “BubbleUp” 42623 45 33 6714 98 pos_akhir index 4 1 N 8 did_swap true Swap 0 1 2 3 4 5 6 7
  • 47.
    The Third “BubbleUp” 42236 45 33 6714 98 pos_akhir index 4 1 N 8 did_swap true Swap 0 1 2 3 4 5 6 7
  • 48.
    The Third “BubbleUp” 42236 45 33 6714 98 pos_akhir index 4 2 N 8 did_swap true 0 1 2 3 4 5 6 7
  • 49.
    The Third “BubbleUp” 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
  • 50.
    The Third “BubbleUp” 42236 45 33 6714 98 pos_akhir index 4 3 N 8 did_swap true 0 1 2 3 4 5 6 7
  • 51.
    The Third “BubbleUp” 42236 45 33 6714 98 pos_akhir index 4 3 N 8 did_swap true Swap 0 1 2 3 4 5 6 7
  • 52.
    The Third “BubbleUp” 42236 33 45 6714 98 pos_akhir index 4 3 N 8 did_swap true Swap 0 1 2 3 4 5 6 7
  • 53.
    The Third “BubbleUp” 42236 33 45 6714 98 pos_akhir index 4 4 N 8 did_swap true 0 1 2 3 4 5 6 7
  • 54.
    The Third “BubbleUp” 42236 33 45 6714 98 pos_akhir index 4 4 N 8 did_swap true Swap 0 1 2 3 4 5 6 7
  • 55.
    The Third “BubbleUp” 45236 33 42 6714 98 pos_akhir index 4 4 N 8 did_swap true Swap 0 1 2 3 4 5 6 7
  • 56.
    After Third Passof Outer Loop 45236 33 42 6714 98 pos_akhir index 4 5 N 8 did_swap true Finished third “Bubble Up” 0 1 2 3 4 5 6 7
  • 57.
    The Fourth “BubbleUp” 45236 33 42 6714 98 pos_akhir index 3 0 N 8 did_swap false 0 1 2 3 4 5 6 7
  • 58.
    The Fourth “BubbleUp” 45236 33 42 6714 98 pos_akhir index 3 0 N 8 did_swap false Swap 0 1 2 3 4 5 6 7
  • 59.
    The Fourth “BubbleUp” 452314 33 42 676 98 pos_akhir index 3 0 N 8 did_swap true Swap 0 1 2 3 4 5 6 7
  • 60.
    The Fourth “BubbleUp” 452314 33 42 676 98 pos_akhir index 3 1 N 8 did_swap true 0 1 2 3 4 5 6 7
  • 61.
    The Fourth “BubbleUp” 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
  • 62.
    The Fourth “BubbleUp” 452314 33 42 676 98 pos_akhir index 3 2 N 8 did_swap true 0 1 2 3 4 5 6 7
  • 63.
    The Fourth “BubbleUp” 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
  • 64.
    The Fourth “BubbleUp” 452314 33 42 676 98 pos_akhir index 3 3 N 8 did_swap true 0 1 2 3 4 5 6 7
  • 65.
    The Fourth “BubbleUp” 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
  • 66.
    After Fourth Passof 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
  • 67.
    The Fifth “BubbleUp” 452314 33 42 676 98 pos_akhir index 2 0 N 8 did_swap false 0 1 2 3 4 5 6 7
  • 68.
    The Fifth “BubbleUp” 452314 33 42 676 98 pos_akhir index 2 0 N 8 did_swap false No Swap 0 1 2 3 4 5 6 7
  • 69.
    The Fifth “BubbleUp” 452314 33 42 676 98 pos_akhir index 2 1 N 8 did_swap false 0 1 2 3 4 5 6 7
  • 70.
    The Fifth “BubbleUp” 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
  • 71.
    The Fifth “BubbleUp” 452314 33 42 676 98 pos_akhir index 2 2 N 8 did_swap false 0 1 2 3 4 5 6 7
  • 72.
    The Fifth “BubbleUp” 452314 33 42 676 98 pos_akhir index 2 2 N 8 did_swap false No Swap 0 1 2 3 4 5 6 7
  • 73.
    After Fifth Passof Outer Loop 452314 33 42 676 98 pos_akhir index 2 3 N 8 did_swap false Finished fifth “Bubble Up” 0 1 2 3 4 5 6 7
  • 74.
    Finished “Early” 452314 3342 676 98 pos_akhir index 2 3 N 8 did_swap false We didn’t do any swapping, so all of the other elements must be correctly placed. We can “skip” the last two passes of the outer loop. 0 1 2 3 4 5 6 7