Java 如何在没有字符串或数组的情况下按升序对整数进行排序?
我试图对任意长度的整数的数字进行升序排序,而不使用字符串、数组或递归 例如:Java 如何在没有字符串或数组的情况下按升序对整数进行排序?,java,sorting,modulo,Java,Sorting,Modulo,我试图对任意长度的整数的数字进行升序排序,而不使用字符串、数组或递归 例如: Input: 451467 Output: 144567 我已经知道了如何用模除法得到整数的每个数字: int number = 4214; while (number > 0) { IO.println(number % 10); number = number / 10; } 但是我不知道如何在没有数组的情况下对数字进行排序 不要担心IO类;这是我们教授给我们的定制课程。如何在不使用数组
Input: 451467
Output: 144567
我已经知道了如何用模除法得到整数的每个数字:
int number = 4214;
while (number > 0) {
IO.println(number % 10);
number = number / 10;
}
但是我不知道如何在没有数组的情况下对数字进行排序
不要担心
IO
类;这是我们教授给我们的定制课程。如何在不使用数组、字符串或排序api的情况下对数字进行排序?好的,您可以通过以下简单步骤对数字进行排序(如果太多而无法读取,请参阅下面的调试输出,了解如何进行排序):
Subchunk是45156
Subchunk是4515
Subchunk是451
子块为45
子块为4
451567中的小数字为1
商店是:1
从451567中删除1个
减少的数字是:76554
最后一位数字是:76554的第4位
Subchunk是7655
Subchunk是765
子chunk是76
子块为7
76554中的小数字是4
商店是:14
从76554中删除4
减少的数量为:5567
数字5567的最后一位是:7
Subchunk是556
子chunk是55
子块为5
5567中的小数字为5
商店是:145
从5567中删除5个
找到重复的最小数字5。存储为:145
存储区中添加了重复的最小数字5。更新的存储区为:1455
减少的数量为:76
最后一位数字是:76的第6位
子块为7
76中的小数字是6
商店地址:14556
从76中删除6个
减少的数量为:7
数字7的最后一位是:7
7中的小数字是7
商店地址:145567
从7中删除7
减少的数量为:0
451567的升序为145567 示例代码如下所示:
//stores our sorted number
static int store = 0;
public static void main(String []args){
int number = 451567;
int original = number;
while (number > 0) {
//digit by digit - get last most digit
int digit = number % 10;
System.out.println("Last digit is : " + digit + " of number : " + number);
//get the whole number minus the last most digit
int temp = number / 10;
//loop through number minus the last digit to compare
while(temp > 0) {
System.out.println("Subchunk is " + temp);
//get the last digit of this sub-number
int t = temp % 10;
//compare and find the lowest
//for sorting descending change condition to t > digit
if(t < digit)
digit = t;
//divide the number and keep loop until the smallest is found
temp = temp / 10;
}
System.out.println("Smalled digit in " + number + " is " + digit);
//add the smallest digit to store
store = (store * 10) + digit;
System.out.println("Store is : " + store);
//we found the smallest digit, we will remove that from number and find the
//next smallest digit and keep doing this until we find all the smallest
//digit in sub chunks of number, and keep adding the smallest digits to
//store
number = getReducedNumber(number, digit);
}
System.out.println("Ascending order of " + original + " is " + store);
}
/*
* A simple method that constructs a new number, excluding the digit that was found
* to b e smallest and added to the store. The new number gets returned so that
* smallest digit in the returned new number be found.
*/
public static int getReducedNumber(int number, int digit) {
System.out.println("Remove " + digit + " from " + number);
int newNumber = 0;
//flag to make sure we do not exclude repeated digits, in case there is 44
boolean repeatFlag = false;
while(number > 0) {
int t = number % 10;
//assume in loop one we found 1 as smallest, then we will not add one to the new number at all
if(t != digit) {
newNumber = (newNumber * 10) + t;
} else if(t == digit) {
if(repeatFlag) {
System.out.println("Repeated min digit " + t + "found. Store is : " + store);
store = (store * 10) + t;
System.out.println("Repeated min digit " + t + "added to store. Updated store is : " + store);
//we found another value that is equal to digit, add it straight to store, it is
//guaranteed to be minimum
} else {
//skip the digit because its added to the store, in main method, set flag so
// if there is repeated digit then this method add them directly to store
repeatFlag = true;
}
}
number /= 10;
}
System.out.println("Reduced number is : " + newNumber);
return newNumber;
}
}
//存储我们的排序编号
静态整数存储=0;
公共静态void main(字符串[]args){
整数=451567;
int原始=数字;
而(数量>0){
//逐位-获取最后一位
整数位数=数字%10;
System.out.println(“最后一位是:“+数字+”,数字:“+数字”);
//获取整数减去最后一个最高位
内部温度=数字/10;
//循环数字减去要比较的最后一个数字
而(温度>0){
系统输出println(“子模块为”+temp);
//获取此子编号的最后一位数字
int t=温度%10;
//比较并找出最低的
//对于排序,将条件更改为t>位
if(t<数字)
数字=t;
//将数字除以并保持循环,直到找到最小值
温度=温度/10;
}
System.out.println(“在“+number+”中的小数位是“+digit”);
//添加要存储的最小数字
存储=(存储*10)+数字;
System.out.println(“存储为:“+Store”);
//我们找到了最小的数字,我们将从数字中删除它并找到
//下一个最小的数字,继续这样做,直到我们找到所有最小的数字
//在数字的子块中输入数字,并不断将最小的数字添加到
//贮藏
数字=getReducedNumber(数字);
}
System.out.println(“原始+的升序为+存储”);
}
/*
*构造新数字的简单方法,不包括找到的数字
*返回最小值并添加到存储中。返回新的数字以便
*无法找到返回的新编号中的最小数字。
*/
公共静态整型整型getReducedNumber(整型数字,整型数字){
System.out.println(“从“+数字”中删除“+数字+”);
int newNumber=0;
//标记以确保我们不排除重复的数字,以防有44位
布尔repeatFlag=false;
而(数量>0){
int t=数字%10;
//假设在循环1中,我们发现1是最小的,那么我们根本不会将1添加到新的数字中
如果(t!=位数){
newNumber=(newNumber*10)+t;
}else if(t==位){
如果(重复标志){
System.out.println(“找到重复的最小数字”+t+”。存储为:“+Store”);
存储=(存储*10)+t;
System.out.println(“重复的最小数字”+t+”添加到存储。更新的s
int number = 4214173;
int sorted = 0;
int digits = 10;
int sortedDigits = 1;
boolean first = true;
while (number > 0) {
int digit = number % 10;
if (!first) {
int tmp = sorted;
int toDivide = 1;
for (int i = 0; i < sortedDigits; i++) {
int tmpDigit = tmp % 10;
if (digit >= tmpDigit) {
sorted = sorted/toDivide*toDivide*10 + digit*toDivide + sorted % toDivide;
break;
} else if (i == sortedDigits-1) {
sorted = digit * digits + sorted;
}
tmp /= 10;
toDivide *= 10;
}
digits *= 10;
sortedDigits += 1;
} else {
sorted = digit;
}
first = false;
number = number / 10;
}
System.out.println(sorted);
public static void sortDigits(int x) {
Map<Integer, Integer> digitCounts = new HashMap<>();
while (x > 0) {
int digit = x % 10;
Integer currentCount = digitCounts.get(digit);
if (currentCount == null) {
currentCount = 0;
}
digitCounts.put(x % 10, currentCount + 1);
x = x / 10;
}
for (int i = 0; i < 10; i++) {
Integer count = digitCounts.get(i);
if (count == null) {
continue;
}
for (int j = 0; j < digitCounts.get(i); j++) {
System.out.print(i);
}
}
}
int number = 4214;
List<Integer> numbers = new LinkedList<>(); // a LinkedList is not backed by an array
for (int i = number; i > 0; i /= 10)
numbers.add(i % 10);
numbers.stream().sorted().forEach(System.out::println); // or for you forEach(IO::println)
int ascending(int a)
{
int b = a;
int i = 1;
int length = (int)Math.log10(a) + 1; // getting the number of digits
for (int j = 0; j < length - 1; j++)
{
b = a;
i = 1;
while (b > 9)
{
int s = b % 10; // getting the last digit
int r = (b % 100) / 10; // getting the second last digit
if (s < r)
{
a = a + s * i * 10 - s * i - r * i * 10 + r * i; // switching the digits
}
b = a;
i = i * 10;
b = b / i; // removing the last digit from the number
}
}
return a;
}
public class SortDigits
{
public static void main(String[] args)
{
sortDigits(3413657);
}
public static void sortDigits(int num)
{
System.out.println("Number : " + num);
String number = Integer.toString(num);
int len = number.length(); // get length of the number
int[] digits = new int[len];
int i = 0;
while (num != 0)
{
int digit = num % 10;
digits[i++] = digit; // get all the digits
num = num / 10;
}
System.out.println("Digit before sorting: ");
for (int j : digits)
{
System.out.print(j + ",");
}
sort(digits);
System.out.println("\nDigit After sorting: ");
for (int j : digits)
{
System.out.print(j + ",");
}
}
//simple bubble sort
public static void sort(int[] arr)
{
for (int i = 0; i < arr.length - 1; i++)
for (int j = i + 1; j < arr.length; j++)
{
if (arr[i] > arr[j])
{
int tmp = arr[j];
arr[j] = arr[i];
arr[i] = tmp;
}
}
}
}
int number = 451467;
// the possible elements are known, 0 to 9
for (int i = 0; i <= 9; i++) {
int tempNumber = number;
while (tempNumber > 0) {
int digit = tempNumber % 10;
if (digit == i) {
IO.print(digit);
}
tempNumber = tempNumber / 10;
}
}
class SortDigits {
public static void main(String[] args) {
int inp=57437821;
int len=Integer.toString(inp).length();
int[] arr=new int[len];
for(int i=0;i<len;i++)
{
arr[i]=inp%10;
inp=inp/10;
}
Arrays.sort(arr);
int num=0;
for(int i=0;i<len;i++)
{
num=(num*10)+arr[i];
}
System.out.println(num);
}
}
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
int length = 0;
long tem = 1;
while (tem <= n) {
length++;
tem *= 10;
}
int last=0;
int [] a=new int[length];
int i=0;
StringBuffer ans=new StringBuffer(4);
while(n!=0){
last=n%10;
a[i]=last;
n=n/10;
i++;
}
int l=a.length;
for(int j=0;j<l;j++){
for(int k=j;k<l;k++){
if(a[k]<a[j]){
int temp=a[k];
a[k]=a[j];
a[j]=temp;
}
}
}
for (int j :a) {
ans= ans.append(j);
}
int add=Integer.parseInt(ans.toString());
System.out.println(add);
n=762941 ------->integer
149267 ------->integer