Java:不使用Arrays.sort()对整数数组进行排序 [英] Java : Sort integer array without using Arrays.sort()

查看:213
本文介绍了Java:不使用Arrays.sort()对整数数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我们Java类中的一个练习中的指令。在其他任何事情之前,我想说我'做我的功课'而且我不只是懒得让Stack Overflow上的某个人为我回答这个问题。这个特定项目一直是我在所有其他练习中的问题,因为我一直在努力为此找到完美算法。

This is the instruction in one of the exercises in our Java class. Before anything else, I would like to say that I 'do my homework' and I'm not just being lazy asking someone on Stack Overflow to answer this for me. This specific item has been my problem out of all the other exercises because I've been struggling to find the 'perfect algorithm' for this.


编写JAVA程序,输入10个整数值,并以升序或降序显示。注意:不允许使用Arrays.sort()。

Write JAVA program that will input 10 integer values and display either in ascending or descending order. Note: Arrays.sort() is not allowed.

这是我提出的代码,它有效,但有一个明显的缺陷。如果我输入相同的值两次或更多,例如:

This is the code I have come up with, it works but it has one obvious flaw. If I enter the same value twice or more, for example:


5,5,5,4,6,7,3,2 ,8,10

5, 5, 5, 4, 6, 7, 3, 2, 8, 10

输入的三个5中只有一个将被计算并包含在输出中。我得到的输出(升序)是:

Only one of the three 5s entered would be counted and included in the output. The output I get (for the ascending order) is:


2 3 4 5 0 0 6 7 8 10。

2 3 4 5 0 0 6 7 8 10.



import java.util.Scanner;

public class Exer3AscDesc
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        int tenNums[]=new int[10], orderedNums[]=new int[10];
        int greater;
        String choice;

        //get input
        System.out.println("Enter 10 integers : ");
        for (int i=0;i<tenNums.length;i++)
        {
            System.out.print(i+1+"=> ");
            tenNums[i] = scan.nextInt();
        }
        System.out.println();

        //imperfect number ordering algorithm
        for(int indexL=0;indexL<tenNums.length;indexL++)
        {
            greater=0;
            for(int indexR=0;indexR<tenNums.length;indexR++)
            {
                if(tenNums[indexL]>tenNums[indexR])
                {
                    greater++;
                }
            }
            orderedNums[greater]=tenNums[indexL];
        }

        //ask if ascending or descending
        System.out.print("Display order :\nA - Ascending\nD - Descending\nEnter your choice : ");
        choice = scan.next();

        //output the numbers based on choice
        if(choice.equalsIgnoreCase("a"))
        {
            for(greater=0;greater<orderedNums.length;greater++)
            {
                System.out.print(orderedNums[greater]+" ");
            }
        }
        else if(choice.equalsIgnoreCase("d"))
        {
            for(greater=9;greater>-1;greater--)
            {
                System.out.print(orderedNums[greater]+" ");
            }
        }
    }
}


推荐答案

您可以在互联网上找到许多不同的排序算法,但如果您想修复自己的解决方案,可以在代码中进行以下更改:

You can find so many different sorting algorithms in internet, but if you want to fix your own solution you can do following changes in your code:

而不是:

 orderedNums[greater]=tenNums[indexL];

你需要这样做:

while (orderedNums[greater] == tenNums[indexL]) {
     greater++;
}
orderedNums[greater] = tenNums[indexL];

此代码基本上检查该特定索引是否被类似的数字占用,然后它会尝试查找下一个免费索引。

This code basically checks if that particular index is occupied by a similar number, then it will try to find next free index.

注意:由于排序数组元素中的默认值为0,因此您需要确保列表中没有0。否则你需要
来启动你的排序数组,你必须确定
不在你的列表中,例如: Integer.MAX_VALUE

Note: Since the default value in your sorted array elements is 0, you need to make sure 0 is not in your list. otherwise you need to initiate your sorted array with an especial number that you sure is not in your list e.g: Integer.MAX_VALUE

这篇关于Java:不使用Arrays.sort()对整数数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆