选择排序的一段蛋疼 code

胖子吴 2016-07-15 10:11:09
/*数组排序
* 1. 选择排序 selectSort
* 2. 冒泡排序 bubbleSort
* 3. 插入排序 insertSort
*/
public class Demo6_12 {

/**
* @param args
*/
public static void main(String[] args) {

System.out.print("Please Enter the numbers: ");
java.util.Scanner input = new java.util.Scanner(System.in);
double[] numbers = new double[5];
//录入测试化数据
for(int i=0; i<numbers.length; i++){
numbers[i] = input.nextDouble();
}
//selectSort 后 输出
System.out.println("The selectSort result: " );
numbers = selectSort(numbers);
for(double num :numbers )
System.out.print(num+" ");
System.out.println();
//bubbleSort 后 输出
//insertSort 后 输出

}
//selectSort 数组
public static double[] selectSort (double[] x){
// double min = 0;
int index = 0;
double temp = 0;

for(int i=0; i<x.length; i++){
double min = x[i];
for(int j=i+1; j<x.length; j++)
//记录最小值
if (x[j] < min){
index = j;
min = x[j];
}
//交换数据
temp = x[i];
x[i] = min;
x[index] = temp;
}
return x;
}// close selectSort
}
...全文
93 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
小灰狼 2016-07-15
  • 打赏
  • 举报
回复
楼主给的分那么少 并且也不说到底有什么问题 楼主,你以为别人是神仙?
ZOJO13531373407 2016-07-15
  • 打赏
  • 举报
回复
public class demo6 {

	public static void main(String[] args) {

		System.out.print("Please Enter the numbers: ");
		java.util.Scanner input = new java.util.Scanner(System.in);
		double[] numbers = new double[5];
		// 录入测试化数据
		for (int i = 0; i < numbers.length; i++) {
			numbers[i] = input.nextDouble();
		}
		// selectSort 后 输出
		System.out.println("The selectSort result: ");
		numbers = selectSort(numbers);
		for (double num : numbers) {
			System.out.print(num + " ");
			System.out.println();
		}
	}

	// selectSort 数组
	public static double[] selectSort(double[] x) {
		// double min = 0;
		// int index = 0;
		// double temp = 0;
		//
		// for (int i = 0; i < x.length; i++) {
		// double min = x[i];
		// for (int j = i + 1; j < x.length; j++) {
		// // 记录最小值
		// if (x[j] < min) {
		// index = j;
		// min = x[j];
		// }
		// // 交换数据
		// temp = x[i];
		// x[i] = min;
		// x[index] = temp;
		// }
		//
		// }
		// return x;
		// }

		for (int i = 0; i < x.length; i++) {
			double temp = 0;
			for (int j = i + 1; j < x.length; j++) {
				if (x[i] > x[j]) {
					temp = x[i];
					x[i] = x[j];
					x[j] = temp;
				}
			}
		}
		return x;
	}
}

62,626

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧