Computer Science Canada

String Shuffler

Author:  Carey [ Fri Mar 28, 2008 7:40 am ]
Post subject:  String Shuffler

This program scrambles an array of strings. With some modifications, can be used with any type of variable.

Author:  Carey [ Mon Apr 07, 2008 7:38 am ]
Post subject:  RE:String Shuffler

Comments anyone?

Author:  Tallguy [ Thu Apr 17, 2008 10:03 am ]
Post subject:  RE:String Shuffler

"some modifications" wat is ur defination of this?

Author:  repsoccer16 [ Mon Apr 21, 2008 7:30 am ]
Post subject:  RE:String Shuffler

ya what are the modifications that you are talking about?

Author:  Carey [ Wed Apr 23, 2008 8:23 am ]
Post subject:  Re: String Shuffler

to make it sort any other type of array, just changes all the string variables into something else.

in the below example yourType is the type of variable to shuffle

note: changing the type without changing how you get and put the data will result in an error.

Example:
Turing:
type yourType : string

const fileName := "Data.txt"
var phrases : flexible array 1 .. 0 of yourType
var file : int

proc getWords
    open : file, fileName, get
    loop
        exit when eof (file)
        new phrases, upper (phrases) + 1
        get : file, phrases (upper (phrases)) : *
    end loop
    close : file
end getWords

proc putWords
    open : file, "scrambled" + fileName, put
    for i : 1 .. upper (phrases)
        put : file, phrases (i)
    end for
    close : file
end putWords

procedure quickSort (var nums : array 1 .. * of real, var p : array 1 .. * of yourType, left, right : int)
    var pivotPlace : int
    var tempN : real
    var tempP : yourType
    tempN := nums (left)
    tempP := p (left)
    nums (left) := nums ((left + right) div 2)
    p (left) := p ((left + right) div 2)
    nums ((left + right) div 2) := tempN
    p ((left + right) div 2) := tempP
    var lastSmall := left
    for i : left + 1 .. right
        if nums (i) <= nums (left) then
            lastSmall += 1
            tempN := nums (lastSmall)
            tempP := p (lastSmall)
            nums (lastSmall) := nums (i)
            p (lastSmall) := p (i)
            nums (i) := tempN
            p (i) := tempP
        end if
    end for
    tempN := nums (left)
    tempP := p (left)
    nums (left) := nums (lastSmall)
    p (left) := p (lastSmall)
    nums (lastSmall) := tempN
    p (lastSmall) := tempP
    pivotPlace := lastSmall
    if left < pivotPlace - 1 then
        quickSort (nums, p, left, pivotPlace - 1)
    end if
    if pivotPlace + 1 < right then
        quickSort (nums, p, pivotPlace + 1, right)
    end if
end quickSort

proc scramble (var a : array 1..* of yourType)
    var t : array 1 .. upper (a) of real
    for i : 1 .. upper (t)
        t (i) := Rand.Real
    end for
    quickSort (t, a, lower (t), upper (t))
end scramble

getWords
scramble (phrases)
putWords


: