import java.util.Scanner;
class MySort {
public static void main(String[] args) {
int[] arr = new int[10];
boolean ret = getNumFromStdin(arr);
if (ret) {
//putMaxAtFirstIndex(arr);
//putMaxAtSecondIndex(arr);
//selectSort(arr);
bubbleSort(arr);
printArray(arr);
}
}
/*
冒泡排序
*/
public static boolean bubbleSort(int[] arr) {
//参数合法性判断
if (arr == null || arr.length == 0) {
System.out.println("Input Param is invalid!");
return false; //表示函数运行失败
}
//外层for循环控制当前的比较轮次
for (int i = 0; i < arr.length - 1; i++) {
//内层循环控制当前轮次要进行多少次两两比较
for (int j = 0; j < arr.length - 1 - i; j++) {
//两两比较,满足条件,交换位置
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
} //if
} // for j
} // for i
return true;
}
/*
选择排序 selectSort
*/
public static boolean selectSort(int[] arr) {
//参数合法性判断
if (arr == null || arr.length == 0) {
System.out.println("Input Param is invalid!");
return false; //表示函数运行失败
}
//循环的问题
//核心代码需要循环9次才能确定数据排序成功
for (int i = 0; i < arr.length - 1; i++) {
//核心代码
int index = i;
//找出数组中剩余数据的最大值,放到对应的位置
for (int j = i + 1; j < arr.length; j++) {
if (arr[index] < arr[j]) {
index = j;
}
}
//i就是每一次要对应的位置
if (index != i) {
int temp = arr[index];
arr[index] = arr[i];
arr[i] = temp;
}
}
return true;
}
/* ------------------------------------------------------------------- */
/**
需求:找出数组位处理数据中最大的元素,和下标为1的元素互换位置
参数: int[] arr 表示要传入的数组【首地址】
返回值: boolean 返回true 表示函数运行成功,返回false表示函数运行失败
1、传入的数组首地址是null
2、传入的数组长度为0
*/
public static boolean putMaxAtSecondIndex(int[] arr) {
//参数合法性判断
if (arr == null || arr.length == 0) {
System.out.println("Input Param is invalid!");
return false; //表示函数运行失败
}
int index = 1;
for (int i = 2; i < arr.length; i++) {
if (arr[index] < arr[i]) {
index = i;
}
}
if (index != 1) {
int temp = arr[index];
arr[index] = arr[1];
arr[1] = temp;
}
return true;
}
/**
需求:找出数组中最大的元素,和下标为0的元素互换位置
参数: int[] arr 表示要传入的数组【首地址】
返回值: boolean 返回true 表示函数运行成功,返回false表示函数运行失败
1、传入的数组首地址是null
2、传入的数组长度为0
*/
public static boolean putMaxAtFirstIndex(int[] arr) {
//参数合法性判断
if (arr == null || arr.length == 0) {
System.out.println("Input Param is invalid!");
return false; //表示函数运行失败
}
//获取最大值的下标 利用for循环找出数组中最大值下标的操作
int index = 0; //用index保存数组中的最大值下标
for (int i = 1; i < arr.length; i++) {
if (arr[index] < arr[i]) {
index = i;
}
} //这个循环结束,就可以找出数组中最大值的下标
//和下标为0的元素互换位置
if (index != 0) {
int temp = arr[index];
arr[index] = arr[0];
arr[0] = temp;
}
return true;
}
/**
功能:从键盘上获取十个int类型的数据放入数组
参数: int[] array 表示要传入的数组【首地址】
返回值: boolean 返回true 表示函数运行成功,返回false表示函数运行失败
1、传入的数组首地址是null
2、传入的数组长度为0
*/
public static boolean getNumFromStdin(int[] array) {
//参数合法性判断
if (array == null || array.length == 0) {
System.out.println("Input Param is invalid!");
return false; //表示函数运行失败
}
Scanner sc = new Scanner(System.in);
System.out.println("请输入" + array.length + "个数:");
for (int i = 0; i < array.length; i++) {
array[i] = sc.nextInt();
}
return true;
}
/**
功能:展示指定的int类型的数组
参数: int[] array 表示要传入的数组【首地址】
返回值: void
如果函数没有打印数组,存在两种情况:
1、传入的数组首地址是null
2、传入的数组长度为0
*/
public static void printArray(int[] array) {
//参数合法性判断
if (array == null || array.length == 0) {
System.out.println("Input Param is invalid!");
return; //传入参数不合法,结束函数运行
}
//利用for循环打印数组
for (int i = 0; i < array.length; i++) {
System.out.println("array[" + i + "] = " + array[i]);
}
//这里可以不用写return 因为函数么有返回值
}
}