选择排序法
2016-04-08 23:36
337 查看
选择排序法:
/*
* 数组排序之选择排序:
* 从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(int
x=0; x<arr.length-1;
x++){
for(int
y=x+1;y<arr.length;
y++){
if(arr[y]
<arr[x]){
int
temp = arr[x];
arr[x] =
arr[y];
arr[y] =
temp;
}
}
}
}
// 遍历功能
public
static void printArray(int[]
arr) {
System.out.print("[");
for (int
x = 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]
/*
* 数组排序之选择排序:
* 从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(int
x=0; x<arr.length-1;
x++){
for(int
y=x+1;y<arr.length;
y++){
if(arr[y]
<arr[x]){
int
temp = arr[x];
arr[x] =
arr[y];
arr[y] =
temp;
}
}
}
}
// 遍历功能
public
static void printArray(int[]
arr) {
System.out.print("[");
for (int
x = 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]
相关文章推荐
- mybatis关系映射之一对多和多对一
- 汉堡包评价
- 非关系型数据库中的「关系」实现
- 位运算-不用加减乘除做加法。
- 2016年4月7号渗透学习总结
- <SLAM for Dummies>阅读笔记
- 冒泡排序法
- 文件编程
- Hadoop学习四:hadoop分布式环境搭建
- 笔记练习:《Javascript入门经典(第5版)》page115_9.9_Practice
- Apache CAS部署在tomcat上实现单点登录
- 百思不得姐之新帖(四)
- [android] 手机卫士自定义对话框布局
- 错误: 找不到或无法加载主类 org.codehaus.plexus.classworlds.launcher.Launcher 问题排查
- Android学习笔记(Google官方教程)(二)
- 操作型模式之State(状态)模式
- 根据先序和中序遍历重建二叉树java实现
- 编程测试题:有17个人围成一圈(编号0~16),问此人原来的位置是多少号?
- 数据库sqlite3
- 0408 汉堡包