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

  • 相关阅读:
    hbase全分布安装配置
    ElasticSearch概述及Linux下的单机ElasticSearch安装
    Redis 集群搭建详细指南
    spark集群安装配置
    hbase全分布安装配置
    zookeeper图形界面工具zooinspector
    storm集群安装配置
    kafka集群安装配置
    sqoop配置安装以及导入
    Flume的安装部署
  • 原文地址:https://www.cnblogs.com/no7dw/p/2252000.html
Copyright © 2011-2022 走看看