Sort disjoint sublist: Difference between revisions
Content added Content deleted
(Added solution for Action!) |
(Added Go) |
||
Line 1,286: | Line 1,286: | ||
fmt.Println("sorted: ", s.values) |
fmt.Println("sorted: ", s.values) |
||
}</lang> |
}</lang> |
||
=={{header|Go}}== |
|||
{{trans|Wren}} |
|||
<lang go>package main |
|||
import ( |
|||
"fmt" |
|||
"sort" |
|||
) |
|||
// sorts values in place, leaves indices unsorted |
|||
func sortDisjoint(values, indices []int) { |
|||
var sublist []int |
|||
for _, ix := range indices { |
|||
sublist = append(sublist, values[ix]) |
|||
} |
|||
sort.Ints(sublist) |
|||
indices2 := make([]int, len(indices)) |
|||
copy(indices2, indices) |
|||
sort.Ints(indices2) |
|||
for i, ix := range indices2 { |
|||
values[ix] = sublist[i] |
|||
} |
|||
} |
|||
func main() { |
|||
values := []int{7, 6, 5, 4, 3, 2, 1, 0} |
|||
indices := []int{6, 1, 7} |
|||
fmt.Println("Initial: ", values) |
|||
sortDisjoint(values, indices) |
|||
fmt.Println("Sorted : ", values) |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Initial: [7 6 5 4 3 2 1 0] |
|||
Sorted : [7 0 5 4 3 2 1 6] |
|||
</pre> |
|||
=={{header|Groovy}}== |
=={{header|Groovy}}== |