Sorts an array of data using the insertion sort algorithm : Sort : Collections Data Structure C# Source Code


Custom Search

C# Source Code » Collections Data Structure » Sort »

 

Sorts an array of data using the insertion sort algorithm









    


using System;

public class InsertionSort {
    
  public static void InsertNext(int i, int[] item) {
    int current = item[i];
    int j = 0;
    while (current > item[j]) j++;
    for (int k = i; k > j; k--)
      item[k] = item[k-1];
    item[j] = current;
  }

  public static void Sort(int[] item) {
    for (int i = 1; i < item.Length; i++) {
      InsertNext(i, item); 
    }
  }

  public static void Main()  {
    int[] item = new int[]{2,4,1,6,3,8,1,0,2,6,3,6};
    Sort(item);
    for(int i=0; i<item.Length;i++){
        Console.WriteLine(item[i]);
    }
  }
}

           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Collections Data Structure
» Sort