如何在不使用数组的情况下对数字进行排序?

我如何找到可以使用给定数字的数字(例如)形成的最大值和最小数,而根本不使用任何数组?485735


我正在研究气泡排序算法(使用数组),我试图弄清楚如何在没有数组的情况下编写算法,但我的问题是计算每个数字的索引


我唯一想到的是一个算法来计算输入中的位数(朋友帮我解决了这个问题),但到目前为止,我试图弄清楚这件事4天,这是一个与我的家庭作业成绩有关的问题。


规则是最小的数字不能以零开头,例如:


Input: 3134059 

The largest number is: 9543310

The smallest number is: 1033459


胡子哥哥
浏览 93回答 2
2回答

杨__羊羊

参考资料:请参阅以下逻辑演示。假装数字是一个数字数组,例如命名,并假装索引是最右边的数字。d[]0气泡排序会将更高的值移动到更高的索引,因此,如果我们保持该逻辑,则排序将产生所需的 ,例如 成为。dlargestNumber13579249754321假设你有一个计算10n的方法,然后你可以在任何索引处得到数字:pow10(n)d[i] = number / pow10(i) % 10例:         6 4 2 0  index         ↓ ↓ ↓ ↓number = 1357924d[4] = 1357924 / pow10(4) % 10     = 1357924 / 10000 % 10     = 135 % 10     =   5在气泡排序中,如果索引较低的元素较大,则可以交换相邻的元素,因此首先需要两个值。假设我们这样做是为了:i = 3         6 4 2 0  index         ↓ ↓ ↓ ↓number = 1357924i = 3a = d[i] = d[3] = 7b = d[i+1] = d[4] = 5因为我们需要交换值。我们可以按如下方式执行此操作:a > b 1357924-   7000   Clear digit at i=3-  50000   Clear digit at i=4=1300924   Value with digits cleared+  70000   Set digit at i=4+   5000   Set digit at i=3=1375924   Value with digits at index 3 and 4 swapped公式为:number = number - a * pow10(i) - b * pow10(i+1)                + a * pow10(i+1) + b * pow10(i)可以重构为:number += ((a - b) * 10 - (a - b)) * pow10(i)现在您已经知道如何获取“数组元素值”(又名 ),以及如何使用上述公式“交换数组元素”,然后将其写入正常的气泡排序算法中,以便您可以:d[i]largestNumber = sortDigits(number)现在,您已经计算出最大值。要计算最小值,您只需反转数字,但在执行此操作之前,您需要确保:d[0] != 0n = largestNumber, i = 0while (n % 10 == 0) { // locate least non-zero digit    n /= 10    i++}if (i != 0) {    // clear least digit and add at index 0    n = n / 10 * pow10(i + 1) + n % 10}例:n = 97500After loop: n = 975, i = 2n / 10 = 97            * pow10(i + 1) = 97000                                   + n % 10 = 97005现在,您可以计算所需的其他值:smallestNumber = reverse(n)例如,请参阅Java在不使用数组的情况下反转int值,了解如何执行此操作。

LEATH

public static void main(String[] args) {&nbsp; &nbsp; StringBuilder s = new StringBuilder("4857035");&nbsp; &nbsp; char aux;&nbsp; &nbsp; for (int i = 0; i < s.length() - 1; i++) {&nbsp; &nbsp; &nbsp; &nbsp; for (int j = i + 1; j < s.length(); j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (s.charAt(i) > (s.charAt(j))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aux = s.charAt(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.setCharAt(i, s.charAt(j));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.setCharAt(j, aux);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; //output 0345578&nbsp; &nbsp; while (s.charAt(0) == '0') {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i < s.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (s.charAt(i) != '0') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aux = s.charAt(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.setCharAt(0, s.charAt(i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.setCharAt(i, aux);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; //output 3045578}这是针对最小的数字,对于最大的数字,请更改 if 语句 ( ) 上的符号并删除 while 语句。if (s.charAt(i) < (s.charAt(j))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java