Chinaunix首页 | 论坛 | 博客
  • 博客访问: 376696
  • 博文数量: 214
  • 博客积分: 770
  • 博客等级: 军士长
  • 技术积分: 1969
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-08 01:22
文章分类

全部博文(214)

文章存档

2013年(110)

2012年(104)

我的朋友

分类:

2012-11-07 12:24:56

原文地址:排序算法1 选择排序 作者:scutan

1. 选择排序算法
 

#include <iostream>
using namespace std;

void selectionSort(int arr[], int n)
{
    int smallIndex;
    int pass, j;
    int temp;

    for (pass = 0; pass < n-1; pass++)
    {
        smallIndex = pass;
        for (j = pass+1; j < n; j++)
            if (arr[j] < arr[smallIndex])
                smallIndex = j;
        if (smallIndex != pass)
        {
            temp = arr[pass];
            arr[pass] = arr[smallIndex];
            arr[smallIndex] = temp;
        }
    }
}

int main()
{
    int arr[15] = {10, 21, 2, 1, 3, 45, 2, 932, 32, 27, 86, 65, 576, 434, 76753};

    int i;
    
    cout << "Original array" << endl;
    for (i = 0; i < 15; i++)
        cout << arr[i] << " ";
    cout << endl << endl;

    
    selectionSort(arr, 15);

    cout << "Sorted array" << endl;
    for (i = 0; i < 15; i++)
        cout << arr[i] << " ";
    cout << endl;

    return 0;
}

阅读(268) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~