zoukankan      html  css  js  c++  java
  • 数据结构:选择排序

    选择排序(Selection sort)是一种简单直观的排序算法。
    它的基本思想是:首先在未排序的数列中找到最小(or最大)元素,然后将其存放到数列的起始位置;接着,再从剩余未排序的元素中继续寻找最小(or最大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。

     1 void select_sort(int a[],int n)
     2 {
     3     int i,j,min;
     4     for(i=0;i<n;i++)
     5     {
     6         min=i;
     7         for(j=i+1;j<n;j++)
     8         {
     9             if(a[j]<a[min]) min=j;
    10         }
    11         if(min!=i) 
    12         {
    13             int temp=a[i];
    14             a[i]=a[min];
    15             a[min]=temp;
    16         }
    17     }
    18 }

    选择排序C++实现:

     1 #include <iostream>
     2 #include <cstdio>
     3 using namespace std;
     4 void select_sort(int a[],int n)
     5 {
     6     int i,j,min;
     7     for(i=0;i<n;i++)
     8     {
     9         min=i;
    10         for(j=i+1;j<n;j++)
    11         {
    12             if(a[j]<a[min]) min=j;
    13         }
    14         if(min!=i) 
    15         {
    16             int temp=a[i];
    17             a[i]=a[min];
    18             a[min]=temp;
    19         }
    20     }
    21 }
    22 int main()
    23 {
    24     int a[]={200,87,12,100,34,26,58};
    25     int ilen=sizeof(a)/sizeof(a[0]);
    26     select_sort(a,ilen);
    27     for(int i=0;i<ilen;i++) cout<<a[i]<<" ";
    28     cout<<endl;
    29     return 0;
    30 }
    天晴了,起飞吧
  • 相关阅读:
    hack games
    Metasploit 使用简介
    Back Track5学习笔记
    Metasploit没有db_autopwn命令的解决办法
    BT5 set_config各个选项的配置
    c# 截屏
    c#图像计算知识
    游戏代码
    Google Protocol Buffers (一个客户端与服务器协议生成工具)
    WinPcap抓取数据包
  • 原文地址:https://www.cnblogs.com/jianqiao123/p/12131023.html
Copyright © 2011-2022 走看看