0
0
DSA Javascriptprogramming~10 mins

Insertion Sort Algorithm in DSA Javascript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start the outer loop from the second element in the array.

DSA Javascript
for(let i = [1]; i < arr.length; i++) {
  // sorting logic
}
Drag options to blanks, or click blank then click option'
A1
B2
Carr.length - 1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from index 0 causes unnecessary comparisons.
Starting from the last element misses sorting the array.
2fill in blank
medium

Complete the code to store the current element to be inserted in a variable.

DSA Javascript
let key = arr[[1]];
Drag options to blanks, or click blank then click option'
Aarr.length - 1
Bi - 1
C0
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using i - 1 gets the previous element, not the current one.
Using 0 or arr.length - 1 does not refer to the current element.
3fill in blank
hard

Fix the error in the while loop condition to correctly compare elements for insertion.

DSA Javascript
while(j >= 0 && arr[j] [1] key) {
  arr[j + 1] = arr[j];
  j--;
}
Drag options to blanks, or click blank then click option'
A<
B==
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' reverses the logic and breaks sorting.
Using '==' or '<=' does not correctly shift elements.
4fill in blank
hard

Fill both blanks to correctly shift elements right.

DSA Javascript
arr[j [1] 1] = arr[j];
j = j [2] 1;
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' for the array index causes overwriting.
Incrementing j instead of decrementing causes infinite loop.
5fill in blank
hard

Fill all three blanks to complete the insertion of the key after shifting elements.

DSA Javascript
arr[j [1] 1] = [2];

for(let i = [3]; i < arr.length; i++) {
  // insertion sort logic
}
Drag options to blanks, or click blank then click option'
A+
Bkey
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' causes wrong insertion position.
Using wrong variable instead of key overwrites data.
Starting loop at 0 causes unnecessary comparisons.