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> |
<lang csharp>namespace Sort { |
||
using System; |
|||
static class InsertionSort<T> where T : IComparable { |
|||
namespace insertionsort |
|||
⚫ | |||
{ |
|||
Sort(entries, 0, entries.Length - 1); |
|||
⚫ | |||
} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
insertionSort(ref A); |
|||
⚫ | |||
⚫ | |||
public static void Sort(T[] entries, Int32 first, Int32 last) { |
|||
for (var i = first + 1; i <= last; i++) { |
|||
var entry = entries[i]; |
|||
var j = i; |
|||
while (j >= 0 && A[j] > value) |
|||
while (j > first && entries[j - 1].CompareTo(entry) > 0) |
|||
entries[j] = entries[--j]; |
|||
j--; |
|||
entries[j] = entry; |
|||
⚫ | |||
A[j + 1] = value; |
|||
} |
|||
} |
|||
} |
} |
||
} |
} |
||
</lang> |
}</lang> |
||
'''Example''': |
|||
<lang csharp> using Sort; |
|||
using System; |
|||
⚫ | |||
static void Main(String[] args) { |
|||
⚫ | |||
InsertionSort<Int32>.Sort(entries); |
|||
⚫ | |||
⚫ | |||
}</lang> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Translated from the Haskell example: |
Translated from the Haskell example: |