105k views
2 votes
How does insertion sort work?

1) Separates the array into arrays with a length of 1 and then merges them together in sorted order in a recursive method
2) The next element in the "unsorted" list is moved in comparison to other elements in the "sorted" list until all elements are sorted to each other.
3) The smallest element in the array is swapped with the element in the 0th index. Then, the element of the smallest part of the rest of the array is swapped with the next element and so on.

User Dooltaz
by
8.1k points

1 Answer

3 votes

Final answer:

Insertion sort is a sorting algorithm that works by repeatedly taking an element from the unsorted part of the array and inserting it into its correct position in the sorted part of the array. So, the correct answer is 2) The next element in the "unsorted" list is moved in comparison to other elements in the "sorted" list until all elements are sorted to each other.

Step-by-step explanation:

Insertion sort is a sorting algorithm that works by repeatedly taking an element from the unsorted part of the array and inserting it into its correct position in the sorted part of the array.

Here's how it works:

Start with the second element in the array and compare it to the elements in the sorted part of the array.

If the current element is smaller, shift the larger elements to the right until the correct position is found.

Repeat this process for each element in the unsorted part of the array.

This process continues until all the elements are sorted, and the array is in ascending order.

Thus, the correct answer is 2) The next element in the "unsorted" list is moved in comparison to other elements in the "sorted" list until all elements are sorted to each other.

User Vakio
by
7.7k points