You are on page 1of 4

Bubble Sort

Bubble sort is a simple sorting algorithm that repeatedly steps through the list,
compares adjacent elements, and swaps them if they are in the wrong order. The pass
through the list is repeated until the list is sorted. Here are the step-by-step instructions
for the bubble sort algorithm:

1. Start with an unsorted list of elements.


2. Compare the first element with the second element. If the first element is greater
than the second element, swap them.
3. Move to the next pair of elements (second and third) and compare them. Again, if
the second element is greater than the third element, swap them.
4. Continue this process, comparing and swapping adjacent elements, until you
reach the end of the list.
5. After the first pass, the largest element will be in its correct position at the end of
the list.
6. Repeat steps 2-5 for the remaining elements, excluding the last element that is
already in its correct position.
7. Continue this process for each element in the list until the entire list is sorted.
Here's an example to illustrate the steps:

Unsorted list: [5, 3, 8, 2, 1]

Pass 1:

● Compare 5 and 3. Since 5 is greater than 3, swap them. Updated list: [3, 5, 8, 2,
1]
● Compare 5 and 8. No swap needed. List remains the same: [3, 5, 8, 2, 1]
● Compare 8 and 2. Since 8 is greater than 2, swap them. Updated list: [3, 5, 2, 8,
1]
● Compare 8 and 1. Since 8 is greater than 1, swap them. Updated list: [3, 5, 2, 1,
8]

Pass 2:

● Compare 3 and 5. No swap needed. List remains the same: [3, 5, 2, 1, 8]


● Compare 5 and 2. Since 5 is greater than 2, swap them. Updated list: [3, 2, 5, 1,
8]
● Compare 5 and 1. Since 5 is greater than 1, swap them. Updated list: [3, 2, 1, 5,
8]

Pass 3:
● Compare 3 and 2. Since 3 is greater than 2, swap them. Updated list: [2, 3, 1, 5,
8]
● Compare 3 and 1. Since 3 is greater than 1, swap them. Updated list: [2, 1, 3, 5,
8]

Pass 4:

● Compare 2 and 1. Since 2 is greater than 1, swap them. Updated list: [1, 2, 3, 5,
8]

The list is now sorted: [1, 2, 3, 5, 8]

YT link: https://youtu.be/xcPFUCh0jT0

Selection Sort

The selection sort algorithm is a simple sorting algorithm that works by repeatedly
finding the minimum element from the unsorted part of the array and putting it at the
beginning. Here are the step-by-step instructions for the selection sort algorithm:

1. Start with an unsorted array of elements.


2. Find the minimum element in the unsorted part of the array.
3. Swap the minimum element with the first element of the unsorted part.
4. Move the boundary of the sorted part one element to the right.
5. Repeat steps 2-4 until the entire array is sorted.
Let's go through an example to illustrate the steps:

Example: Consider the following array: [5, 3, 8, 2, 1]

Step 1: The array is initially unsorted: [5, 3, 8, 2, 1]

Step 2: The minimum element in the unsorted part is 1.

Step 3: Swap the minimum element (1) with the first element of the unsorted part. The
array becomes: [1, 3, 8, 2, 5]

Step 4: Move the boundary of the sorted part one element to the right. The sorted part
now contains the minimum element: [1]

Step 2: The minimum element in the unsorted part is 2.

Step 3: Swap the minimum element (2) with the first element of the unsorted part. The
array becomes: [1, 2, 8, 3, 5]
Step 4: Move the boundary of the sorted part one element to the right. The sorted part
now contains the minimum elements: [1, 2]

Step 2: The minimum element in the unsorted part is 3.

Step 3: Swap the minimum element (3) with the first element of the unsorted part. The
array becomes: [1, 2, 3, 8, 5]

Step 4: Move the boundary of the sorted part one element to the right. The sorted part
now contains the minimum elements: [1, 2, 3]

Step 2: The minimum element in the unsorted part is 5.

Step 3: Swap the minimum element (5) with the first element of the unsorted part. The
array becomes: [1, 2, 3, 5, 8]

Step 4: Move the boundary of the sorted part one element to the right. The sorted part
now contains all the elements: [1, 2, 3, 5, 8]

The array is now fully sorted: [1, 2, 3, 5, 8].

YT link: https://youtu.be/dQa4A2Z0_Ro

Insertion Sort

The insertion sort algorithm is a simple sorting algorithm that works by building a sorted
portion of the array one element at a time. Here are the step-by-step instructions for the
insertion sort algorithm:

1. Start with the second element (index 1) of the array.


2. Compare the second element with the first element (index 0). If the second
element is smaller, swap them.
3. Move to the third element (index 2) and compare it with the elements to its left.
Insert the third element into its correct position in the sorted portion of the array.
4. Repeat steps 3-4 for all the remaining elements in the array, moving from left to
right.
5. Once all the elements are sorted, the array is in ascending order.
Let's go through an example to illustrate the steps. Consider the following array: [5, 2, 4,
6, 1, 3]

Step 1: Start with the second element (2).


● Compare 2 with 5. Since 2 is smaller, swap them. The array becomes [2, 5, 4, 6,
1, 3].

Step 2: Move to the third element (4).

● Compare 4 with 5. Since 4 is smaller, swap them. The array becomes [2, 4, 5, 6,
1, 3].
● Compare 4 with 2. Since 4 is larger, stop comparing.

Step 3: Move to the fourth element (6).

● Compare 6 with 5. Since 6 is larger, stop comparing.

Step 4: Move to the fifth element (1).

● Compare 1 with 6. Since 1 is smaller, swap them. The array becomes [2, 4, 5, 1,
6, 3].
● Compare 1 with 5. Since 1 is smaller, swap them. The array becomes [2, 4, 1, 5,
6, 3].
● Compare 1 with 4. Since 1 is smaller, swap them. The array becomes [2, 1, 4, 5,
6, 3].
● Compare 1 with 2. Since 1 is smaller, swap them. The array becomes [1, 2, 4, 5,
6, 3].

Step 5: Move to the sixth element (3).

● Compare 3 with 6. Since 3 is smaller, swap them. The array becomes [1, 2, 4, 5,
3, 6].
● Compare 3 with 5. Since 3 is smaller, swap them. The array becomes [1, 2, 4, 3,
5, 6].
● Compare 3 with 4. Since 3 is smaller, swap them. The array becomes [1, 2, 3, 4,
5, 6].

Now, all the elements are sorted in ascending order: [1, 2, 3, 4, 5, 6].

YT link : https://youtu.be/3GC83dh4cf0

You might also like