Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Jonathon Dilworth
Jonathon Dilworth
2,871 Points

Up until what point is it acceptable to use Bubble Sort?

So, let's say you've got 50 key-value pairs in an array that you want to sort by value. From a users perspective you're probably not going to see much of a performance benefit at all (if any) from choosing an alternative sorting algorithm.

But let's say you've got 1000 key-value pairs, how about 10,000; at what level are you going to start seeing a noticeable performance benefit from using an alternative sorting algorithm? Assuming we're dealing with a request to a web-server that performs the sorting in the back end using PHP; requests are only made a couple of times a day and none of the data is cached.

4 Answers

I don't know the performance issues, but where does that data come from? If it is a database, order it in your query. I would otherwise look at ordering it as it is acquired by the site.

What about writing the API data to the database first? Then you sort easily. If you don't want the data to persist, delete it after the session.

Jonathon Dilworth
Jonathon Dilworth
2,871 Points

I'm dealing with records that have multiple attributes - the records populate a table and the table is sortable by the user; for instance: the user can sort by price, by timestamp, etc - the kind of thing you might see on an e-commerce website. Sorting by attributes that are pulled directly from the database is quite convenient, because it means I can specify a limit, an offset and an order, this is also done via an Ajax request too, so it makes it seem a little more seamless for the user.

However, the problem lies in that fact that some of the columns in the table are populated partially through calls to an API; so in this instance, if the user wants to sort by one of these columns, I'm going to have to pull all the rows, make two separate API calls, wait for all of the data to be returned, aggregate the data and then sort it; so it becomes quite computationally intensive quite quickly. At the moment, it doesn't necessarily matter, since I'm dealing with fairly small numbers of records; however, ideally I would like the solution to scale.

Jonathon Dilworth
Jonathon Dilworth
2,871 Points
  • When the data is returned by the API call, it's dumped into an array of key-value pairs, where the key is representative of the row and the value is what needs to be sorted, hence the question :) I hope that clarifies things a little bit?
Jonathon Dilworth
Jonathon Dilworth
2,871 Points

Thanks for the responses Ted. In case anyone else is facing a similar problem and is curious, I ended up deciding to only load in the relevant data initially (querying the DB with a limit of ten and an offset of zero), so that it doesn't take too long to load the initial table view; but if the user decides to sort by a calculated column, an Ajax request is sent to the server and API calls for all rows are made, then all response data is dumped in a table which will be cached for an hour, so that any further sorting for that session can be done pretty quickly, whilst keeping it fairly up to date.

That sounds like a very reasonable solution.