java數組排序練習題(java數組的排序方法)

本文目錄一覽:

求解一道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-hk/n/129115.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
XO8JT的頭像XO8JT
上一篇 2024-10-03 23:26
下一篇 2024-10-03 23:26

相關推薦

  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • Java Bean加載過程

    Java Bean加載過程涉及到類加載器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean加載的過程。 一、類加載器 類加載器是Java虛擬機…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • 解決.net 6.0運行閃退的方法

    如果你正在使用.net 6.0開發應用程序,可能會遇到程序閃退的情況。這篇文章將從多個方面為你解決這個問題。 一、代碼問題 代碼問題是導致.net 6.0程序閃退的主要原因之一。首…

    編程 2025-04-29
  • ArcGIS更改標註位置為中心的方法

    本篇文章將從多個方面詳細闡述如何在ArcGIS中更改標註位置為中心。讓我們一步步來看。 一、禁止標註智能調整 在ArcMap中設置標註智能調整可以自動將標註位置調整到最佳顯示位置。…

    編程 2025-04-29
  • Python創建分配內存的方法

    在python中,我們常常需要創建並分配內存來存儲數據。不同的類型和數據結構可能需要不同的方法來分配內存。本文將從多個方面介紹Python創建分配內存的方法,包括列表、元組、字典、…

    編程 2025-04-29
  • Python中init方法的作用及使用方法

    Python中的init方法是一個類的構造函數,在創建對象時被調用。在本篇文章中,我們將從多個方面詳細討論init方法的作用,使用方法以及注意點。 一、定義init方法 在Pyth…

    編程 2025-04-29
  • Python導入數組

    本文將為您詳細闡述Python導入數組的方法、優勢、適用場景等方面,並附上代碼示例。 一、numpy庫的使用 numpy是Python中一個強大的數學庫,其中提供了非常豐富的數學函…

    編程 2025-04-29

發表回復

登錄後才能評論