Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Insertion Sort in C#
Insertion Sort is a simple sorting algorithm that builds the sorted array one element at a time. It takes each element from the unsorted portion and inserts it into its correct position within the already sorted portion of the array.
The algorithm works similarly to how you might sort playing cards in your hand − you pick up cards one by one and insert each into its proper position among the cards you've already sorted.
How Insertion Sort Works
The algorithm divides the array into two parts: a sorted portion (initially just the first element) and an unsorted portion. For each element in the unsorted portion, it finds the correct position in the sorted portion and inserts it there.
Syntax
Following is the basic structure of insertion sort algorithm −
for (int i = 1; i < array.Length; i++) {
int key = array[i];
int j = i - 1;
while (j >= 0 && array[j] > key) {
array[j + 1] = array[j];
j--;
}
array[j + 1] = key;
}
Example
using System;
class InsertionSortExample {
static void Main(string[] args) {
int[] arr = new int[10] { 23, 9, 85, 12, 99, 34, 60, 15, 100, 1 };
int n = arr.Length;
Console.WriteLine("Insertion Sort");
Console.Write("Initial array is: ");
for (int i = 0; i = 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
Console.Write("\nSorted Array is: ");
for (int i = 0; i
The output of the above code is −
Insertion Sort
Initial array is: 23 9 85 12 99 34 60 15 100 1
Sorted Array is: 1 9 12 15 23 34 60 85 99 100
Step-by-Step Process
The insertion sort algorithm works as follows −
-
Start from the second element (index 1) since the first element is considered sorted.
-
Store the current element in a temporary variable called key.
-
Compare the key with elements in the sorted portion (moving backwards).
-
Shift elements that are greater than the key one position to the right.
-
Insert the key at its correct position.
-
Repeat for all remaining elements.
Time and Space Complexity
| Case | Time Complexity | Description |
|---|---|---|
| Best Case | O(n) | When array is already sorted |
| Average Case | O(n²) | When elements are in random order |
| Worst Case | O(n²) | When array is sorted in reverse order |
| Space Complexity | O(1) | In-place sorting algorithm |
Using Generic Method for Different Data Types
Example
using System;
class GenericInsertionSort {
public static void InsertionSort(T[] array) where T : IComparable {
for (int i = 1; i = 0 && array[j].CompareTo(key) > 0) {
array[j + 1] = array[j];
j--;
}
array[j + 1] = key;
}
}
static void Main(string[] args) {
// Sort integers
int[] numbers = { 64, 34, 25, 12, 22, 11, 90 };
Console.WriteLine("Original integers: " + string.Join(", ", numbers));
InsertionSort(numbers);
Console.WriteLine("Sorted integers: " + string.Join(", ", numbers));
// Sort strings
string[] names = { "John", "Alice", "Bob", "Diana", "Charlie" };
Console.WriteLine("\nOriginal strings: " + string.Join(", ", names));
InsertionSort(names);
Console.WriteLine("Sorted strings: " + string.Join(", ", names));
}
}
The output of the above code is −
Original integers: 64, 34, 25, 12, 22, 11, 90 Sorted integers: 11, 12, 22, 25, 34, 64, 90 Original strings: John, Alice, Bob, Diana, Charlie Sorted strings: Alice, Bob, Charlie, Diana, John
Conclusion
Insertion Sort is an efficient algorithm for small datasets and nearly sorted arrays. While it has O(n²) time complexity in the worst case, its simplicity and adaptive nature make it useful for educational purposes and as a building block for more complex algorithms like quicksort's recursive calls.
