Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Insertion Sort | Sorting and Searching
Advanced Java 2077
course content

Conteúdo do Curso

Advanced Java 2077

Advanced Java 2077

1. Data Structures
2. Sorting and Searching
3. Concurrent Programming
4. Input-Output (I-O) and Networking
5. Java GUI Development

Insertion Sort

Insertion sort is another simple sorting algorithm that is often used for teaching the basics of sorting algorithms. The algorithm sorts the list by iterating through each element and inserting it into the correct position in the sorted part of the list. In this chapter, we will discuss the algorithm in more detail, including its advantages and disadvantages, a step-by-step guide on how to implement the algorithm, and how to optimize it.

Advantages of Insertion Sort

One of the main advantages of insertion sort is that it is very simple to understand and implement. The algorithm is easy to explain, and the code to implement it is short and easy to write. Insertion sort also has a worst-case time complexity of O(n^2), making it an efficient choice for small lists.

Disadvantages of Insertion Sort

One of the main disadvantages of insertion sort is its time complexity. Like bubble sort, insertion sort has a worst-case time complexity of O(n^2), which means that it takes a long time to sort large lists. Insertion sort is also not very efficient when it comes to swapping elements, as it needs to shift elements in the sorted part of the list to make room for the new element being inserted.

Step-by-Step Guide to Implementing Insertion Sort

  • Take the input list and determine its length.
  • Iterate through the list, starting at the second element.
  • For each element, compare it to the elements in the sorted part of the list.
  • If the element is less than the current sorted element, shift the current element one position to the right.
  • Repeat step 4 until the correct position for the element is found.
  • Insert the element into the correct position in the sorted part of the list.
  • Repeat until the entire list is sorted.

Here is an example implementation of insertion sort in Java.

java

Main

copy
1234567891011121314
public static void insertionSort(int[] arr) { int n = arr.length; for (int i = 1; i < n; i++) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; } }

The implementation starts with an outer loop that iterates through the list, starting at the second element. The inner loop compares the current element to the sorted part of the list and shifts elements to make room for the new element to be inserted. Once the correct position is found, the element is inserted into the sorted part of the list.

Optimizing Insertion Sort

Insertion sort can be optimized in several ways to improve its efficiency. One optimization is to use binary search to find the correct position for the new element to be inserted. This reduces the number of comparisons needed to find the correct position. Another optimization is to use shell sort, which is a modified version of insertion sort that sorts the list in smaller increments.

1. What is the time complexity of insertion sort in the worst case?
2. What is the disadvantage of insertion sort?

What is the time complexity of insertion sort in the worst case?

Selecione a resposta correta

What is the disadvantage of insertion sort?

Selecione a resposta correta

Tudo estava claro?

Seção 2. Capítulo 2
We're sorry to hear that something went wrong. What happened?
some-alt