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/n/129115.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
XO8JTXO8JT
上一篇 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

发表回复

登录后才能评论