Scala 언어로는 리스트 대신 배열로 처리하였다.
 
[파일명:  testStringFindInArray.scala]------------------------------------------------
object TestStringFindInArray {
    def quickSort(arr: Array[String], start: Int, end: Int) {
        var i: Int = start
        var k: Int = end
        var pivot: String = ""

        if (end - start >= 1) {
            pivot = arr(start)
            while (k > i) {
                 while (arr(i) <= pivot && i <= end && k > i) {
                     i += 1
                 }
                 while (arr(k) > pivot && k >= start && k >= i) {
                     k -= 1
                 }
                 if (k > i) {
                     swap(arr, i, k)
                 }
             }
            swap(arr, start, k)

            quickSort(arr, start, k - 1)
            quickSort(arr, k + 1, end)
        }
    }

    def swap(arr: Array[String], x: Int, y: Int) {
        var tmp: String = arr(x)
        arr(x) = arr(y)
        arr(y) = tmp
    }

    def find(arr: Array[String], s: String): Int = {
        var i: Int = 0
        for (i <- 0 to arr.length - 1) {
        if (arr(i).indexOf(s) >= 0) {
     return i
        }
        }
    return -1
    }

    def printArray(arr: Array[String]) {
        print("[")
        for (i <- 0 to arr.length - 2) {
           print(arr(i) + ", ")
        }
        println(arr(arr.length - 1) + "]")
    }

    def main(args: Array[String]) {
        var words: Array[String] = Array[String]( "하나", "둘", "셋", "넷", "다섯", "여섯" )
        var where: Int = 0

        print("array: ")
        printArray(words)

        where = find(words, "셋")
        if (where > 0) {
            print("발견!  ")
            println("Next word of 셋 in array: " + words(where+1))
        }

        quickSort(words, 0, words.length - 1)

        print("array: ")
        printArray(words)

        where = find(words, "셋")
        if (where > 0) {
            print("발견!  ")
            println("Next word of 셋 in array: " + words(where+1))
        }
    }
}
------------------------------------------------


컴파일> scalac -encoding ms949 testStringFindInArray.scala

실행> scala testStringFindInArray
array: [하나, 둘, 셋, 넷, 다섯, 여섯]
발견!  Next word of 셋 in array: 넷
Sorting...
array: [넷, 다섯, 둘, 셋, 여섯, 하나]
발견!  Next word of 셋 in array: 여섯


크리에이티브 커먼즈 라이선스
Creative Commons License

Posted by Scripter
,