Bubble n Insertion Algorithm
Bubble n Insertion Algorithm
2) Bubble Sort & Insertion Sort Algorithms & VB Code Computer Science 9608
with Majid Tahir
Syllabus Content:
4.1.2 Algorithms (Bubble sort & insertion sort)
Bubble Sort
When we have completed the first pass through the entire array, the largest value is in the
correct position at the end of the array.
1
P4 Sec 4.1.2) Bubble Sort & Insertion Sort Algorithms & VB Code Computer Science 9608
with Majid Tahir
In effect we perform a loop within a loop, a nested loop. This method is known as a bubblesort.
The name comes from the fact that smaller values slowly rise to the top, like bubbles in a liquid.
1. Module bubbleSort
2. Sub Main()
4. Make a function named sorting. This will automatically sort the inputted elements that
we will create in Sub Main.
2
P4 Sec 4.1.2) Bubble Sort & Insertion Sort Algorithms & VB Code Computer Science 9608
with Majid Tahir
4. For a = i + 1 To y - 1
5. If x(i) > x(a) Then
6. t = x(i)
7. x(i) = x(a)
8. x(a) = t
9. End If
10. Next
11. Next
12. End Sub
1. Console.WriteLine("Bubble Sorting")
2. Console.WriteLine()
3. Dim num, i As Integer
4. Console.Write("Enter Number of Elements: ")
5. num = CInt(Console.ReadLine)
6. Dim arr(num) As Integer
7. Console.WriteLine()
8. For i = 0 To num - 1
9. Console.Write("Enter Element(" & (i + 1) & "): ")
10. arr(i) = CInt(Console.ReadLine)
11. Next
6. For printing the inputted elements above, put this code below.
1. Console.WriteLine()
2. Console.WriteLine("Inputted Elements")
3. Console.WriteLine()
4. For i = 0 To num - 1
5. Console.WriteLine("Element in (" & i & "): " & arr(i))
6. Next
7. Lastly, we will code for the sorting of elements (bubble sort), put this code below.
1. Console.WriteLine()
2. sorting(arr, num)
3. Console.WriteLine("Sorted Elements")
4. Console.WriteLine()
5. For i = 0 To num - 1
6. Console.WriteLine("Element in (" & i & "): " & arr(i))
7. Next
8. Console.ReadLine()
3
P4 Sec 4.1.2) Bubble Sort & Insertion Sort Algorithms & VB Code Computer Science 9608
with Majid Tahir
Sub Main()
Console.WriteLine("Bubble Sorting")
Console.WriteLine()
num = CInt(Console.ReadLine)
For i = 0 To num - 1
Console.Write("Enter Element(" & (i + 1) & "): ")
arr(i) = CInt(Console.ReadLine)
Next
Console.WriteLine()
Console.WriteLine("Inputted Elements")
Console.WriteLine()
For i = 0 To num - 1
Console.WriteLine("Element in (" & i & "): " & arr(i))
Next
Console.WriteLine()
sorting(arr, num)
Console.WriteLine("Sorted Elements")
Console.WriteLine()
For i = 0 To num - 1
Console.WriteLine("Element in (" & i & "): " & arr(i))
Next
Console.ReadLine()
End Sub
End Module
4
P4 Sec 4.1.2) Bubble Sort & Insertion Sort Algorithms & VB Code Computer Science 9608
with Majid Tahir
Output:
Insertion sort:
Imagine you have a number of cards with a different value printed on each card. How would you
sort these cards into order of increasing value?
You can consider the pile of cards as consisting of a sorted part and an unsorted part. Place the
unsorted cards in a pile on the table. Hold the sorted cards as a pack in your hand. To start with
only the first (top) card is sorted. The card on the top of the pile on the table is the next card to
be inserted. The last (bottom) card in your hand is your current card.
Figure 23 .01 shows the sorted cards in your hand as blue and the pile of unsorted cards as
white. The next card to be inserted is shown in red. Each column shows the state of the pile as
the cards are sorted.
5
P4 Sec 4.1.2) Bubble Sort & Insertion Sort Algorithms & VB Code Computer Science 9608
with Majid Tahir
Insertion Sort
Insertion sort is a sorting algorithm in which the elements are transferred one at a time to the
right position. In other words, an insertion sort helps in building the final sorted list, one item at a
time, with the movement of higher-ranked elements. An insertion sort has the benefits of
simplicity and low overhead.
In an insertion sort, the first element in the array is considered as sorted, even if it is an
unsorted array. In an insertion sort, each element in the array is checked with the previous
elements, resulting in a growing sorted output list.
With each iteration, the sorting algorithm removes one element at a time and finds the
appropriate location within the sorted array and inserts it there. The iteration continues until the
whole list is sorted.
There are many advantages associated with an insertion sort. It is simple to implement and is
quite efficient for small sets of data, especially if it is substantially sorted. It has low overhead
and can sort the list as it receives data. Another advantage associated with insertion sort is the
fact that it needs only a constant amount of memory space for the whole operation.
It is more efficient than other similar algorithms such as bubble sort or selection sort.
However, an insertion sort is less efficient on larger data sets and less efficient than the heap
sort or quick sort algorithms.
Insertion sort is a simple sorting algorithm that works the way we sort playing cards in our
hands.
6
P4 Sec 4.1.2) Bubble Sort & Insertion Sort Algorithms & VB Code Computer Science 9608
with Majid Tahir
Algorithm
Example:
7
P4 Sec 4.1.2) Bubble Sort & Insertion Sort Algorithms & VB Code Computer Science 9608
with Majid Tahir
Dim i As Integer
For i = 0 To arr.Length - 1
Console.WriteLine(arr(i))
Next
8
P4 Sec 4.1.2) Bubble Sort & Insertion Sort Algorithms & VB Code Computer Science 9608
with Majid Tahir
References:
Computer Science AS & A Level Coursebook by Sylvia Langfield & Dave Duddell
https://www.dotnetperls.com/dictionary-vbnet
http://www.worldbestlearningcenter.com/index_files/vb.net-example-insertion-sort.htm