Common Sorting Algorithms Explained
Common Sorting Algorithms Explained
Sorting Algorithms:
Sorting algorithms help arrange data in order, either from smallest to largest (ascending) or largest to smallest
(descending). Some common sorting methods include Insertion Sort and Bubble Sort.
Insertion Sort works by comparing two nearby numbers and placing them in the correct order. The
smallest number is picked and moved to the first position. This process continues until all numbers are
sorted in order.
Here is a group of numbers that we need to sort in ascending order (smallest to largest).
10 30 22 7 35 15
10 30 22 7 35 15
We pick the second element (30) and compare it with the adjacent element in the sorted array.
10 30 22 7 35 15
Because 30 is larger than 10, it means that the first element is already in the correct order.
10 30 22 7 35 15
Now, move to the next element (22) and compare it with the adjacent element in the sorted array
(22 with 30).
10 30 22 7 35 15
Here, 22 is smaller compared to 30. It means 30 is not in the correct position. So, we swap both
elements, 22 with 30.
10 22 30 7 35 15
Along with swapping, we also need to check and compare it with all elements in the sorted array.
Till now, the sorted array contains only one element (10). So, 22 is compared with 10. Here, 22
is greater than 10, so the sorted array remains the same.
Now, we move forward to the next element, which is 7, and compare it with its adjacent element
in the sorted array (30).
10 22 30 7 35 15
10 22 7 30 35 15
After swapping, we notice that elements 22 and 7 are not in the correct order.
10 22 7 30 35 15
10 7 22 30 35 15
10 7 22 30 35 15
7 10 22 30 35 15
Now, we move to the next element, 35, and compare it with the adjacent element in the sorted
array, which is 30.
7 10 22 30 35 15
Similarly, 35 is compared with the previous element in the sorted array, but there is no change
because they are already sorted.
7 10 22 30 35 15
Here we will take the next element that is 15 and compared with its adjacent element which is 35
7 10 22 30 35 15
Let's solve the Insertion sort by applying the computational thinking properties.
Insertion Sort arranges a list by picking one item at a time and placing it in the correct
position among the already sorted items. This process continues until the whole list is
sorted.
Steps Involved:
Pick and Insert: Take an item and place it in the correct position in the sorted
part of the list.
Finding the Right Spot: Compare the item with others to find where it belongs.
SCHOLARS LECTURE NOTES – 1st YEAR (COMPUTER SCIENCE)
SCHOLARS RESEARCH CENTRE
CHAPTER 2 COMPUTATIONAL THINKING & ALGORITHM
Algorithm Steps:
If we want to sort an array in ascending order, the algorithm starts by comparing the first
element with the second.
If the first element is greater than the second, they are swapped.
Then, it moves forward to compare the second element with the third, continuing this process
until the largest element reaches the last position in the array.
This process is repeated (iteration) until the entire list is sorted.
First Iteration:
Here, the first element 11 is smaller than second element 30, so it is already sorted.
Now compare second element 30 with its adjacent element 25.
11 30 25 40 7
Now take third element 30 and compare it with next adjacent element 40.
11 25 30 40 7
(Note: The greatest number 40 has reached at the last place of the array at the end of first
iteration)
Second Iteration:
11 25 30 7 40
11 25 30 7 40
11 25 7 30 40
Third Iteration:
11 25 7 30 40
11 7 25 30 40
11 7 25 30 40
Fourth Iteration:
Again follow the same process, here 11 is greater than 7 so swapping is needed.
11 7 25 30 40
Abstraction:
Bubble Sort organizes a group of items by repeatedly comparing and swapping two adjacent
items if they are out of order. This process continues until the entire list is sorted.
Decomposition:
Steps Involved:
Pattern Recognition:
Comparing Pairs: The algorithm focuses on checking and fixing two items at a time.
Repeating Steps: The process of comparing and swapping is done again and again until the list
is sorted.
Multiple Rounds: The algorithm goes through the list several times to sort it completely.
Algorithm:
3. Repeat this for every pair in the list until you reach the end.
4. After one round, the biggest item will be at the correct position at the end of the list.
5. Do the same process for the remaining unsorted items in the list.
6. Keep repeating until the entire list is sorted.
The number of rounds needed is one less than the total number of items.
Searching Algorithms:
Searching algorithms are methods used to find a specific element in a group of items. Common examples are
Binary Search and Linear Search.
Binary Search is a fast method for finding an item in a sorted list. It works by repeatedly dividing the
list into two parts until the desired item is found.
1. Start by comparing the middle element of the list with the item you are searching for.
2. If it matches, you’ve found your item.
3. If it’s smaller than the middle element, search in the left half of the list.
4. If it’s larger, search in the right half of the list.
5. Repeat this process until the item is found or the list is fully checked.
5 6 7 8
40 50 60 70
Again divide the array into two parts.
5 6 7 8
40 50 60 70
Abstraction:
Search for a specific item in a sorted collection by repeatedly dividing the search space in
half until the target item is found or the search space is empty. It efficiently narrows down
the search space by half in each iteration.
Decomposition:
Sub-Processes:
1. Middle Item Check: Examine the middle item to determine if it matches the target.
2. Search Space Division: Divide the search space based on the comparison result.
3. Recursive Process: Repeat the process on the narrowed-down search space.
Pattern Recognition: The core concept is repeatedly reducing the search space by half.
Algorithmic Design:
This is a simple search algorithm; we go through the complete list and match the elements one
by one until the required item is found. If the item is found, then the location of that element is
returned; otherwise, a NULL is returned. This search is also called a sequential search algorithm.
However, there is no compulsion that the array should be sorted.
Start from the first element. Compare the required item 77 with required item 12.
0 1 2 3 4
12 7 34 77 100
If it matches, stop; otherwise, continue to the next element. Repeat this process until the
item is found or the entire array is checked.
0 1 2 3 4
12 7 34 77 100
0 1 2 3 4
12 7 34 77 100
0 1 2 3 4
12 7 34 77 100
Abstraction:
Search for a specific item in a list by checking each item one by one until either
the item is found or the end of the list is reached. This method goes through each
item sequentially.
Decomposition:
1. Starting Point: Begin the search from the first item in the list.
2. Item Checking: Check each item to see if it matches the one you’re looking
for.
3. Stopping Condition: Stop the search when the target item is found or when
the end of the list is reached.
Pattern Recognition:
Algorithmic Design:
3. Repeat this process until the target is found or you reach the end of the list.
4. If you reach the end without finding the item, it means the item is not in
the list.
Question A B C D A B C D
Question A B C D A B C D
For example, in [5, 3, 8], compare 5 and 3 → swap them → [3, 5, 8].
6. Describe the behavior of Linear Search when the target element is not in the list. [ U.B]
Linear Search will check every element in the list. If the target is not found after scanning all elements,
it returns -1 or "not found."
For example, if the array is [1, 2, 3, 4] and the target is 5. Linear Search will check all elements: 1, 2, 3,
and 4. After reaching the end of the array without finding the target, it will return "not found.
7. Differentiate between Linear Search and Binary Search in Computational Thinking. [U.B]
Aspect Linear Search Binary Search
Breaks the problem into smaller
Problem steps by checking each element
Divides the search space into halves
Decomposition repeatedly.
sequentially.
Sequential approach: starts from
Algorithmic the first element and continues Uses a divide-and-conquer approach
Thinking until the target is found or the list to locate the target.
ends.
No abstraction required; works
Requires the abstraction of sorting
Abstraction directly on the list regardless of
the data before searching.
order.
8. Differentiate between Bubble Sort and Insertion Sort in Computational Thinking. [U.B]
Aspect Bubble Sort Insertion Sort
Decomposition Compare and swap adjacent Insert elements into the correct
elements. position.
Pattern Largest element "bubbles up."
Elements to the left are always
Recognition sorted.
2. Why is Insertion Sort more efficient than Bubble Sort for partially sorted datasets? Explain with an
example.
3. Sort the array [8, 4, 6, 2, 5] step-by-step using Bubble Sort and show all intermediate results.
4. Search for the element 25 in the sorted array [10, 20, 25, 30, 35] using Binary Search and show all steps.
5. Demonstrate Linear Search on the array [15, 10, 5, 20, 45] to find the element 10, showing each
comparison.