Skip to content

Instantly share code, notes, and snippets.

@barunthapa
Last active August 18, 2020 06:18
Show Gist options
  • Save barunthapa/beebf943a227c4afa161d272510548fe to your computer and use it in GitHub Desktop.
Save barunthapa/beebf943a227c4afa161d272510548fe to your computer and use it in GitHub Desktop.
Sort numbers 5 at a time from list of 25 to find out top 3

It is a sorting algorithm generation. We have a list containing 25 numbers, that we need to sort and figure out the greatest three numbers. However, the catch is that you can only sort 5 numbers in a single operation. So, basically imagine a function that will sort your numbers but can only take in 5 at a time. Using this function we need to be able to sort the 25 numbers to get top 3. Also, make a note of how many times the sorting function is called.

package main
import (
"fmt"
"math/rand"
"sort"
"time"
)
// It is a sorting algorithm generation.
// We have a list containing 25 numbers, that we need to sort and figure out the greatest three numbers.
// However, the catch is that you can only sort 5 numbers in a single operation.
// So, basically imagine a function that will sort your numbers but can only take in 5 at a time.
// Using this function we need to be able to sort the 25 numbers to get top 3.
// Also, make a note of how many times the sorting function is called.
// Generates a list of 25 random numbers
func initialList() []int {
list := make([]int, 0)
s1 := rand.NewSource(time.Now().UnixNano())
r1 := rand.New(s1)
for n := 0; n < 25; n++ {
list = append(list, r1.Intn(100))
}
return list
// a test case
//return []int{94,26,54,10,1,30,65,11,53,53,53,4,45,0,56,81,33,66,75,46,0,51,51,39,39}
}
// Sorting function that is designed to sort 5 values at a time
// Also has a counter
func sorter(list []int, count *int) []int {
// Need to implement the limit of 5 at a time.
//if len(list) > 5 {
// return []int{}
//}
*count++
sort.Ints(list)
return list
}
func main() {
list := initialList()
fmt.Println("Initial list:")
fmt.Println(list)
// A counter to check how may time the sorting function is called
sortCounter := 0
// Build groups of 5 item each
matrix := make([][]int, 5)
// First stage of sorting
// Select 5 from the list as a group, sort them and add it to matrix
for i:=0; i<5; i++ {
fmt.Println(list[i*5:(i*5 + 5)])
matrix[i] = sorter(list[i*5:(i*5 + 5)], &sortCounter)
}
// Now we have 5 sorted groups in matrix
fmt.Println("Groups : ", matrix)
// Second stage of sorting
// Sort the top from each group to find out the least from the list
// Extract the least numbers from each group
toppers := make([]int, 5)
for i:=0; i<5; i++ {
toppers[i] = matrix[i][0]
}
fmt.Println("Toppers : ", toppers)
// Make a copy of toppers to later identify their group via index
toppersCopy := make([]int, 5)
copy(toppersCopy, toppers)
// Sort the least num from each group
// Using new variable just for semantics
sortedToppers := sorter(toppers, &sortCounter)
fmt.Println("Sorted Toppers : ", sortedToppers)
// This gives the least value of the main list
// toppers[0]
// Now need to figure out which sub group the topper[0] belongs to
// also each of them
mappedIndex := make([]int, 5)
for i, sVal := range sortedToppers {
for j, val := range toppersCopy {
if sVal == val {
mappedIndex[i] = j // this will hold the original index of the groups in matrix
toppersCopy[j] = -1 // to make sure duplicates are handled
break
}
}
}
fmt.Println("mappedIndex :", mappedIndex)
// Third stage of sorting
// These are the possible list of numbers that can be 2nd and 3rd least
runnerUps := []int{
matrix[mappedIndex[0]][1], // second from winner's group
matrix[mappedIndex[0]][2], // third from winner's group
matrix[mappedIndex[1]][0], // first from first runner up's group
matrix[mappedIndex[1]][1], // second from first runner up's group
matrix[mappedIndex[2]][0], // first from second runner up's group
}
fmt.Println("runner ups :", runnerUps)
runnerUps = sorter(runnerUps, &sortCounter)
fmt.Println("The least three are :")
fmt.Println(toppers[0], runnerUps[0], runnerUps[1])
fmt.Println("The sort count:", sortCounter)
// Verifying our algorithm works
sort.Ints(list)
fmt.Println(list[0:3])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment