Sorting Algorithms
Sorting Algorithms
Insertion Sort
Step 1 − If it is the first element, it is already sorted. return 1;
Step 2 − Pick next element
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 list is sorted
Algorithm: Insertion-Sort(A)
for j = 2 to A.length
key = A[j]
i=j–1
while i > 0 and A[i] > key
A[i + 1] = A[i]
i = i -1
A[i + 1] = key
Bubble Sort
Step 1 − Check if the first element in the input array is greater than the next
element in the array.
Step 2 − If it is greater, swap the two elements; otherwise move the pointer
forward in the array.
Step 3 − Repeat Step 2 until we reach the end of the array.
Step 4 − Check if the elements are sorted; if not, repeat the same process (Step
1 to Step 3) from the last element of the array to the first.
Step 5 − The final output achieved is the sorted array.
Selection Sort
Step 1- Set MIN to location 0.
Step 2- Search the minimum element in the list.
Step 3- Swap with value at location MIN.
Step 4- Increment MIN to point to next element.
Step 5- Repeat until the list is sorted.