Skip to content

Instantly share code, notes, and snippets.

@bimbimprasetyoafif
Created March 4, 2022 14:14
Show Gist options
  • Save bimbimprasetyoafif/29866a0a7f1acf6c34e584442d9d92e3 to your computer and use it in GitHub Desktop.
Save bimbimprasetyoafif/29866a0a7f1acf6c34e584442d9d92e3 to your computer and use it in GitHub Desktop.
Coret2 an code sort, stack, queue
package main
import "fmt"
type Queue struct {
element []int
}
func (q *Queue) enqueue(i int) {
q.element = append(q.element, i)
}
func (s *Queue) dequeue() int {
value := s.element[0]
s.element[0] = 0
s.element = s.element[1:]
return value
}
type Stack struct {
element []int
}
func (s *Stack) push(i int) {
s.element = append(s.element, i)
}
func (s *Stack) pop() int {
value := s.element[len(s.element) - 1]
s.element[len(s.element) - 1] = 0
s.element = s.element[:len(s.element) - 1]
return value
}
func main() {
// counting sort
// countingArray([]int{99, 2, 1, 19, 7, 20, 5, 2})
// stack
// var s []int
// fmt.Println(s)
// s = push(s, 2)
// fmt.Println(s)
// top(s)
// s = push(s, 4)
// fmt.Println(s)
// top(s)
// s = push(s, 1)
// fmt.Println(s)
// top(s)
// fmt.Println(s)
// s, n := pop(s)
// fmt.Println(s, n)
// top(s)
// s, n = pop(s)
// fmt.Println(s, n)
// top(s)
// stack object
// s := Stack{
// element: []int{},
// }
// fmt.Println(s.element)
// s.push(12)
// fmt.Println(s.element)
// s.push(1)
// fmt.Println(s.element)
// s.push(40)
// fmt.Println(s.element)
// n := s.pop()
// fmt.Println(s.element, n)
// queue object
s := Queue{
element: []int{},
}
fmt.Println(s.element)
s.enqueue(12)
fmt.Println(s.element)
s.enqueue(1)
fmt.Println(s.element)
s.enqueue(40)
fmt.Println(s.element)
n := s.dequeue()
fmt.Println(s.element, n)
// queue
// var s []int
// s = enqueue(s, 2)
// fmt.Println(s)
// s = enqueue(s, 5)
// fmt.Println(s)
// s = enqueue(s, 10)
// fmt.Println(s)
// s, n := dequeue(s)
// fmt.Print(s)
// fmt.Println(n)
// s, n = dequeue(s)
// fmt.Print(s)
// fmt.Println(n)
}
func enqueue(s []int, n int) []int {
s = append(s, n)
return s
}
func dequeue(s []int) ([]int, int) {
value := s[0]
s[0] = 0
s = s[1:]
return s, value
}
func push(s []int, n int) []int {
s = append(s, n)
return s
}
func pop(s []int) ([]int, int) {
value := s[len(s) - 1]
s[len(s) - 1] = 0
s = s[:len(s) - 1]
return s, value
}
func top(s []int) {
fmt.Println(s[len(s) - 1])
}
/*
queue
1, 2, 3 , 4 , 5
duluan akhir
*/
func countingArray(input []int) {
// step 1 cari max
max := 0
for i := 0; i < len(input); i++ {
if input[i] > max {
max = input[i]
}
}
fmt.Println("max = ", max)
// step 2 bikin temp container
temp := make(map[int]int, max)
// step 3 masukan jumlah value ke dalam temp
for i := 0; i < len(input); i++ {
nilai := input[i]
temp[ nilai ]++
}
fmt.Println("temp isinya = ", temp)
// step 4 jumlah dengan index sebelumnya
for i := 1; i <= len(temp); i++ {
temp[i] += temp[i-1]
}
fmt.Println("temp isinya setelah di jumlah = ", temp)
// step 5 isi array yg sorted
result := make([]int, len(input))
for i := 0; i < len(input); i++ {
nilaiInputan := input[i]
// [2, 1, 7, 5, 2]
// 0 1 2 3 4
// 0 == 2
value := temp[nilaiInputan]
// [1:1, 2:3, 3:3, 4:3, 5:4, 6:4, 7:5]
// 2 -> 3
result[value - 1] = nilaiInputan
// assign nilai inputan (2) ke index 3 (- 1) (array dari 0) result
// [ , ,2 , , ]
// 0 1 2 3 4
temp[nilaiInputan]--
// [1:1, 2:(2), 3:3, 4:3, 5:4, 6:4, 7:5]
// berubah decrement index 2
}
fmt.Println("hasil sorted = ", result)
}
/*
(input)
[2, 1, 7, 5, 2] max = 7, length = 5
1 2 3 4 5
for i, v := range input {
temp[v]++
}
(temp) length = max
[1 ,2 ,0 ,0 ,1 ,0 ,1 ]
1 2 3 4 5 6 7
penambahan
for i := 1; i < len temp; i++ {
temp[i] = temp[i+1]
}
[0 ,1 ,3 ,3 ,3 ,4 ,4 ]
1 2 3 4 5 6 7
(input)
[2, 1, 7, 5, 2]
sort
[1 ,2 ,2 ,5 ,7]
1 2 3 4 5
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment