Sorting algorithms/Insertion sort: Difference between revisions
Content added Content deleted
(add Tcl) |
(add E example) |
||
Line 164: | Line 164: | ||
} |
} |
||
</lang> |
</lang> |
||
=={{header|E}}== |
|||
A direct conversion of the pseudocode. |
|||
<lang e>def insertionSort(array) { |
|||
for i in 1..!(array.size()) { |
|||
def value := array[i] |
|||
var j := i-1 |
|||
while (j >= 0 && array[j] > value) { |
|||
array[j + 1] := array[j] |
|||
j -= 1 |
|||
} |
|||
array[j+1] := value |
|||
} |
|||
}</lang> |
|||
Test case: |
|||
<lang e>? def a := [71, 53, 22, 24, 83, 54, 39, 78, 65, 26, 60, 75, 67, 27, 52, 59, 93, 62, 85, 99, 88, 10, 91, 85, 13, 17, 14, 96, 55, 10, 61, 94, 27, 50, 75, 40, 47, 63, 10, 23].diverge() |
|||
> insertionSort(a) |
|||
> a |
|||
# value: [10, 10, 10, 13, 14, 17, 22, 23, 24, 26, 27, 27, 39, 40, 47, 50, 52, 53, 54, 55, 59, 60, 61, 62, 63, 65, 67, 71, 75, 75, 78, 83, 85, 85, 88, 91, 93, 94, 96, 99].diverge()</lang> |
|||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
<pre> |
<pre> |