Last active
May 22, 2020 13:12
-
-
Save sillyfellow/3d738d8efc172ba3e89faf322f827db3 to your computer and use it in GitHub Desktop.
Sometimes it is not enough to know whether an element is in the slice. One may want to insert a new element into the slice. But without knowing what is in the slice already. So, a slightly tweaked search will find the position where it should go.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sort" | |
) | |
/* | |
Sometimes it is not enough to know whether an element is in the slice. One | |
may want to insert a new element into the slice. But without knowing what | |
is in the slice already. So, a slightly tweaked search will find the | |
position where it should go. | |
Obviously, checking whether the element is smaller than the first, and is | |
larger than the last will help to find the `edge` scenarios. But for a | |
generic case see the code below | |
-- (sillyfellow@05/22/2020) */ | |
// findIndex to insert helps you to insert a new element in the slice without | |
// breaking the sorted order. For the documented usage of sort.Search, see: | |
// https://play.golang.org/p/dl4mu0HWJty | |
func sortedInsert(sortedSlice []int, x int) { | |
i := sort.Search(len(sortedSlice), func(i int) bool { return sortedSlice[i] > x }) | |
if i == 0 { | |
fmt.Printf("%d should be in the beginning\n", x) | |
fmt.Println("sortedSlice = append([]int{x}, sortedSlice...)\n") | |
} else if i < len(sortedSlice) { | |
if sortedSlice[i-1] != x { | |
fmt.Printf("%d should go at %d (before %d in %v)\n", x, i, sortedSlice[i], sortedSlice) | |
fmt.Println("sortedSlice = append(sortedSlice[:i], append([]int{x}, sortedSlice[i:]...)...)\n") | |
} else { | |
fmt.Printf("%d is already at %d (before %d in %v)\n", x, i-1, sortedSlice[i], sortedSlice) | |
fmt.Println("nothing to do\n") | |
} | |
} else { | |
fmt.Printf("%d should go at the end (index: %d), after %d\n", x, i, sortedSlice[i-1]) | |
fmt.Println("sortedSlice = append(sortedSlice, x)\n") | |
} | |
} | |
func main() { | |
sortedSlice := []int{1, 3, 6, 10, 15, 21, 28, 36, 45, 55} | |
sortedInsert(sortedSlice, 0) | |
//0 should be in the beginning | |
//sortedSlice = append([]int{x}, sortedSlice...) | |
sortedInsert(sortedSlice, 1) | |
//1 is already at 0 (before 3 in [1 3 6 10 15 21 28 36 45 55]) | |
//nothing to do | |
sortedInsert(sortedSlice, 2) | |
//2 should go at 1 (before 3 in [1 3 6 10 15 21 28 36 45 55]) | |
//sortedSlice = append(sortedSlice[:i], append([]int{x}, sortedSlice[i:]...)...) | |
sortedInsert(sortedSlice, 3) | |
//3 is already at 1 (before 6 in [1 3 6 10 15 21 28 36 45 55]) | |
//nothing to do | |
sortedInsert(sortedSlice, 5) | |
//5 should go at 2 (before 6 in [1 3 6 10 15 21 28 36 45 55]) | |
//sortedSlice = append(sortedSlice[:i], append([]int{x}, sortedSlice[i:]...)...) | |
sortedInsert(sortedSlice, 54) | |
//54 should go at 9 (before 55 in [1 3 6 10 15 21 28 36 45 55]) | |
//sortedSlice = append(sortedSlice[:i], append([]int{x}, sortedSlice[i:]...)...) | |
sortedInsert(sortedSlice, 55) | |
//55 should go at the end (index: 10), after 55 | |
//sortedSlice = append(sortedSlice, x) | |
sortedInsert(sortedSlice, 57) | |
//57 should go at the end (index: 10), after 55 | |
//sortedSlice = append(sortedSlice, x) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment