Annotation of Examples/AppKit/SortingInAction/SelectionSort.m, revision 1.1

1.1     ! root        1: 
        !             2: /* Selection sort is not terribly fast, but the algorithm is the easiest 
        !             3:  * to understand. It searchs the data set for the smallest element, and 
        !             4:  * puts it in the first position.  Then it seeks out the next smallest 
        !             5:  * element to put in the second position and so forth.  Lots and lots of 
        !             6:  * comparision, but very little moving. Another of the O(n^2) variety sorts.  
        !             7:  * As this algorithm is quite stable, it doesn't perform any better or worse 
        !             8:  * on sorted data.
        !             9:  *
        !            10:  * Author: Julie Zelenski, NeXT Developer Support
        !            11:  * You may freely copy, distribute and reuse the code in this example.  
        !            12:  * NeXT disclaims any warranty of any kind, expressed or implied, as to 
        !            13:  * its fitness for any particular use.
        !            14:  */
        !            15: 
        !            16: 
        !            17: #import "SelectionSort.h"
        !            18: 
        !            19: 
        !            20: @implementation SelectionSort:GenericSort
        !            21: 
        !            22: 
        !            23: - init;
        !            24: {
        !            25:     [super init];
        !            26:     sortName = "Selection Sort";
        !            27:     sortNum = SELECTION_SORT;
        !            28:     return self;
        !            29: }
        !            30: 
        !            31: 
        !            32: - sort; 
        !            33: {
        !            34:     int c,d,smallest;
        !            35:     
        !            36:     [super sort];
        !            37:     fcalls++;
        !            38:     for (c = 0 ; c < numElements-1 ; c++){
        !            39:        smallest = c;
        !            40:        for (d = c+1; d < numElements; d++){ // find smallest element
        !            41:            if ([self lessThan:d :smallest])
        !            42:                smallest = d;
        !            43:        }
        !            44:        [self swap:smallest with:c];    // swap smallest with front position
        !            45:     }
        !            46:     [self sortDone];
        !            47:     return self;
        !            48: }
        !            49:        
        !            50: @end

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.