Сортировка вставкой - C#
Формулировка задачи:
using System;
namespace insertion_sort
{
class Program
{
static void Main(string[] args)
{
int[] A = new int[] { 5, 2, 4, 6, 1, 3 };
for (int j = 2; j <= A.Length - 1; j++)
{
int key = A[j];
int i = j - 1;
while (i > 0 && A[i] > key)
{
A[i + 1] = A[i];
i = j - 1;
A[i + 1] = key;
}
}
for (int i = 0; i < A.Length-1; i++) Console.Write(A[i] + " ");
}
}
}Решение задачи: «Сортировка вставкой»
textual
Листинг программы
private void InsertionSort(long[] inputArray)
{ // сортировка вставками
long j = 0, temp = 0;
for (int i = 1; i < inputArray.Length; i++)
{
j = i;
temp = inputArray[i];
while ((j > 0) && (inputArray[j - 1] > temp))
{
inputArray[j] = inputArray[j - 1];
j--;
}
inputArray[j] = temp;
}
}