Sort-selection sort-simple selection sort (direct selection sort)

Simple selection sort

Simple Selection Sort is also called direct selection sort .

Algorithm ideas

[Algorithm steps]

① Assume that the records to be sorted are accessed in the array r[1...n]. The first pass starts from r[1], and through n-1 comparisons, the record with the smallest keyword is selected from the n records, recorded as r[k], and r[1] and r[k] are exchanged.
② The second pass starts from r[2]. Through n-2 comparisons, the record with the smallest keyword is selected from the n-1 records, recorded as r[k], and r[2] and r[k] are exchanged. .
③ By analogy, the i-th pass starts from r[i], and through ni comparisons, the record with the smallest keyword is selected from n-i+1 records, as r[k], and r[i] and r[ are exchanged. k].
④ After n-1 times, the sorting is completed.

Algorithm implementation

Define the linear table structure for storing records

//记录
typedef struct ElemType{
    
    
    int key;
    string info;
}ElemType;

#define InitSize 50
typedef struct {
    
    
    ElemType data[InitSize];
    int length;
}SqList;

void toString(SqList L);

void toString(SqList L,int low,int high);

void InitSqList(SqList &L);

Code

void SelectSort(SqList &L) {
    
    
    ElemType min = L.data[0];
    for (int i = 0; i < L.length-1; i++) {
    
    
        int k = i;
        cout << " -----------------第" << k+1 << "趟排序--------------------- "
             << endl;
        cout << "初始min r["<< k <<"]= " << L.data[k].key
             << endl;
        for (int j = i + 1; j < L.length; j++) {
    
    
            if (L.data[j].key < L.data[k].key) {
    
    
                k = j;
                cout << "新的min r["<< k <<"]= " << L.data[k].key << endl;
            }
        }
        if (k != i) {
    
    
            swap(L.data[i], L.data[k]);
        }
        cout << "有序 " ;
        toString(L,0,i);
        cout << "待排序 " ;
        toString(L,i+1,L.length-1);
    }
}

Test Case

void testSelectSort(){
    
    
    SqList L;
    InitSqList0(L);
    cout <<" ----------------------------初始 L----------------------------------"<< endl;
    toString(L,0,L.length-1);
    SelectSort(L);
    cout <<" -------------------------最终排序结果--------------------------------"<< endl;
    toString(L,0,L.length-1);

}
int main() {
    
    
    testSelectSort();
    return 0;
}

Test Results

 ----------------------------------初始 L----------------------------------------
 [ 41 67 34 0 69 24 78 58 62 64 ]
 -----------------------------------1趟排序--------------------------------------- 
初始min r[0]= 41
新的min r[2]= 34
新的min r[3]= 0
有序  [ 0 ]
待排序  [ 67 34 41 69 24 78 58 62 64 ]
 -----------------------------------2趟排序--------------------------------------- 
初始min r[1]= 67
新的min r[2]= 34
新的min r[5]= 24
有序  [ 0 24 ]
待排序  [ 34 41 69 67 78 58 62 64 ]
 -----------------------------------3趟排序--------------------------------------- 
初始min r[2]= 34
有序  [ 0 24 34 ]
待排序  [ 41 69 67 78 58 62 64 ]
 -----------------------------------4趟排序--------------------------------------- 
初始min r[3]= 41
有序  [ 0 24 34 41 ]
待排序  [ 69 67 78 58 62 64 ]
 -----------------------------------5趟排序--------------------------------------- 
初始min r[4]= 69
新的min r[5]= 67
新的min r[7]= 58
有序  [ 0 24 34 41 58 ]
待排序  [ 67 78 69 62 64 ]
 -----------------------------------6趟排序--------------------------------------- 
初始min r[5]= 67
新的min r[8]= 62
有序  [ 0 24 34 41 58 62 ]
待排序  [ 78 69 67 64 ]
 -----------------------------------7趟排序--------------------------------------- 
初始min r[6]= 78
新的min r[7]= 69
新的min r[8]= 67
新的min r[9]= 64
有序  [ 0 24 34 41 58 62 64 ]
待排序  [ 69 67 78 ]
 -----------------------------------8趟排序--------------------------------------- 
初始min r[7]= 69
新的min r[8]= 67
有序  [ 0 24 34 41 58 62 64 67 ]
待排序  [ 69 78 ]
 -----------------------------------9趟排序--------------------------------------- 
初始min r[8]= 69
有序  [ 0 24 34 41 58 62 64 67 69 ]
待排序  [ 78 ]
 -------------------------------最终排序结果--------------------------------------
 [ 0 24 34 41 58 62 64 67 69 78 ]

进程已结束,退出代码0

Analysis of Algorithms

① Time complexity:

In the simple selection sorting process, fewer record moves are required.

  • Best case (positive sequence): no movement;
  • Worst case (reverse order): move 3(n-1) times.

However, regardless of the initial ordering of records, the required interkeyThe number of comparisons is the same, both are
KCN=(n-1)+(n-2)+…+(3-1)+(2-1)=n(n-1)/2.
Therefore,The time complexity of simple selection sort is O(n^2/2)

② Space complexity

Like bubble sort, an auxiliary space is only needed when two records are exchanged, soThe space complexity is O(1)

[Algorithm Features]

① itselfstableSorting method, using the "exchange records" strategy will produceunstable phenomenon.
② Yesfor chain structures.
③ The number of movement records is small, when each itemRecording takes up a lot of space, simple selection sort is faster than direct insertion sort.

Guess you like

Origin blog.csdn.net/QQ657205470/article/details/127754354
Recommended