Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

caffeineaddict

Help with bubblesort.

This topic is 5388 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, i''m trying to use bubble sort to sort 2 different arrays with different types of data, one array has names, and the other has numbers, i sort the names first since i want them in alphabetic order, so, when i want to sort the numbers that corresponds to the right words, how do i do this? I need some kind of offset from its original starting spot. Any help is appreciated.

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by caffeineaddict
i''m trying to use bubble sort to sort 2 different arrays with different types of data, one array has names, and the other has numbers, i sort the names first since i want them in alphabetic order...


ok wait a min, is there any reason you''re not group the two pieces of data together using struct or class?





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
Store the info in a structure, and sort based on the keys (strings in your case). Or, look into using any presupplied dictionaries/associative arrays. If you're using C++, consider std::map. It's hella fast, and SGI's hash_map is even faster. Those offer searching by key (string), as well as sorting.

Also, consider using Selection Sort, Insertion Sort, Merge Sort, or Quick Sort (some Google terms for you). All are more efficient than Bubble Sort, and about as easy to implement. I find a merge sort to be much simpler - and far more elegant - than a bubble sort.

Basically, unless you have to write a sort (special purpose, or some rule), use whatever's supplied. Do not fall prey to NIH (not invented here) syndrome.

[edited by - ze_jackal on November 17, 2003 4:34:30 PM]

Share this post


Link to post
Share on other sites
I had need to sort some parallel arrays before, and the prospects are not good.

Since you''re writing your own sort, make it a template function, and when-ever you swap elements in the first array, swap the same elements in the second one.

I snarfed the STL qsort and doctor''ed it up to do this.

I did find a parallel iterator which claimed it could be used with std::sort, but I was using MSVC6 at the time, so nothing worked.

Share this post


Link to post
Share on other sites
After i converted it over to structures I got it working quite well, i''d never used them before and found them to be quite enjoyable to use. I think i will migrate over to more OOP code in my programs, it seems much more flexible, i''ve just been diehard C this whole time :-p

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!