选择排序法
来源:互联网 发布:jquery get json数据 编辑:程序博客网 时间:2024/06/14 04:52
选择排序法:
/*
* 数组排序之选择排序:
* 从0索引开始,依次和后面元素比较,小的往前放,第一次完毕,最小值出现在了最小索引处
*/
public classArrayDemo {
public static void main(String[] args) {
// 定义一个数组
int[]arr = { 24, 69, 80, 57, 13};
System.out.println("排序前:");
printArray(arr);
/*
//第一次
intx = 0;
for(int y = x + 1; y < arr.length; y++) {
if(arr[y] < arr[x]) {
inttemp = arr[x];
arr[x]= arr[y];
arr[y]= temp;
}
}
System.out.println("第一次比较后:");
printArray(arr);
//第二次
x= 1;
for(int y = x + 1; y < arr.length; y++) {
if(arr[y] < arr[x]) {
inttemp = arr[x];
arr[x]= arr[y];
arr[y]= temp;
}
}
System.out.println("第二次比较后:");
printArray(arr);
//第三次
x= 2;
for(int y = x + 1; y < arr.length; y++) {
if(arr[y] < arr[x]) {
inttemp = arr[x];
arr[x]= arr[y];
arr[y]= temp;
}
}
System.out.println("第三次比较后:");
printArray(arr);
//第四次
x= 3;
for(int y = x + 1; y < arr.length; y++) {
if(arr[y] < arr[x]) {
inttemp = arr[x];
arr[x]= arr[y];
arr[y]= temp;
}
}
System.out.println("第四次比较后:");
printArray(arr);
*/
/*
//通过观察发现代码的重复度太高,所以用循环改进
for(intx=0; x<arr.length-1; x++){
for(inty=x+1; y<arr.length; y++){
if(arr[y]<arr[x]){
inttemp = arr[x];
arr[x]= arr[y];
arr[y] = temp;
}
}
}
System.out.println("排序后:");
printArray(arr);
*/
//用方法改进
selectSort(arr);
System.out.println("排序后:");
printArray(arr);
}
public static void selectSort(int[]arr){
for(intx=0; x<arr.length-1;x++){
for(inty=x+1;y<arr.length;y++){
if(arr[y]<arr[x]){
inttemp = arr[x];
arr[x] =arr[y];
arr[y] =temp;
}
}
}
}
// 遍历功能
public static void printArray(int[]arr) {
System.out.print("[");
for (intx = 0; x < arr.length; x++) {
if (x ==arr.length - 1) {
System.out.print(arr[x]);
}else{
System.out.print(arr[x] +", ");
}
}
System.out.println("]");
}
}
运行结果:
排序前:
[24,69, 80, 57, 13]
排序后:
[13,24, 57, 69, 80]
- 常用排序-选择法排序
- 常见排序-选择法排序
- 排序算法--选择排序法
- 按选择排序法排序。
- 排序模板 选择法排序
- 排序算法--选择排序法
- 排序之选择排序法
- 选择排序法法
- 排序:选择法
- 选择排序法
- 选择法排序
- 选择排序法
- 选择排序法证明
- 选择排序法
- 选择排序法
- 选择排序法
- 选择排序法
- C++ 选择排序法
- Hadoop学习四:hadoop分布式环境搭建
- 位运算之不用加减乘除做加法.
- java web中servlet、jsp、html 互相访问的路径问题。
- Xcode,OS X(SDK),iOS(SDK)版本对应
- java对象的强引用,软引用,弱引用和虚引用
- 选择排序法
- PPT快速制作缩略图
- awk命令详解
- 二分查找法
- Ubuntu的一些使用小技巧【不断更新中】
- ubuntu安装pip+python27+ipython+scrapy+zlib+libssl-dev-及遇到的各种问题解决
- De Bruijn序列的matlab暴力生成
- leetcode: 237. Delete Node in a Linked List
- js第十一节-字符串方法