You are on page 1of 12

SORTING AND SEARCHING

MDEN SCHOOL OF PROGRAMMING


Objectives
By the end of this lesson students should be able to:
• Define the term sorting in programming terms
• Define and demonstrate the meaning of bubble sort.
• Demonstrate an ability to write pseudocode and program code
using either vb.net or python(pycharm) that demonstrates
bubble sorting elements in an array

6/8/2023 MDEN 2
Definition of terms
Sorting -is the process of arranging data into meaningful order so that you
can analyze it more effectively.
One can sort data as follows:
• sort text data into alphabetical order
• sort numeric data into numerical order
In programming terms we have different types of sorting, which are:
i. Bubble sort
ii. Radix sort
iii. Merge sort
iv. Insertion sort
For the purpose of 0478 computer systems our focus will be on bubble sort.

6/8/2023 MDEN 3
Bubble sort
▪ Bubble sort is a basic algorithm for arranging a string of
numbers or other elements in the correct order.

▪ The method works by examining each set of adjacent


elements in the string, from left to right, switching their
positions if they are out of order.

▪ The algorithm then repeats this process until it can run through
the entire string and find no two elements that need to be
swapped
6/8/2023 MDEN 4
Working of Bubble Sort
Suppose we are trying to sort the elements in ascending order.
1. First Iteration (Compare and Swap)
1.Starting from the first index, compare the first and the second
elements.
2.If the first element is greater than the second element, they are
swapped.
3.Now, compare the second and the third elements. Swap them if
they are not in order.
The above process goes on until the last element.
6/8/2023 MDEN 5
6/8/2023 MDEN 6
Step 1

Remaining Iteration
i. The same process goes on for the remaining
iterations.
ii. After each iteration, the largest element among
the unsorted elements is placed at the end.
iii. In each iteration, the comparison takes place up
to the last unsorted element.

6/8/2023 MDEN 7
Bubble sort algorithm

6/8/2023 MDEN 8
Identifier table

6/8/2023 MDEN 9
Program code using vb.net

6/8/2023 MDEN 10
Advantages and disadvantages

6/8/2023 MDEN 11
Thank you
Any questions

You might also like