Sorting a multidimensional array

JdV

Active Member
Licensed User
Longtime User
Hello

What's the fastest way to sort a multidimensional array?

For example, I have an array which consists of the following mixed data:
Account number, First name, Surname, Date of birth
I want to carry out a sort based on the account number.

Using a slightly adjusted version of the the bubble sort method (shown here) I can quickly sort a few hundred rows. When I have tested with 2,000 rows there is a long pause before the iterative part of the sort takes place. Once that begins it takes less than a second.

A similar thing can be seen if the program in the 'Sorting Algorithms Tutorial' is adjusted to have 2,000 items in the list.

Is there a reason for this delay? Is there a better way of sorting multidimensional arrays?

I was hoping to avoid using a database as I didn't think a few thousand rows was that many!

Regards

Joe
 

JdV

Active Member
Licensed User
Longtime User
Klaus

Thanks for the quick reply!

I will investigate the db option.

Just for my own reference:
- Is 2,000 rows an unreasonable amount for an array to handle?
- Is there an explanation for the pause that takes place before the sorting?

Regards

Joe
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Is 2,000 rows an unreasonable amount for an array to handle?
I'm afraid yes.
Is there an explanation for the pause that takes place before the sorting?
Are you sure there is a pause before sorting? Isn't it simply the time needed to do the sorting.

The advantage of a database is the big number of possibities.

Best regards.
 
Upvote 0

JdV

Active Member
Licensed User
Longtime User
Are you sure there is a pause before sorting? Isn't it simply the time needed to do the sorting.

Good point.

I added a progress bar in the sort loop and that seemed to leap from 0% to 100% in one step.

Though now that I think about it, this progress bar on the screen wouldn't be updated while the loop is being processed would it?
 
Upvote 0

joseluis

Active Member
Licensed User
Longtime User
You must include a DoEvents inside the loop in order to view the changes before the loop finishes. It will make it slower, though.
 
Upvote 0

JdV

Active Member
Licensed User
Longtime User
Thanks all for your help!

I added DoEvents to the sort routine and it updated the progress bar. It also made the performance so much worse that I gave up on that idea!

The only reason I chose the 'Bubble Sort' was because I could understand and change the code easily!

When I have more time I might look at the 'Quick Sort' code option. It has been much quicker on the tests I carried out on a single dimension array containing a few thousand items.

Regards

Joe
 
Upvote 0

joseluis

Active Member
Licensed User
Longtime User
You could also call DoEvents every 10 loops or so... Or use a Timer to call it every 500 milliseconds
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
This reminds me a story about 3 decades ago with a dynamic calculation program where one calculation session lasted about 20 minutes. I added a display to show the remaining time, unfortunately this did more than double the calculation time. Finaly, I calculated the estimated finish hour and displayed this one.
But well, this was the so called 'good old time'.

Best regards.
 
Upvote 0

JdV

Active Member
Licensed User
Longtime User
You could also call DoEvents every 10 loops or so... Or use a Timer to call it every 500 milliseconds

I tried calling the DoEvents every 10th step but the performance still wasn't very good.

I have now tried sorting the array with the 'Quick Sort' method and its performance is just as bad.

I will concede that my coding probably isn't all that efficient!

Anyway, for the purpose of what I'm trying to achieve with this test the db looks like the best option.
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
I tried calling the DoEvents every 10th step but the performance still wasn't very good.

I have now tried sorting the array with the 'Quick Sort' method and its performance is just as bad.

I will concede that my coding probably isn't all that efficient!

Anyway, for the purpose of what I'm trying to achieve with this test the db looks like the best option.

I'd be interested in seeing the code you used for QuickSort.
 
Upvote 0

JdV

Active Member
Licensed User
Longtime User
Please see the text file attached. It is just the parts of the original 'Quick Sort' code that I copied and adjusted.

I call it like this:

B4X:
Dim now As Long   : now = DateTime.now   
   
QuickSort(0, MyArray.Length - 1)

Msgbox("It took: "&(DateTime.now - now)&" ms", "")

The MyArray array has 5,000 items for this test and it took around 22 seconds on the emulator.
 

Attachments

  • QuickSortCode.txt
    1.2 KB · Views: 419
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Please see the text file attached. It is just the parts of the original 'Quick Sort' code that I copied and adjusted.

I call it like this:

B4X:
Dim now As Long   : now = DateTime.now   
   
QuickSort(0, MyArray.Length - 1)

Msgbox("It took: "&(DateTime.now - now)&" ms", "")

The MyArray array has 5,000 items for this test and it took around 22 seconds on the emulator.

I'm going to take a look at the code, but you do know that it will run much slower on an emulator, right?
 
Upvote 0

JdV

Active Member
Licensed User
Longtime User
I'm going to take a look at the code, but you do know that it will run much slower on an emulator, right?

On my Galaxy Mini it takes about 9 seconds to sort 5,000 rows.

Thanks for your help. There's really no need to spend much time on it. I was only investigating the different storage/sorting options available.

It seems that the db option is working out to be the best one by far.
 
Upvote 0
Top