📄 selectsort.cpp
字号:
//SelectSort.cpp
//直接选择排序
# include <iostream.h>
# include <conio.h>
# define MAXSIZE 20
typedef int RedType;
typedef struct //定义 SqList
{ RedType r[MAXSIZE+1];
int length;
}SqList;
void SelectSort(SqList &L) //SelectSort() 子函数
{ int i,j,k,temp;
for(i=1;i<=L.length;++i) //选择第i个小的记录,并交换到位
{ k=i;
for(j=i+1;j<L.length;++j) //在L.r[i..length]中选择值最小的记录
if(L.r[j]<L.r[k])
k=j;
if(i!=k)
{ temp=L.r[i]; //与第i个记录交换
L.r[i]=L.r[k];
L.r[k]=temp;
}
}
}//SelectSort() end
void main() //main() 函数
{ int i;
SqList L;
cout<<endl<<endl<<"SelectSort.cpp";
cout<<endl<<"=============="<<endl;
cout<<endl<<"Please input the length of SqList (eg,5): ";
cin>>L.length;
for(i=1;i<=L.length;++i)
{ cout<<"Please input the "<<i<<"th element of SqList (eg,58): ";
cin>>L.r[i];
}
cout<<endl<<"The disordered : "; //未排序的线性表
for(i=1;i<=L.length;i++)
cout<<L.r[i]<<" ";
SelectSort(L); //调用 SelectSort()
cout<<endl<<"The sorted : "; //排序后的线性表
for(i=1;i<=L.length;i++)
cout<<L.r[i]<<" ";
cout<<endl<<endl<<"...OK!...";
getch();
} //main() end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -