Golang program for implementation of Pancake Sort
Pancake sorting is the colloquial term for the mathematical problem of sorting a disordered stack of pancakes in order of size when a spatula can be inserted at any point in the stack and used to flip all pancakes above it. A pancake number is the maximum number of flips required for a given number of pancakes. Here is source code of the Go Program to implement Pancake Sorting Algorithm.
Example
package main
import "fmt"
func main() {
list := data{28, 11, 59, -26, 503, 158, 997, 193, -23, 44}
fmt.Println("\n--- Unsorted --- \n\n", list)
list.pancakesort()
fmt.Println("\n--- Sorted ---\n\n", list, "\n")
}
type data []int32
func (dataList data) pancakesort() {
for uns := len(dataList) - 1; uns > 0; uns-- {
// find largest in unsorted range
lx, lg := 0, dataList[0]
for i := 1; i <= uns; i++ {
if dataList[i] > lg {
lx, lg = i, dataList[i]
}
}
// move to final position in two flips
dataList.flip(lx)
dataList.flip(uns)
}
}
func (dataList data) flip(r int) {
for l := 0; l < r; l, r = l+1, r-1 {
dataList[l], dataList[r] = dataList[r], dataList[l]
}
}
Output
--- Unsorted ---
[28 11 59 -26 503 158 997 193 -23 44]
--- Sorted ---
[-26 -23 11 28 44 59 158 193 503 997]
Most Helpful This Week
GO Program to Find LCM and GCD of given two numbers
How do you handle HTTP server shutdown gracefully in Go?
Implementing Multiple Interfaces in Go Programming Language
Cannot convert <type1> to <type2> error in Golang
Golang program for implementation LIFO Stack and FIFO Queue
React JS Count number of checkboxes are checked
Launches 10 Goroutines and each goroutine adding 10 values to a Channel
Program in Go language to Calculate Standard Deviation using Math package
Golang read csv file into struct
Declaration of a struct type