zoukankan      html  css  js  c++  java
  • 【排序】SelectSort

    Why does it call Select Sort ?

    It's name contains what kind of method it uses:

    Every time by comparing i_item  from  bottom to top ,if found smaller one, mark the one.

    In one loop, one smallest one is found.

    then , exchange the right position of i_item find smallest in that loop and it's original position. 

    //SelectSort.c
    #include <stdio.h>
    #include "type.h"
    void swap(ElemType *a, ElemType *b)
    {
        ElemType *tmp = *a;
        *a=*b;
        *b=tmp;
    }
    void SelectSort(SqList *L)
    {
        int min_key; ElemType min_value;
        forint i= 0; i< L->length; i++)
        {
            
            min_key = i; 
            min_value = L->data[i];
            forint j= i+1; j< L->length; j++)
            {
                if(min_value>L->data[j])
                {
                    min_value  = L->data[j];
                    min_key = j;
                }
            }
            if(i!=min_key)    
            swap( &(L->data[i]) , &(L->data[min_key] ));
        }
    }
    void printContent(SqList *L)
    {
        for(int i = 0; i< L->length; i++)
        {
            printf("%d \t",L->data[i] );
        }
    }
    void main(void)
    {
        SqList l ;
        
        l.data[0] = 9;
        l.data[1] = 1;
        l.data[2] = 5;
        l.data[3] = 8;
        l.data[4] = 3;
        l.data[5] = 7;
        l.data[6] = 4;
        l.data[7] = 6;
        l.data[8] = 2;
        l.length = 9;
        printContent(&l);
        printf("\n");
        SelectSort(&l);
        printContent(&l);
        printf("\n");
    }

     Ref to a blog about swap function.

     file of "type.h" ref to this blog.

     previous blog about Select Sort ref to here

  • 相关阅读:
    BZOJ1787 [Ahoi2008]Meet 紧急集合[结论题]
    poj3728 The merchant[倍增]
    poj2750 Potted Flower[线段树]
    poj2482 Stars in Your Window[扫描线]
    poj2182 Lost Cows[BIT二分]
    UVA12096 The SetStack Computer
    第05组(65) 需求分析报告
    团队介绍与选题报告
    数据采集技术第三次作业
    结对编程作业
  • 原文地址:https://www.cnblogs.com/no7dw/p/2252000.html
Copyright © 2011-2022 走看看