Jump to content

Sorting algorithms/Insertion sort: Difference between revisions

(+ second D entry)
Line 282:
 
=={{header|C sharp|C#}}==
<lang csharp>usingnamespace System;Sort {
using System;
 
static class InsertionSort<T> where T : IComparable {
namespace insertionsort
public static void MainSort(stringT[] argsentries) {
{
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 Sort(ref intT[] Aentries, Int32 first, Int32 last) {
for (intvar i = 0first + 1; i <= A.Lengthlast; i++) {
var entry = {entries[i];
int value = A[i],var j = i-1;
 
while (j >= 0 && A[j] > value)
while (j > first && entries[j - 1].CompareTo(entry) {> 0)
Aentries[j + 1] = Aentries[--j];
 
j--;
entries[j] = }entry;
{}
A[j + 1] = value;
}
}
}
}
}</lang>
'''Example''':
<lang csharp> using Sort;
using System;
 
class Program {
static void Main(String[] args) {
var int[] Aentries = new intInt32[] { 3, 9, 4, 6, 8, 1, 7, 2, 5 };
InsertionSort<Int32>.Sort(entries);
Console.WriteLine(stringString.Join(", ", Aentries));
}
}</lang>
 
=={{header|Clojure}}==
Translated from the Haskell example:
159

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.