C#

Insertion Sort in C#

Programmingempire

This article explains Insertion Sort in C#. Another simple sorting algorithm is the Insertion Sort. Basically, this algorithm works by first virtually dividing the list in two parts. While, the first part belongs to the sorted list, the other part is unsorted. Further, the agorithm picks an element from the unsorted part and inserts it in the correct place in the sorted part of the list.

An Example of Performing Sorting Using Insertion Sort

In order to demonstrate insertion sort, let us assume that we need to sort this list [9, 3, 2, 1, 5, 8]. Further, the sorted part contains only the first element. Whereas the unsorted part contains rest of the elements. So, the element in green color belongs to the sorted part of the list. While, those in red color belong to the unsorted part.

[9, 3, 2, 1, 5, 8]. At first, the algorithm compares element 3 with 9 and places it at first position and shift tle element 9. So the sorted list has two elements [3, 9].

[3, 9, 2, 1, 5, 8]. Further, it places the element 2 at its correct place.

[2, 3, 9, 1, 5, 8]. After that, it compares element 1 with each element in the sorted part and places the element 1 at first postion.

[1, 2, 3, 9, 5, 8]. Similarly, it takes 5 as the next element and compares it with 9. Again, it compares 5 with 3. Since, it is larger than 3, so it places 5 after 3.

[1, 2, 3, 5, 9, 8]. Likewise, the element 8 is placed before 9. Finally, the whole list is sorted.

[1, 2, 3, 5, 8, 9]

Implementation of Insertion Sort in C#

The following code shows Implementation of Insertion Sort in C#. Here, the variable i in the InsertionSort() function represents the unsorted part. While, j represents an element in the Sorted part.

using System;

namespace InsertionSort
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] myarray = { 4, 1, 9, -13, 90, 56, 81, 34, -2, -15, 60, 88 };
            Console.WriteLine("Array before sorting...");
            foreach (int x in myarray)
                Console.Write(x + "  ");
            Sort.InsertionSort(myarray);
            Console.WriteLine("\nArray after sorting...");
            foreach (int x in myarray)
                Console.Write(x + "  ");
            Console.WriteLine();
        }
    }
    class Sort
    {
        public static void InsertionSort(int[] a1)
        {
            int j, x;
            for(int i=1;i<a1.Length;i++)
            {
                x = a1[i];
                j = i - 1;
                while((j>=0) && (a1[j]>x))
                {
                    a1[j + 1] = a1[j];
                    j--;
                }
                a1[j + 1] = x;
            }
        }
    }
}

Output

Sorting an Array using Insertion Sort in C#
Sorting an Array using Insertion Sort in C#

Further Reading

Selection Sort in C#

Insertion Sort in C#

Bubble Sort in C#

How to Create Instance Variables and Class Variables in Python

Comparing Rows of Two Tables with ADO.NET

Example of Label and Textbox Control in ASP.NET

One Dimensional and Two Dimensuonal Indexers in C#

Private and Static Constructors in C#

Methods of Array Class

Anonymous Functions in C#

Programs to Find Armstrong Numbers in C#

Matrix Multiplication in C#

One Dimensional and Two Dimensional Indexers in C#

Static Class Example in C#

Rotating an Array in C#

Generic IList Interface and its Implementation in C#

Recursive Binary search in C#

C# Practice Questions

Creating Navigation Window Application Using WPF in C#

Find Intersection Using Arrays

An array of Objects and Object Initializer

Performing Set Operations in LINQ

Using Quantifiers in LINQ

Data Binding Using BulletedList Control

Grouping Queries in LINQ

Generic Binary Search in C#

Understanding the Quantifiers in LINQ

Join Operation using LINQ

Deferred Query Execution and Immediate Query Execution in LINQ

Examples of Query Operations using LINQ in C#

An array of Objects and Object Initializer

Language-Integrated Query (LINQ) in C#

How Data Binding Works in WPF

Examples of Connected and Disconnected Approach in ADO.NET

New Features in C# 9

IEnumerable and IEnumerator Interfaces

KeyValuePair and its Applications

C# Root Class – Object

Access Modifiers in C#

Learning Properties in C#

Learning All Class Members in C#

Examples of Extension Methods in C#

How to Setup a Connection with SQL Server Database in Visual Studio

Understanding the Concept of Nested Classes in C#

LINQ To SQL Examples

A Beginner’s Tutorial on WPF in C#

Explaining C# Records with Examples

Everything about Tuples in C# and When to Use?

Creating Jagged Arrays in C#

Linear Search and Binary search in C#

Learning Indexers in C#

Object Initializers in C#

Examples of Static Constructors in C#

When should We Use Private Constructors?

C# Basic Examples

IEqualityComparer Interface

programmingempire

You may also like...