You are on page 1of 2

Bubble sort:

You have a list of numbers that we need to order from smallest to largest: 8 7 2 4 1
On the first pass the computer will compare each two and make a switch:
First step:
7 8 2 4 1
Second step:
7 2 8 4 1
Third step:
7 2 4 8 1
Fourth step:
72418
So now that it completed the list it will go again:
1st: 2 7 4 1 8
2nd: 2 4 7 1 8
3rd: 2 4 1 7 8
4th: 2 4 1 7 8 ( no switch)
The list is still not sorted so it will go again:
1st: 2 4 1 7 8 ( no switch)
2nd: 2 1 4 7 8
3rd: 2 1 4 7 8 ( no switch)
4th: 2 1 4 7 8 ( no switch)
The list is still not sorted so it will go again:
1st: 1 2 4 7 8
2nd: 1 2 4 7 8 ( no switch)
3rd: 1 2 4 7 8 ( no switch)
4th: 1 2 4 7 8 ( no switch) The list is now sorted
A different view:
Compare each 2 and make a switch if needed
First round New list Second round
step 1st 2nd rd
3 4th
step 1st 2nd 3rd 4th
8 7 7 7 7 7 2 2 2
7 8 2 2 2 2 7 4 4
2 2 8 4 4 4 4 7 1
4 4 8 1 1 1 7 No
1 1 8 8 8 switch

New list Third round New list Fourth round


step 1st 2nd 3rd 4 th
step 1st 2nd 3rd 4th
2 2 2 2 2 1 1 1
4 4 1 1 1 2 2 2
1 1 4 4 4 4 4 4
No
7 7 7 7 switch
7 7
No No
8 switch
8 switch 8 No
8 No
switch switch

Insertion Sort:
You have a list of numbers:
8 7 2 4 1
Step 1 : 7 8 2 4 1
Step 2 : 2 7 8 4 1
Step 3 : 2 4 7 8 1
Step 4 : 1 2 4 7 8
Each step it picks the next number and inserts it in its correct position.
A different view:
Step 1st 2nd 3rd 4th Finally
8 8 7 2 2 1
7 8 7 4 2
2 8 7 4
4 8 7
1 8

You might also like