本文目錄一覽:
- 1、求解一道Java數組排序方面的題目
- 2、Java題,有數組 int[] arr={5.2.3.4.9.8.7.1} 請編寫一段程序為該數組進行排序,
- 3、java題:請使用任意一例排序演算法,對int[] intArr={5,9,1,4,1,2,6,3,8,0,7}進行排序
求解一道Java數組排序方面的題目
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int[] array = new int[1000];
for (int i = 0; i 1000; i++) {
array[i] = i;
}
boolean loopFlag = true;
while(loopFlag) {
System.out.println(“請輸入字元:”);
Scanner sc = new Scanner(System.in);
String result = sc.nextLine();
if (“A”.equalsIgnoreCase(result)) {
array = sort(array, result);
print(array);
} else if (“B”.equalsIgnoreCase(result)) {
array = sort(array, result);
print(array);
} else if (“C”.equalsIgnoreCase(result)) {
array = sort(array, result);
print(array);
} else {
System.out.println(“你輸入的不合法,請重新輸入…”);
}
System.out.println(“按Y/y繼續,按N/n退出?”);
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
if (“N”.equalsIgnoreCase(input)) {
loopFlag = false;
System.out.println(“退出成功!”);
}
}
}
/**
* @param a
* @param b
* @param type 比較類型
* @return 若ab 返回true
*/
public static boolean compare(int a, int b, String type) {
int hundredsDigitA = a / 100;
int tenDigitA = a % 100 / 10;
int singleDigitA = a % 100 % 10;
int hundredsDigitB = b / 100;
int tenDigitB = b % 100 / 10;
int singleDigitB = b % 100 % 10;
if(“B”.equalsIgnoreCase(type)) {
// 十位個位百位
if (tenDigitA tenDigitB) {
return true;
} else if(tenDigitA tenDigitB){
return false;
} else {
if (singleDigitA singleDigitB) {
return true;
} else if(singleDigitA singleDigitB){
return false;
} else {
if (hundredsDigitA hundredsDigitB) {
return true;
} else if(hundredsDigitA hundredsDigitB){
return false;
} else {
// a,b相等,返回true/false都可以,基本上不會走到這一步
return true;
}
}
}
} else if (“C”.equalsIgnoreCase(type)) {
// 個位百位十位
if (singleDigitA singleDigitB) {
return true;
} else if(singleDigitA singleDigitB){
return false;
} else {
if (hundredsDigitA hundredsDigitB) {
return true;
} else if(hundredsDigitA hundredsDigitB){
return false;
} else {
if (tenDigitA tenDigitB) {
return true;
} else if(tenDigitA tenDigitB){
return false;
} else {
// 相等,返回true/false都可以,基本上不會走到這一步
return true;
}
}
}
} else {
// 百位十位個位
if (hundredsDigitA hundredsDigitB) {
return true;
} else if(hundredsDigitA hundredsDigitB){
return false;
} else {
if (tenDigitA tenDigitB) {
return true;
} else if(tenDigitA tenDigitB){
return false;
} else {
if (singleDigitA singleDigitB) {
return true;
} else if(singleDigitA singleDigitB){
return false;
} else {
// 相等,返回true/false都可以,基本上不會走到這一步
return true;
}
}
}
}
}
/**
*
* @param array 排序數組
* @param type 排序類型,傳入字元串”A”、”B”、”C”
* @return
*/
public static int[] sort(int[] array, String type) {
int length = array.length;
for (int i = 0; i length; i++) {
for (int j = 0; j length – 1; j++) {
if (!compare(array[j], array[j+1], type)) {
// 如果前面的數小於後面的數,就換位
int temp;
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
return array;
}
public static void print(int[] array) {
for (int number : array) {
System.out.print(number + “”);
}
System.out.println();
}
}
Java題,有數組 int[] arr={5.2.3.4.9.8.7.1} 請編寫一段程序為該數組進行排序,
public static void main(String[] args) throws Exception {
int[] arr={5,2,3,4,9,8,7,1};
insertSort(arr);
}
/**
* @param array插入排序演算法待排數組
*/
static void insertSort(int …array){
int i,j,temp;
for(i=1;iarray.length;i++){
if(array[i]array[i-1]){
temp=array[i];
for(j=i-1;j!=-1array[j]temp;j–){
array[j+1]=array[j];
}
array[j+1]=temp;
}
}
for(int item:array) out.print(item+” “);
}
java題:請使用任意一例排序演算法,對int[] intArr={5,9,1,4,1,2,6,3,8,0,7}進行排序
import static java.lang.System.*;
public class Program {
public static void main(String[] args) {
int[] intArr={5,9,1,4,1,2,6,3,8,0,7};
insertSort(intArr);
}
/**
* @param array插入排序演算法待排數組
*/
static void insertSort(int …array){
int i,j,temp;
for(i=1;iarray.length;i++){
if(array[i]array[i-1]){
temp=array[i];
for(j=i-1;j!=-1array[j]temp;j–){
array[j+1]=array[j];// 元素向後挪動
}
array[j+1]=temp;
}
}
for(int item:array) out.print(item+” “);
}
}
原創文章,作者:XO8JT,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/129115.html