523 views
0 votes
0 votes
Consider this array [ 2,2,2,1,1,1,1,1,0,0,0 ].

Find the min. no. of swaps needed for this array to be sorted in asc. order using:

a) bubble sort

b) insertion sort

1 Answer

0 votes
0 votes

Number of swaps in both case will be always same, as the idea is the same in both of the case, the difference lies in how it is implemented.

A detailed explanation can be found here:

https://stackoverflow.com/questions/17270628/insertion-sort-vs-bubble-sort-algorithms

Execution to count the swaps:

Insertion Sort:

https://ide.geeksforgeeks.org/lSl4Y8miIS

Bubble Sort run:

https://ide.geeksforgeeks.org/SbwqkTU2d1

Related questions

0 votes
0 votes
2 answers
1
dhruba asked Jun 5, 2023
1,094 views
Binary search is performed on a sorted array of n elements. The search key is not in the array and falls between the elements at positions m and m+1 (where 1 ≤ m < n). ...
8 votes
8 votes
2 answers
2
0 votes
0 votes
1 answer
3
LavTheRawkstar asked Jan 12, 2017
808 views
INSERTION-SORT (A, n) ⊳ A[1 . . n]for (j ← 2 to len(A) ){key ← A[ j];i ← j – 1 ; while (i 0 and A[i] key) { A[...