Help with bubblesort.

Started by
17 comments, last by caffeineaddict 20 years, 5 months ago
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.
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!}

--{You fight like a dairy farmer!}

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]
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.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Thanks for the replies. I''ve changed it over to use a struct, but need to make a function to swap integers, then i should be done, thanks again.
template<typename T>void swap(T& a, T& b){    T temp = a;    a = b;    b = temp;}


Or you could just use std::sort().
GSACP: GameDev Society Against Crap Posting
To join: Put these lines in your signature and don't post crap!
Bubble sort and similar are O(N^2)...merge sort(quick sort) is O(NlogN). Quick sort is implemented in std (and also in C). Why bubble?
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
struct is a C keyword...
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Yeah, i know struct is a C keyword, i just meant the style, it''s a bit like oop, well, maybe just a little more structured programming is what i''m looking for.

This topic is closed to new replies.

Advertisement