Sorting algorithms/Insertion sort: Difference between revisions

Content added Content deleted
(+ second D entry)
Line 282: Line 282:


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<lang csharp>using System;
<lang csharp>namespace Sort {
using System;


static class InsertionSort<T> where T : IComparable {
namespace insertionsort
public static void Sort(T[] entries) {
{
Sort(entries, 0, entries.Length - 1);
class Program
{
}
static void Main(string[] args)
{
int[] A = new int[] { 3, 9, 4, 6, 8, 1, 7, 2, 5 };
insertionSort(ref A);
Console.WriteLine(string.Join(",", A));
}


public static void insertionSort (ref int[] A){
public static void Sort(T[] entries, Int32 first, Int32 last) {
for (int i = 0; i < A.Length; i++)
for (var i = first + 1; i <= last; i++) {
{
var entry = entries[i];
int value = A[i], j = i-1;
var j = i;

while (j >= 0 && A[j] > value)
{
while (j > first && entries[j - 1].CompareTo(entry) > 0)
A[j + 1] = A[j];
entries[j] = entries[--j];

j--;
}
entries[j] = entry;
}
A[j + 1] = value;
}
}
}
}
}
}
</lang>
}</lang>
'''Example''':
<lang csharp> using Sort;
using System;

class Program {
static void Main(String[] args) {
var entries = new Int32[] { 3, 9, 4, 6, 8, 1, 7, 2, 5 };
InsertionSort<Int32>.Sort(entries);
Console.WriteLine(String.Join(" ", entries));
}
}</lang>

=={{header|Clojure}}==
=={{header|Clojure}}==
Translated from the Haskell example:
Translated from the Haskell example: