Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //var 1
- package main
- import (
- "fmt"
- "strconv"
- )
- type arrInt []int
- func partiton(low int, high int, less func(i, j int )bool, swap func(i, j int)) int{
- i := low
- for j := low; j < high; j++ {
- if less(j,high) {
- swap(i,j)
- i++
- }
- }
- swap(i,high)
- return i
- }
- func quickSortRec(low int, high int, less func(i, j int) bool, swap func(i, j int)){
- if low < high{
- q := partiton(low, high, less ,swap)
- quickSortRec(low , q - 1, less, swap)
- quickSortRec(q + 1, high, less, swap)
- }
- }
- func qsort(n int, less func(i, j int) bool,swap func(i, j int)) {
- quickSortRec(0, n - 1, less, swap)
- }
- func main(){
- var n int
- var m int
- fmt.Scan(&n)
- arr := make([]int, n)
- for i:=0; i<n; i++ {
- fmt.Scan(&m)
- arr[i] = m
- }
- qsort(n,
- func(i, j int) bool {
- a := strconv.Itoa(arr[i])
- b := strconv.Itoa(arr[j])
- ch1, _ := strconv.Atoi(a + b)
- ch2, _ := strconv.Atoi(b + a)
- return ch1 > ch2 },
- func(i, j int){arr[i], arr[j] = arr[j], arr[i]})
- for i := 0; i < n; i++{
- fmt.Print(arr[i])
- }
- }
- // var 2
- package main
- import (
- "fmt"
- "strconv"
- "sort"
- )
- type arrInt []int
- func(arr arrInt) Less(i,j int) bool {
- a := strconv.Itoa(arr[i])
- b := strconv.Itoa(arr[j])
- ch1, _ := strconv.Atoi(a + b)
- ch2, _ := strconv.Atoi(b + a)
- return ch1 > ch2
- }
- func(arr arrInt) Swap(i,j int) {
- arr[i], arr[j] = arr[j], arr[i]
- }
- func (arr arrInt) Len() int { return len(arr) }
- func main(){
- var n int
- fmt.Scan(&n)
- arr := make([]int, n)
- for i := 0; i < n; i++ {
- fmt.Scan(&arr[i])
- }
- sort.Sort(arrInt(arr))
- for _, e := range arr{
- fmt.Print(e)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement