直接选择排序举例
2010-05-31 20:52
281 查看
#include<iostream>
template<class T>
void Swap(T &x,T &y)
{
T temp;
temp=x;
x=y;
y=temp;
}
template <class T>
void SelectionSort(T A[],int n)
{
int smallIndex;
int i,j;
for(i=0;i<n-1;i++)
{
smallIndex=i;
for(j=i+1;j<n;j++)
if(A[j]<A[smallIndex])
smallIndex=j;
Swap(A[i],A[smallIndex]);
for(int k=0;k<n;k++)
cout<<A[k]<<" ";
cout<<endl;
}
}
int main()
{
int data1[]={1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20};
cout << "排序前的数据:" << endl;
for(int i=0;i<20;i++)
cout << data1[i] << " ";
cout << endl;
cout << "开始排序..." << endl;
SelectionSort(data1, 20);
cout << "排序后的数据:" << endl;
for(int i=0;i<20;i++)
cout << data1[i] << " ";
cout << endl;
}
template<class T>
void Swap(T &x,T &y)
{
T temp;
temp=x;
x=y;
y=temp;
}
template <class T>
void SelectionSort(T A[],int n)
{
int smallIndex;
int i,j;
for(i=0;i<n-1;i++)
{
smallIndex=i;
for(j=i+1;j<n;j++)
if(A[j]<A[smallIndex])
smallIndex=j;
Swap(A[i],A[smallIndex]);
for(int k=0;k<n;k++)
cout<<A[k]<<" ";
cout<<endl;
}
}
int main()
{
int data1[]={1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20};
cout << "排序前的数据:" << endl;
for(int i=0;i<20;i++)
cout << data1[i] << " ";
cout << endl;
cout << "开始排序..." << endl;
SelectionSort(data1, 20);
cout << "排序后的数据:" << endl;
for(int i=0;i<20;i++)
cout << data1[i] << " ";
cout << endl;
}
相关文章推荐
- 直接选择排序 (异或swap两个数存在陷阱)
- 【内部排序】六:直接选择排序(Straight Selection Sort)详解与代码
- 直接选择排序
- 第十六周--项目1-验证算法(5)直接选择排序
- 排序大全【各种排序】:直接插入,折半插入,冒泡,快排,简单选择,堆排序,归并排序
- 直接选择排序
- 数据结构与算法——冒泡排序,选择排序,直接插入排序
- 第十六周项目1-验证算法(5)直接选择排序
- 第16周项目1-验证算法(5)直接选择排序
- 第十五周项目一(5)直接选择排序
- 第十六周 项目1 验证算法-直接选择排序
- 排序算法(快速排序、直接插入排序、直接选择、冒泡排序)
- 第十六周 项目1 -(5)直接选择排序
- 选择排序--直接选择排序
- 内排序算法的java实现---直接选择排序
- 直接选择排序
- 直接选择排序----java实现
- 选择排序--简单选择排序与直接选择排序的区别
- 选择排序-直接选择排序
- 白话经典算法系列之四 直接选择排序及交换二个数据的正确实现