Sorting algorithms/Insertion sort: Difference between revisions
Content deleted Content added
Go solution |
|||
Line 533: | Line 533: | ||
s; |
s; |
||
# "ABCDEFGHIJKLMNOPQRSTUVWXYZ"</lang> |
# "ABCDEFGHIJKLMNOPQRSTUVWXYZ"</lang> |
||
=={{header|Go}}== |
|||
<lang go>package main |
|||
import "fmt" |
|||
func main() { |
|||
var list insert = []int{31, 41, 59, 26, 53, 58, 97, 93, 23, 84} |
|||
fmt.Println("unsorted:", list) |
|||
list.sort() |
|||
fmt.Println("sorted! ", list) |
|||
} |
|||
type insert []int |
|||
func (a insert) sort() { |
|||
for i := 1; i < len(a); i++ { |
|||
value := a[i] |
|||
j := i - 1 |
|||
for j >= 0 && a[j] > value { |
|||
a[j+1] = a[j] |
|||
j = j - 1 |
|||
} |
|||
a[j+1] = value |
|||
} |
|||
}</lang> |
|||
Output: |
|||
<pre> |
|||
unsorted: [31 41 59 26 53 58 97 93 23 84] |
|||
sorted! [23 26 31 41 53 58 59 84 93 97] |
|||
</pre> |
|||
=={{header|Haskell}}== |
=={{header|Haskell}}== |