Computer Science Canada Sorting in Turing and Returning Arrays from Function |
Author: | andytyk [ Mon Jul 19, 2004 11:40 pm ] |
Post subject: | Sorting in Turing and Returning Arrays from Function |
I was playing with different sorts in Turing, and list operations such as Merge Sort, Quick Sort etc. and needed to pass arrays in recursion to functions and return them as well. I tried the obvious first and found out that Turing will not permit it. So after digging through the Reference manual and finding nothing that will help, I decided to write a class that simply consists of a flexible array and some basic array functions (add, count members, etc.) and had a flexible array of pointers. So the functions simply returned the index in which the pointer lay for the result. This resulted in a horrendously low sorting speed. Using Quicksort and a pregenerated list of 1000 Rand.Real s. Sorting using the first term of the set as the pivot resulted in 288ms while using the average as the pivot resulted in 277ms. Is there a better way to handle this, or are there better ways to sort in Turing? |
Author: | Dan [ Tue Jul 20, 2004 11:02 am ] | ||||
Post subject: | |||||
While bubble sort may not be the fastested but it is easy and i know will work in turing for shure. this is bubble sort in c++
Where a is the array and n is the number if elments in the array. The -i part in the 2nd for is not need but incereces the speed. a smiple verson in turing whould be:
|
Author: | andytyk [ Tue Jul 20, 2004 12:18 pm ] | ||
Post subject: | |||
Hmm... I'm just thinking about the speed, sorting 100,000 Rand.Reals I can get around 3000-4000 sorts per second using Quicksort without a combo with Insertion. This is the code I wrote:
Since I'm a novice at this, I'm sure there are plenty of ways to speed up the code, or a better way to handle the arrays. Memory usage is also a problem. I left out a free statement in one of the quicksorts and ended up with a 2 gig pagefile usage when sorting 1 million Rand.Real s. Memory usage is down to around 200-300 MBs when sorting 1 million Rand.Reals. Any suggestions or comments? |
Author: | Tony [ Tue Jul 20, 2004 1:44 pm ] |
Post subject: | |
instead of making up an array class (known as vector in C++ and ArrayList in Java I belive), why not just stick to the basics? rether then passing the entire array back and forth, can't you just declear it as global where every function can access it? I think your bottleneck is that you have to pass the entire array to the next function on each pass of the loop or whatever. This would slow down your program exponensially, and you always want to avoid that. |
Author: | andytyk [ Tue Jul 20, 2004 2:53 pm ] |
Post subject: | |
Hmm... if we just declared the arrays as globals (which means there will be a predefined number of them), we'll be stuck with using in-place sorting algorithms then.. And Bubblesort is great for small arrays (such as those with less than 100 values), its speed matches the Quicksort (avg) at around 5000 sorts per second. But if we're going to be sorting larger arrays... ![]() Array Size - BubbleSort - Quicksort (using average pivot) (sorts per second) -------------------------------------------------------------------- 50 - 11000 - 6250 100 - 6250 - 5000 500 - 1114 - 4065 2500 - 226 - 3374 3000 - 185 -3398 I'll play around with it to see where the bottleneck is. |
Author: | Dan [ Tue Jul 20, 2004 4:16 pm ] |
Post subject: | |
Just whondering, what are u making that u need to sort an array of 3000? or are you just trying to find the best posable method of sorting? It realy is to bad turing dose not have lists or vectors like java and c++. |
Author: | andytyk [ Tue Jul 20, 2004 5:28 pm ] |
Post subject: | |
Well, a little of both, I want to find the fastest sorting method in Turing as well as finding which triangles to draw first in my 3d engine by sorting the z-values. Coming from C++, PHP, etc. getting Turing to do what you want is a pain. Using a QuickBubble sort (Quick sort for initial breaking down of arrays to small arrays of 20 or less then bubble sorting) yields pretty good results, I think I'll do a QuickInsertion and then compare. Array size -> QuickBubble (sorts per second) 50 - 12500 100 - 11111 1000 - 5952 10000 - 4492 100000 - 3364 |
Author: | Catalyst [ Wed Jul 21, 2004 1:20 am ] |
Post subject: | |
well if ur using it for a 3d engine then you can sometimes get away with a bubble if u do maybe ten passes (and the rest is fast) because of temporal coherence |
Author: | Catalyst [ Wed Jul 21, 2004 1:26 am ] | ||
Post subject: | |||
would this do for a quicksort for your needs? (my tests get 9k-10k sorts per second)
|
Author: | andytyk [ Wed Jul 21, 2004 11:49 am ] |
Post subject: | |
That's interesting partitioning the array like that, never thought to do it that way. Thanks, I think I can get it up to 15k sorts per second now ![]() |