Need a sorting routine - Printable Version +- QB64 Phoenix Edition (https://staging.qb64phoenix.com) +-- Forum: QB64 Rising (https://staging.qb64phoenix.com/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://staging.qb64phoenix.com/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://staging.qb64phoenix.com/forumdisplay.php?fid=10) +---- Thread: Need a sorting routine (/showthread.php?tid=1520) Pages:
1
2
|
Need a sorting routine - TerryRitchie - 03-04-2023 Does anyone have a sorting algorithm laying around I could use? Nothing fancy but something faster than a bubble sort. I have the following I need to sort: TYPE DATATYPE a AS INTEGER b AS INTEGER c AS INTEGER END TYPE REDIM SortedList(0) AS DATATYPE The sort will only be done on the value of 'a' (SortedList().a) and the values can range from 1 to 32767. The Index of SortedList() can also be from 1 to 32767. The first thing you're probably thinking is why not have the index value equal the value in 'a'... There can be multiple duplicate values in 'a'. A bubble sort will probably do fine for the array if less than 1000 indexes but I need a sort that will be faster than bubble for cases where the index surpasses 32000+ QuickSort? MergeSort? InsertionSort? Anyone? RE: Need a sorting routine - mnrvovrfc - 03-04-2023 QB64 Wiki is your friend. https://qb64phoenix.com/qb64wiki/index.php/SWAP (Example #3) RE: Need a sorting routine - mdijkens - 03-04-2023 Code: (Select All) Type DATATYPE RE: Need a sorting routine - bplus - 03-04-2023 +1 Exactly how I'd do it! RE: Need a sorting routine - RhoSigma - 03-04-2023 There's CodeGuy's sorting library in the old archived .org forum: https://qb64forum.alephc.xyz/index.php?topic=530.msg3830#msg3830 RE: Need a sorting routine - SMcNeill - 03-04-2023 How essential is speed to this process? With such a limited dataset, I imagine a counting routine would blaze through the process in a single pass. (Which would be useful when dealing with data sets with millions of elements in it.) Process would work like this: DIM A_String_Array(1 TO Array_Max_Limit) AS STRING Then you simply read the array in one loop and add the index to the proper array element. FOR I = 1 TO Array_Max_Limit A_String_Array(My_Data(I).a) = A_String_Array(My_Data(I).a) + MKL$(I) NEXT ^ That just built you an indexed array in one single pass. Now to "Sort" it, you can just read that array and rebuild your data from it. Count = 0 FOR I = 1 TO Array_Max_Limit FOR J = 1 TO LEN( A_String_Array(I)) STEP 4 'we stored our index as long values Count = Count + 1 index = CLV(MID$(A_String_Array(I), J, 4)) SWAP My_Data(Count), My_Data(index) NEXT NEXT There may be some glitchness in the above as I haven't tested it (I'm not at a PC with QB64 on it at the moment), but the concept is sound. It's the same process as what I use in my MemSort routines for integers and bytes -- the fastest "sort" isn't any sort routine. It's a simple count routine. Sorting is a process of take this, swap with that... repeat an excessive number of times. This is just a case of: Read the value, store the index in an array large enough to hold all our values. Our SWAP of information only comes once for each index. There's no repetition involved. And you can't get any faster than that! RE: Need a sorting routine - TerryRitchie - 03-04-2023 (03-04-2023, 08:58 AM)mnrvovrfc Wrote: QB64 Wiki is your friend. I didn't even think to look in the Wiki. I need to remember that thing contains more than just command information. Thank you. @mdijkens - Thank you for the code. I'll give it a whirl. @RhoSigma - I knew codeguy did this work some time back and have a text file with some of his work in it. This link is much better than what I have. Thank you. @SMcNeill - Speed is crucial in my use case. This may need to be done up to 60 times per second depending on the implementation. However keeping it to once per second is the goal. I'll give your method a whirl and see what the result is. Thanks for the replies everyone. RE: Need a sorting routine - SMcNeill - 03-04-2023 @TerryRitchie A working example for you: Code: (Select All) Screen _NewImage(800, 600, 32) RE: Need a sorting routine - TerryRitchie - 03-04-2023 (03-04-2023, 09:00 AM)mdijkens Wrote: Holy heck this routine is fast! It takes 5000 values before it even begins to show a time of .001 sec to complete. RE: Need a sorting routine - SMcNeill - 03-04-2023 Give my example above a run and let me know how it performs in comparison for you. https://staging.qb64phoenix.com/showthread.php?tid=1520&pid=14066#pid14066 |