View Full Version : Sorting and Searching Structures
Silence
08-27-2002, 05:45 AM
Hey all.
Wondering if any of you know where i can find examples or sample code of bubble sorting and binary searching with structures.
I really need to know how to and i have no idea
In C by the way :)
Thanks
A google search on "bubble sorting and binary searching structures" came up with a bunch of hits.
Bradmont
08-27-2002, 12:43 PM
Why on earth do you want to use a bubble sort? It's a really inefficient sort - use something faster, like a quicksort or a heap sort.
/me smells someone's homework...
sicarius
08-27-2002, 09:23 PM
yeah, i think your right about the homework thing.
sans-hubris
08-27-2002, 11:32 PM
You really should come up with your own examples. These are very basic programming problems.
However, I'll give a recap on bubble sort (which, as already mentioned, is horribly inefficient.) In bubble sort, you're given an unsorted array, and you examine each number in the array, as well as the number after it. If the number you're currently examining is larger than the one after it, then the two switch places, and continue on to the next number until you get to the end of the array, and then you start all over. You're finished when you go over the array, and no numbers switch places.
Binary searches take a sorted array, and the number its searching for (this will be denoted as the variable s.) If s is equal to the middle number, then you're done. If s is larger than the number in the middle of the array, then you do a binary search of the upper half of the array (all the numbers that are greater than the middle number.) Otherwise you do a binary search on the lower half of the array (all the number less than the middle number, including the middle number.) I'll let you figure out what you need to test for to see if the number is not found since there are a few different tests for this.
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.