0% found this document useful (0 votes)
10 views

Insertion and Bubble Sort (A-Level Compsci)

Uploaded by

Mal Eficent
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Insertion and Bubble Sort (A-Level Compsci)

Uploaded by

Mal Eficent
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

AS & A LEVEL COMPUTER SCIENCE 9618

INSERTION SORT

Re- arranging items by inserting in the correct position


Steps
1. It starts from the second digit
2. Compare the digit to the current position-1
3. If digit at current position is less than digit at compared position then
• Digit at current position will be extracted and all digits from compared
position to the current position will be shifted right
4. Extracted digit will then be placed at compared position
5. Current loop is discontinued

1 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618

INSERTION SORT
1 2 3 4 5
9 3 5 2 4

Consider Element in position 2


Is 3 < 9 ?
YES
1. TempStorage = 3
2. Shift 9 to right
3. Position1 will be empty
4. Store 3 in position 1

2 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618

INSERTION SORT
1 2 3 4 5
3 9 5 2 4

Consider Element in position 3


Where 5 should be inserted
Between 3 and 9
1. TempStorage = 5
2. Shift 9 to right
3. Insert 5 between 3 and 9

3 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618

INSERTION SORT
1 2 3 4 5
3 5 9 2 4

Consider Element in position 4


Where element 2 should be inserted
1. TempStorage = 2
2. Shift element in position 1, 2 and 3 to right to right
3. Insert 2 in position 1

4 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618

INSERTION SORT
1 2 3 4 5
2 3 5 9 4

Consider Element in position 5


Where 4 should be inserted
1. TempStorage = 4
2. Shift element in position 2 and 3 to right to right
3. Insert 4 in position 3

2 3 4 5 9

5 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618

PSEUDOCODE EXAMPLE

1 2 3 4 5 6 7 8 9 10 Positions
11 12 25 33 52 56 57 59 91 85 Elements

ArraySize = 10
FOR Pointer = 2 TO ArraySize
ValueToSort = CardData[Pointer] Temporary storing
PositionToInsert = Pointer
WHILE ( PositionToInsert > 1 ) AND ( CardData[PositionToInsert – 1 >
ValueToSort )
CardData[ PositionToInsert] = CardData[PositionToInsert – 1]
PositionToInsert = PositionToInsert -1
ENDWHILE Shifting
CardData[PositionToInsert] = ValueToSort
NEXT
Inserting Element

6 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618

DRY RUN

Note: If elements are already sorted, While Loop will be skipped straight to the
array initialization statement outside the while loop

7 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618

DRY RUN

8 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618

SITUATION AND REASONS WHEN INSERTION SORT IS KORE EFFICEINT


THAT ABUBBLE SORT

BUUBLE SORT
Situation: When a list is almost sorted
Reason: Because it stops as soon as it is sorted

INSERTION SORT
Situation: When there are large numbers of data items
Reason: Because it will perform fewer comparisons

9 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618

INSERTION SORT

10 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618

INSERTION SORT

11 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618

INSERTION SORT

12 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618

BUBBLE SORT
Consider StudentID array containing 100 elements.
Boundary = 99
REPEAT
NoMoreSwaps = TRUE
FOR m = 1 TO Boundary
IF StudentID(m) > StudentID(m+1)
THEN
TempStorage = StudentID(m)
StudentID(m) = StudentID(m+1)
StudentID(m+1) = TempStorage
NoMoreSwaps = FALSE
ENDIF
NEXT
Boundary = Boundary - 1
UNTILL NoMoreSwaps = TRUE

13 Davis_Kazibwe@2023KIS

You might also like