数据结构学习笔记之--简单选择排序

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

#include <iostream>

using namespace std;

//入口参数:数组, 数组元素个数

template<typename T>

void selectionSort(T arr[], int size)

{

int j;

T temp;

int minIndex; //每次查找到的最小元素下标

for (int i=0; i<size-1; i++)

{

minIndex = i;

//找到最小的元素,用minIndex保存下标

for (j=i+1; j<size; j++)

{

if (arr[minIndex] > arr[j])

{

minIndex = j;

}

}

if (i != minIndex)

{

temp = arr[i];

arr[i] = arr[minIndex];

arr[minIndex] = temp;

}

}

}

int main()

{

int array[] = {10, 20, 50, 5, 654, 24, 128, 25};

cout << "Before sort: ";

for (int i=0; i<8; i++)

{

cout << array[i] << " ";

}

selectionSort(array, 8);

cout << endl << "After sort: ";

for (i=0; i<8; i++)

{

cout << array[i] << " ";

}

cout << endl;

return 0;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航