home *** CD-ROM | disk | FTP | other *** search
-
- /* Selection sort is not terribly fast, but the algorithm is the easiest
- * to understand. It searchs the data set for the smallest element, and
- * puts it in the first position. Then it seeks out the next smallest
- * element to put in the second position and so forth. Lots and lots of
- * comparision, but very little moving. Another of the O(n^2) variety sorts.
- * As this algorithm is quite stable, it doesn't perform any better or worse
- * on sorted data.
- *
- * Author: Julie Zelenski, NeXT Developer Support
- * You may freely copy, distribute and reuse the code in this example.
- * NeXT disclaims any warranty of any kind, expressed or implied, as to
- * its fitness for any particular use.
- */
-
-
- #import "SelectionSort.h"
-
-
- @implementation SelectionSort:GenericSort
-
-
- - init;
- {
- [super init];
- sortName = "Selection Sort";
- sortNum = SELECTION_SORT;
- return self;
- }
-
-
- - sort;
- {
- int c,d,smallest;
-
- [super sort];
- fcalls++;
- for (c = 0 ; c < numElements-1 ; c++){
- smallest = c;
- for (d = c+1; d < numElements; d++){ // find smallest element
- if ([self lessThan:d :smallest])
- smallest = d;
- }
- [self swap:smallest with:c]; // swap smallest with front position
- }
- [self sortDone];
- return self;
- }
-
- @end