选择排序--(简单选择排序和堆排序)

简单选择排序的基本思想:在要排序的一组数中,选出最小的一个数与第一个元素交换位置,然后选出倒数第二小的数与第二个元素交换,直到倒数第二个元素和最后一个元素比较为止。

选择排序不稳定,时间复杂度为:O(n^2)

 

// 简单选择排序.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
using namespace std;

void insert(int data[],int key)
{
    
    for (int i = 0; i < key-1;i++)
    {
        int min = i;
        int temp = 0;
        for (int j = i+1; j < key; j++)
        {
            if (data[min] > data[j])
            {
                temp =data[j];
                data[j] = data[min];
                data[min] = temp;
            }
        }
        
        
        
    }
    
}
int _tmain(int argc, _TCHAR* argv[])
{
    int array[10]={4,10,9,8,7,6,5,15,3,2};
    for (int i = 0; i < 10; i++)
    {
        cout<<array[i]<<"  ";
    }
    cout<<endl;
    insert(array,10);
    for (int i = 0; i < 10; i++)
    {
        cout<<array[i]<<"  ";
    }
    while (true)
    {

    }
    return 0;
}

 

 

 

posted @ 2012-08-23 18:38  subyafei  阅读(130)  评论(0编辑  收藏  举报