logo

Q1. Algorithm for Insertion Sort Ans. 1. Start from all

   

Added on  2023-01-17

2 Pages394 Words25 Views
 | 
 | 
 | 
Q1. Algorithm for Insertion Sort
Ans.
1. Start from all elements from first to last.
2. Pick the element.
3. If an element is already sorted, move to the next element.
4. Compare with all the elements, in the already sub-sorted list.
5. Shift all the elements from the element which is greater than the current element, one step ahead
and insert this element at that position.
6. Repeat all the steps until all elements are checked.
Best Case: n
Average Case: n2
Worst Case:n2
Q2. Algorithm for Selection Sort
Ans
1. Start from index 0.
2. Take a temporary element min and set its value to the element at the current index.
3. Search the minimum element from this index and swap its value with min.
4.Increment min to next element
5. Repeat all the steps until this list is sorted.
Best Case: n2
Average Case: n2
Worst Case:n2
Q3. Algorithm for Bubble Sort
Ans
1. Set start index as 0
2. From all the elements of the list starting from the current index.
3. If element[index]>element[index-1], swap the elements
4. Continue to the next element
5. Increment the index at repeat till the step is performed for all the indexes
6. Finally, the list is sorted.
Best Case: n
Average Case: n2
Worst Case:n2
Q1. Algorithm for Insertion Sort Ans. 1. Start from all_1

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents