Then incorporate calls to this function where you set "smallest"Code:size_t find_min(int *array, size_t lastIndex) { size_t i; int min = INT_MAX; for(i = 0; i <= lastIndex; ++i, ++array) if(*array < min) min = *array; return min; }
Then incorporate calls to this function where you set "smallest"Code:size_t find_min(int *array, size_t lastIndex) { size_t i; int min = INT_MAX; for(i = 0; i <= lastIndex; ++i, ++array) if(*array < min) min = *array; return min; }
Then perhaps a merge-selection-sort is in order :-)
Touche. It would be better and still be a selection-sort Good thinking.
Parts of the original post omitted ...Unfortunately that's not Selection Sort, you've got an item swap inside the inner loop. It's what I've seen called "Simple Sort".
The idea of Selection Sort is that you select the lowest or highest item each time without moving any items in the process, and then only when you've found that lowest or highest item, do you actually swap anything.
It's supposed to be similiar to what you'd do if you had to say, physically line up a whole lot of vehicles in order of weight. You really don't want to move any vechicles more than you have to. This is where the strength of Selection Sort lies.
The easiest way to go from an O(n*n) sorting algorithm to something almost as simple as Bubble Sort, yet much better in terms of Big-Oh-Notation, is undoubtedly to choose Comb Sort.
Whether I'd recommend using anything better than that though depends on how many items you're sorting for starters.
Last edited by iMalc; 11-06-2008 at 12:39 AM.
ok call it that, however my idea was to have two swaps at the same time. Simple sort is supposed to be O(n-1) right, but what i wanted was to make it something that makes two comparisons at the same time, i looked at the merge sort but its not really what i wanted. Id be breaking into two and so on, the idea behind this was to identify the highest and lowest values and swap in one loop both items.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
>> the idea behind this was to identify the highest and lowest values and swap in one loop both items.
So you are wanting to sort from highest to lowest, simulataneously with lowest to highest? Right? Again, this is iMalc's specialty moreso than mine but I don't think you would be gaining anything since you would fundamentally be O(2n^2).
What specifically are you trying to do? Friends don't give friends O(2n^2) code.
ya i know, we've gone over quicksort too which is way better. Like i had said the idea was to pick out first and last elemente with each low and high points. Loop around, and everytime it encounters one bigger and smaller in the array on ints swap them. So instead of having one swap like in selection sort one would have two swaps. Hence the prof name " a little improved selectionsort called "two tail sort". Dnt know if it makes anysense.
Feel free to look at Malcolm's site. When it comes to sorting methods he really is one of the most knowledgeable on this forum. And indeed he did see the thread here. If anything you can always start your own. Threads about sorting algorithms capture his attention as if a moth near a flood light.