Insertion Sort

 

 

using System;

namespace ZeroToCSharp {
    class Program {
        static void Main(string[] args) {

            int[] array = new int[6] { 24, 6, 26, 11, 15, 3 };
            int key, comparer;
            for (int i = 1; i < array.Length; i++) {
                key = array[i];
                comparer = i - 1;
                while(comparer >= 0 && array[comparer] > key) {
                    array[comparer + 1] = array[comparer];
                    comparer--;
                }
                array[comparer + 1] = key;
            }

            //To print output
            Console.Write("Sorted Array: ");
            for (int i = 0; i < array.Length; i++) {
                Console.Write(array[i] + " ");
            }
            Console.ReadKey();
        }
    }
}

 

Output:

Sorted Array: 3 6 11 15 24 26

 

 

 

 

 

 

 

 

 

 

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x