Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Quick Sort
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Saad




PostPosted: Fri May 18, 2007 6:24 pm   Post subject: Quick Sort

Quick sort is a fast way of sorting
I have added an example of using quicksort. You can remove the order parameter as i wanted to know the order of the original values sorted
code:

%Quick Sort by Saad aka a100
proc Swap (var num1, num2 : real)
    var tnum := num1
    num1 := num2
    num2 := tnum
end Swap

proc Quick_Sort (var num : array 1 .. * of real, start, finish : int, var order : array 1 .. * of real)
    if start < finish then
        var pivot := num (start)
        var up := start
        var down := finish
        loop
            exit when up >= down
            loop
                exit when up > finish or num (up) > pivot
                up += 1
            end loop
            loop
                exit when down < start or num (down) <= pivot
                down -= 1
            end loop
            if (up < down) then
                Swap (num (up), num (down))
                Swap (order (up), order (down))
            elsif (up > down) then
                Swap (num (start), num (down))
                Swap (order (start), order (down))
                Quick_Sort (num, start, down - 1, order)
                Quick_Sort (num, down + 1, finish, order)
            end if
        end loop
    end if
end Quick_Sort



var num, o : array 1 .. 10 of real
put "Orig"
for i : 1 .. upper (num)
    num (i) := Rand.Int (0, 500)
    o (i) := i
    put num (i), " num ", o (i)
end for

var ime := Time.Elapsed
Quick_Sort (num, 1, upper (num), o)
var ime2 := Time.Elapsed

put "Time Taken is ", ime2 - ime, " ms "
locate (1, 40)
put "Sorted"
for i : 1 .. upper (num)
    locate (i + 1, 40)
    put num (i), " num ", o (i)
end for

Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: