Seven Bubble sort for you: which do you choose?
#9
(03-12-2023, 11:05 PM)Kernelpanic Wrote:
Quote:More important than being able to pick between various sorting routines, is learning *WHY* the various routines work, and the principle behind them.

That is the crucial point!
Faster sorting is done using the index of records. It works similar to sorting via pointers as in C. But I would have to deal with that first.

That's one big issue when dealing with sorting -- blanket statements such as the above aren't always true.  Sorting via index may -- or may not -- be faster than sorting the actual data.  Without a valid dataset to compare such a statement against, any blanket speed performance statement has to be suspect for validity. 

For example, let's say my data is just 10 integer values.   (1, 3, 5, 2, 4, 8, 9, 7, 0, 6)   Now, is it going to be faster to shuffle those 2-byte integer values around in memory, or would it be faster to shuffle a 2-byte index which points to those values?

Not surprisingly, it'd be faster to move the data itself Array(X) instead of the referencing array (Array(Index(X)).  Both are moving integer data about in memory, but where the first directly addresses that integer data, the second has to address an set of pointers first, to see what the data is, and then move the integer pointers to the proper place.

************************

Where indexing is done is in cases where:

1) It's vital that the data remains static and unmoving.

OR

2) It's faster to move the reference pointer than it is to move the actual data itself around in memory.  (Which is something you often see with user defined types or larger strings in memory.)

But, indexes aren't the end-all be-all answer for speed for sorting purposes.  In *many* cases, they're faster than any other method, but not in *all* situations.  You really need a working data set to find the best solution to what works best and fastest for it.



For an example of a very limited case sort which is generally going to outperform everything under the sun, take a look at a Binary Insertion Sort with previously sorted data.

(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)   <-- -Sorted Data.  You want to add 3.5 to that data as a new element.
Then you check the data from the midpoint of each extreme, narrowing in on where it goes by halves:
1 to 10... midpoint 5  <-- goes left of that 5.
1 to 4... midpoint 2 >-- goes right of that 2.
3 to 4... midpoint 3 >-- goes right of that 3.

3 passes and you now know that the data goes after the 3.   In one move, you _MEMCOPY the first three elements of the array, add the new element, and then _MEMCOPY the rest of the array.

(1, 2, 3) +  (3.5) + (4, 5, 6, 7, 8, 9, 10) = (1, 2, 3, 3.5, 4, 5, 6, 7, 8, 9, 10)

You're not going to get any faster than that, in most cases.  (Unfortunately, it's a very limited use case which depends on the data basically being sorted to begin with and which you're only interested in adding new data to the existing data set.)

Without actual data to work with, it's impossible to say which sorting method is going to be the best or the fastest.  It's the reason why there's so many different methods out in the wild world of programming -- it's always an ease of implementation verses a need for performance with any given set of data.  Wink
Reply


Messages In This Thread
RE: Seven Bubble sort for you: which do you choose? - by SMcNeill - 03-13-2023, 12:09 AM



Users browsing this thread: 11 Guest(s)