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

Bubble n Insertion Algorithm

The document provides an overview of Bubble Sort and Insertion Sort algorithms, including their definitions, workings, and implementations in Visual Basic (VB) code. It explains how Bubble Sort repeatedly compares and swaps adjacent elements to sort an array, while Insertion Sort builds a sorted list by inserting elements into their correct positions. The document includes detailed pseudocode and VB code examples for both sorting algorithms.

Uploaded by

tinasherufudza18
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)
6 views

Bubble n Insertion Algorithm

The document provides an overview of Bubble Sort and Insertion Sort algorithms, including their definitions, workings, and implementations in Visual Basic (VB) code. It explains how Bubble Sort repeatedly compares and swaps adjacent elements to sort an array, while Insertion Sort builds a sorted list by inserting elements into their correct positions. The document includes detailed pseudocode and VB code examples for both sorting algorithms.

Uploaded by

tinasherufudza18
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/ 9

P4 Sec 4.1.

2) Bubble Sort & Insertion Sort Algorithms & VB Code Computer Science 9608
with Majid Tahir

Syllabus Content:
4.1.2 Algorithms (Bubble sort & insertion sort)

write an algorithm to implement an insertion sort


write an algorithm to implement a bubble 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.

The other values may or may not be in the correct order.


We need to work through the array again and again. After each pass through the array the next
largest value will be in its correct position, as shown in Figure below.

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.

Bubble Sort Algorithm:


n Maxindex - 1
REPEAT
NoMoreSwaps TRUE
FOR j 1 TO n
IF MyList [j] > MyList [j + l]
THEN Temp MyList[j]
MyList[j] MyList[j + 1]
MyList[j + 1] Temp
NoMoreSwaps FALSE
ENDIF
END FOR
n n - 1

UNTIL NoMoreSwaps TRUE

Bubble Sort Algorithm using VB Console Mode:


In this tutorial, i will teach you how to create a program for bubble sorting using vb.net console.
We all know that bubble sort is a sorting algorithm that is repeatedly searching through lists that
need to be sorted, comparing each pair of items and swapping them if they are in the wrong
order.
Now, let's start this tutorial!
1. Let's start with creating a Console Application for this tutorial by following the following
steps in Microsoft Visual Studio: Go to File, click New Project, and choose Console
Application.
2. Name your project as bubbleSort as your module.
Note: This is a console application so we cannot have visual controls for this tutorial.
3. Add the following code in your module.

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.

1. Sub sorting(ByVal x() As Integer, ByVal y As Integer)


2. Dim i, a, t As Integer
3. For i = 0 To y - 1

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

5. For entering number of elements, put this code below.

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

Total Code Together: (Code can be copied and tried in VB)


Module Module1
Sub sorting(ByVal x() As Integer, ByVal y As Integer)
Dim i, a, t As Integer
For i = 0 To y - 1
For a = i + 1 To y - 1
If x(i) > x(a) Then
t = x(i)
x(i) = x(a)
x(a) = t
End If
Next
Next
End Sub

Sub Main()
Console.WriteLine("Bubble Sorting")
Console.WriteLine()

Dim num, i As Integer


Console.Write("Enter Number of Elements: ")

num = CInt(Console.ReadLine)

Dim arr(num) As Integer


Console.WriteLine()

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

Definition - What does Insertion Sort mean?

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.

Techopedia explains Insertion Sort

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

// Sort an array[ ] of size n


insertionSort(arr, n)
Loop from i = 1 to n-1.
……a) Pick element arr[i] and insert it into sorted sequence arr [0…i-1]

Example:

Insertion Sort Algorithm


We can write this algorithm using pseudocode. Assume the values to be sorted are stored in a
10 array, List:
FOR Pointer 2 TO NumberOfitems
ItemToBeinserted List [Pointer]
Currentitem Pointer - 1 //pointer to last item in sorted part of list

WHILE (List[Currentitem] > ItemToBeinserted) AND (Currentitem > 0)


List [Currentitem + l] List [Currentitem] //move current item down
Currentitem Currentitem - 1 //look at the item above
ENDWHILE

List[Currentitem + l] ItemToBeinserted //insert item


END FOR

7
P4 Sec 4.1.2) Bubble Sort & Insertion Sort Algorithms & VB Code Computer Science 9608
with Majid Tahir

Insertion sort in VB Console Mode:


Module Module1
Sub Main()
Dim arr() As Integer = New Integer() {100, 12, 320, 34, 45, 90}
'sort the array using insertion sort
insertionSort(arr, arr.Length)

Dim i As Integer
For i = 0 To arr.Length - 1
Console.WriteLine(arr(i))
Next

Console.ReadLine() 'wait for keypress


End Sub

Sub insertionSort(ByVal dataset() As Integer, ByVal n As Integer)


Dim i, j As Integer
For i = 1 To n - 1 Step 1
Dim pick_item As Integer = dataset(i)
Dim inserted As Integer = 0
j = i - 1
While (j >= 0 And inserted <> 1)
If (pick_item < dataset(j)) Then
dataset(j + 1) = dataset(j)
j -= 1
dataset(j + 1) = pick_item
Else : inserted = 1
End If
End While
Next
End Sub
End Module
Output:

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

You might also like