
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Insertion Sort in C#
Insertion Sort is a sorting algorithm that takes an element at a time and inserts it in its correct position in the array. This process is continued until the array is sorted.
A program that demonstrates insertion sort in C# is given as follows.
Example
using System; namespace InsertionSortDemo { class Example { static void Main(string[] args) { int[] arr = new int[10] { 23, 9, 85, 12, 99, 34, 60, 15, 100, 1 }; int n = 10, i, j, val, flag; Console.WriteLine("Insertion Sort"); Console.Write("Initial array is: "); for (i = 0; i < n; i++) { Console.Write(arr[i] + " "); } for (i = 1; i < n; i++) { val = arr[i]; flag = 0; for (j = i - 1; j >= 0 && flag != 1; ) { if (val < arr[j]) { arr[j + 1] = arr[j]; j--; arr[j + 1] = val; } else flag = 1; } } Console.Write("
Sorted Array is: "); for (i = 0; i < n; i++) { Console.Write(arr[i] + " "); } } } }
Output
The output of the above program is as follows.
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
Now, let us understand the above program.
First the array is initialized and its value is printed using a for loop. This can be seen in the following code snippet −
int[] arr = new int[10] { 23, 9, 85, 12, 99, 34, 60, 15, 100, 1 }; int n = 10, i, j, val, flag; Console.WriteLine("Insertion Sort"); Console.Write("Initial array is: "); for (i = 0; i < n; i++) { Console.Write(arr[i] + " "); }
A nested for loop is used for the actual sorting process. In each pass of the outer for loop, the current element is inserted into its correct position in the array. This process continues until the array is sorted. This can be seen in the following code snippet.
for (i = 1; i < n; i++) { val = arr[i]; flag = 0; for (j = i - 1; j >= 0 && flag != 1; ) { if (val < arr[j]) { arr[j + 1] = arr[j]; j--; arr[j + 1] = val; } else flag = 1; } }
Finally, the sorted array is displayed. This can be seen in the following code snippet.
Console.Write("
Sorted Array is: "); for (i = 0; i < n; i++) { Console.Write(arr[i] + " "); }
- Related Articles
- Binary Insertion Sort in C++
- Insertion Sort List in C++
- Insertion Sort List C++
- Insertion sort using C++ STL
- C++ Program Recursive Insertion Sort
- Insertion Sort
- C++ Program to Implement Insertion Sort
- C Program for Recursive Insertion Sort
- Insertion sort in Java.
- An Insertion Sort time complexity question in C++
- Insertion Sort in Python Program
- Explain the insertion sort by using C language.
- Difference Between Insertion Sort and Selection Sort
- What is Insertion sort in Python?
- Python Program for Insertion Sort
