0
1.9kviews
Sort the following array using each of the three sorting algorithms 26,48,12,92,28,6,33 1)Bubble Sort 2)Insertion Sort 3)Selection Sort
1 Answer
3
67views

1] Bubble Sort

Algorithm for Bubble Sort:

Step 1 − Starting from the first position of the array, compare the first and the second elements.

Step 2 − If the first element is greater than the second element, they are swapped otherwise no need to swap.

Step 3 − Then compare the second and the third elements. Swap them if they are not in order.

Step 4 − Iteratively repeat the above process until all elements in the array are sorted.

Let's sort the given array using Bubble Sort:

Bubble Iteration 1

Now reached the end of the array and Iteration - 1 is completed.

Since all the elements in the array are not sorted completely in a single iteration.

Hence, Repeat the same process.

Bubble Iteration 2

Now reached the end of the array and Iteration - 2 is completed.

Since all the elements in the array are not sorted completely in a second iteration.

Hence, Repeat the same process until all elements in the array are sorted.

When there is no swap needed, bubble sorts know that an array is completely sorted and stops iterations.

Bubble Iteration 3,4,5

After 5 iterations Bubble Sort gives the Sorted Array.


2] Insertion Sort

Algorithm for Insertion Sort:

Step 1 − Compare the first two elements of the array, If it is already sorted. No need to swap and add the first element into the Sorted sub-list. Otherwise, swap the elements then add the first element into the Sorted sub-list.

Step 2 − Then compares the next two elements of the array.

Step 3 − Compare with all elements in the sorted sub-list.

Step 4 − Shift all the elements in the sorted sub-list that is greater than the value to be sorted.

Step 5 − Insert the value

Step 6 − Repeat until the array is sorted completely.

Let's sort the given array using Insertion Sort:

Insertion Sort


3] Selection Sort

Algorithm for Selection Sort:

Step 1 − Set MIN at 0th position of the array.

Step 2 − Search the minimum valued element in the remaining array excluding MIN and already swapped sub-list of the array.

Step 3 − Swap this minimum value with the value present at a MIN position.

Step 4 − Increment MIN to point out the next element in the array.

Step 5 − Repeat until the complete array is sorted

Let's sort the given array using Selection Sort:

Selection Sort

Please log in to add an answer.