Ternary Search in Golang On Visual Studio Code
Last time (and the first&only time) I coded Go , I was using the editor on https://tour.golang.org , and It’s not the best place to code. This time I decided to do better than that and find an IDE for my practice on binary search and Ternary Search Algorithms. When I saw that JetBrains GoLand is not free , I set my eyes on Visual Studio Code plus the right plugins. Hasn’t been the smoothest of rides so far , but I won’t complain.
As far As I can tell , func main() is the main entry point for a Golang runnable , like Java psvm. So I created two functions called ternarySearch and binarySearch , and an utility function createArray(size int) , for creating a sorted array of random integers and deciding a random search key , smaller than the Max Value in the array, as Binary search and Ternary search both work exclusively on sorted Lists. I used math/rand but one should use crypto/rand package for better randoms.And I printed the loop step and the L and R (and Sliders for ternary search) values on that step , for comparison and debugging.
I didn’t install all the plugins or spend so much time trying, but it was not so hard to get features like highlighting , outlining and code completion . And the Code breaks on debug was working good enough for my simple task. At some points , I had to open the shell and run some go commands for some packages, though. I’m curious about a multi-thread debugging scenario.
I can truly say I’m warming up to the language, even If just for the brain flexing. It was interesting to see in practice that Ternary search almost always finds the match in fewer steps even though the time complexity is worse than binary search , due to the counts of comparison , hidden behind my logic of STEPS output. The whole code is below.