Java:不使用Arrays.sort()对整数数组进行排序

编程入门 行业动态 更新时间:2024-10-28 20:20:37
本文介绍了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()对整数数组进行排序

本文发布于:2023-11-30 14:41:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650364.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   数组   Java   Arrays   sort

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!