[파일명:  testSort.go]------------------------------------------------
package main

import (
    "fmt"
    "os"
    "sort"
)

func quickSort(array []string, start, end int) {
    var i int = start
    var k int = end
    var pivot string

    if end - start >= 1 {
        pivot = array[start]
        for k > i {
            for cmp(array[i], pivot) >= 0 && i <= end && k > i {
                i += 1
            }
            for cmp(array[k], pivot) < 0 && k >= start && k >= i {
                k -= 1
            }
            if k > i {
              swap(array,i, k)
            }
        }
        swap(array, start, k)

        quickSort(array, start, k - 1)
        quickSort(array, k + 1, end)
    } else {
        return
    }
}

func cmp(s string, t string) int {
     var slen int = len(s)
     var tlen int = len(t)
     if slen == 0 && tlen == 0 {
         return 0
     } else if slen == 0 {
         return 1
     } else if tlen == 0 {
         return -1
     }
     var minlen int = slen
     if tlen < slen {
         minlen = tlen
     }
     var i int
     for i = 0; i < minlen; i++ {
         if s[i] < t[i] {
             return 1
         } else if s[i] > t[i] {
             return -1
         }
     }
     if slen == tlen {
         return 0
     } else if slen < tlen {
         return 1
     } else if tlen > tlen {
         return -1
     }
     return 0
}

func swap(array []string, x int, y int) {
     var TBUF string = array[x]
     array[x]=  array[y]
     array[y] = TBUF
}

func printArray(array []string, start int,end int) {
    var i int
    fmt.Printf("[")
    for i = start; i < end; i++ {
        fmt.Printf("%s, ", array[i])
    }
    if end >= start {
        fmt.Printf("%s", array[end])
    }
    fmt.Printf("]\n")
}

func main() {
    var i int
    if len(os.Args) > 1 && len(os.Args) < 22 {
        var SBUF []string = make([]string, len(os.Args) - 1)
        for i = 0; i < len(os.Args) - 1; i++ {
            SBUF[i] = fmt.Sprintf(os.Args[i+1])
        }
        print("Original array: ")
        printArray(SBUF, 0, len(os.Args) - 2)

        quickSort(SBUF, 0, len(os.Args) - 2)
        print("Sorted array: ")
        printArray(SBUF, 0, len(os.Args) - 2)
    } else if len(os.Args) > 21 {
        fmt.Printf("Given too many items. (The counter of items should be less then 20.)\n")
    }
}
------------------------------------------------


프롬프트> chcp 65001
Active code page: 65001

프롬프트> go run testSort.go 하나 둘 셋 넷
Original array: [하나, 둘, 셋, 넷]
Sorted array: [넷, 둘, 셋, 하나]

프롬프트> go run testSort.go one two three four
Original array: [one, two, three, four]
Sorted array: [four, one, three, two]

프롬프트> chcp 949
활성 코드 페이지: 949

 

 


 

Posted by Scripter
,