0% found this document useful (0 votes)
52 views

Insertion-Sort 9

Uploaded by

ravi893084
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Insertion-Sort 9

Uploaded by

ravi893084
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Data Structure and Algorithm

Topperworld.in

Insertion Sort

Insertion Sort: Insertion Sort is a simple sorting algorithm that builds the
final sorted array one item at a time. It is similar to the way you might sort
a hand of playing cards. The algorithm works by taking an element from
the unsorted part of the array and inserting it into its correct position within
the sorted part of the array.

Algorithm:

1. Start with the second element (index 1) as the first element is


considered sorted.

2. Compare the current element with the elements in the sorted portion
of the array, moving from right to left.

©Topperworld
Data Structure and Algorithm

3. If the current element is smaller (for ascending order) or larger (for


descending order) than the compared element, shift the compared
element one position to the right.

4. Repeat step 3 until you find the correct position for the current
element.

5. Insert the current element into the correct position in the sorted
portion of the array.

6. Move to the next unsorted element and repeat steps 2-5.

7. Continue these iterations until the entire array is sorted.

Example:-

9 6 5 0 8 2 7 1 3 4

©Topperworld
Data Structure and Algorithm

©Topperworld
Data Structure and Algorithm

Java Implementation:

Here's the Java code for implementing Insertion Sort:

public class InsertionSort {


public static void insertionSort(int[] arr) {
int n = arr.length;
for (int i = 1; i < n; i++) {
int currentElement = arr[i];
int j = i - 1;

// Move elements of arr[0..i-1] that are greater than


currentElement to one position ahead of their current position
while (j >= 0 && arr[j] > currentElement) {
arr[j + 1] = arr[j];
j--;
}

// Insert the currentElement at its correct position


arr[j + 1] = currentElement;
}
}

public static void main(String[] args) {


int[] arr = {9,6,5,0,8,2,7,1,3,4};
insertionSort(arr);

System.out.println("Sorted array:");
for (int num : arr) {
System.out.print(num + " ");
}
}
}

OUTPUT

Sorted Array:
0 1 2 3 4 5 6 7 8 9

©Topperworld
Data Structure and Algorithm

➔Complexity Analysis:

• Time Complexity: Insertion Sort has a time complexity of O(n^2)


in the worst case, where n is the number of elements in the array. It
performs more efficiently when the array is partially sorted or
contains fewer inversions. The number of comparisons and swaps are
relatively low when the array is nearly sorted, giving it an average-
case time complexity closer to O(n).

• Space Complexity: Insertion Sort has a space complexity of O(1),


as it only requires a constant amount of additional memory to
perform the swaps and keep track of indices.

Insertion Sort is efficient for small datasets and is also used as part of more
advanced sorting algorithms, such as Timsort (used in Python's built-in
sorted function). However, for larger arrays, other sorting algorithms like
Merge Sort or Quick Sort are generally more efficient.

©Topperworld

You might also like