首页 > 编程知识 正文

java数组排序练习题(java数组的排序方法)

时间:2023-12-12 20:00:05 阅读:315047 作者:RGQJ

本文目录一览:

求解一道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+" ");

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。