• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Bubble Sort

RedArmy

Platinum Member
Let's say you have a bubble sort like this

Now, suppose that your "data" isn't a member within a simple structure (not shown) where it can easily get called, but rather it's the result of another calculation (like the area of a shape or something) within a separate function that gets looped through for as many shapes as you have when called. This function calculates the area on the fly (only when the function is called) and the values that are used for calculating the area are stored, but that's it.

How would you go about modifying this bubble sort (or would you use a completely different kind of bubble sort methodology?) so that you could arrange all the areas of as many shapes as you have in increasing (or decreasing, doesn't really matter) order? If you had to create another function that's perfectly acceptable.

I'm just wondering about this since I'm looking over all the different kinds of sorting algorithms and the different examples out there and I wanted to implement a bubble sort in mine...but as the scenario is described above...mine varies in terms of functions used and the way you go about getting "data".

Any insight is appreciated! Thanks !!
 
Unless the data is dependent on the amount of data at that instant - run the calculation once and the store it for later reference.

If the data is fully dynamic, you may be SOL (from my perspective)
 
Wait, so you're calculating the area, and want to sort the area values, but not store the area values? If you're not storing them, what's the point of sorting them in the first place? 😕 Even if you're printing every individual value to the console as it's calculated, you're still storing it in a sense. It'd be tricky, but you could theoretically retrieve those values. If you're using swing, it's easy as hell. Still, such a method is pathetically impractical when compared to simply storing the values in an efficient data structure.

Just use a HashMap to store the values. IIRC it has O(1) efficiency for reading and storing values, so it shouldn't interfere by much at all of the program is real-time or something.

Since it's the Map-Key style, you could actually associate the areas with the relevant shape/dimensions that define it.

That's in Java at least. Not sure about the implementation in other languages.
 
Well, in any real high level language, you could do this all easily by saying something like "sortBy (comparing area)".

If you're stuck on your C bubble(eww) sort, then change your comparison to something like "area(a) < area(b)"
 
Back
Top