How to Sort Integer Digits in Ascending Order Without Strings or Arrays

How to sort Integer digits in ascending order without Strings or Arrays?

There's actually a very simple algorithm, that uses only integers:

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);

it will print out 1123447.
The idea is simple:

  1. you take the current digit of the number you want to sort(let's call it N)
  2. you go through all digits in already sorted number(let's call it S)
  3. if current digit in S is less than current digit in N, you just insert the digit in the current position in S. Otherwise, you just go to the next digit in S.

That version of the algorithm can sort in both asc in desc orders, you just have to change the condition.

Also, I suggest you to take a look at so-called Radix Sort, the solution here takes some ideas from radix sort, and I think the radix sort is the general case for that solution.

Arranging numbers within the digits in descending order without using arrays

Assuming that your number is not greater than Integer.MAX_VALUE you can use Redix Sort algorithm. It works fine with the complexity of O(n). Here, you can find the solution in following code, that doesn't use any array or lists and Strings.

public static void main(String[] args) {
int number = 45322;
int sortedNumber = 0;

/*
* This loop checks for each digit starting from 9 to 0.
* In case of ascending order it should be 0 to 9.
*/
for (int i = 9; i >= 0; i--) {
int tmpNumber = number;
while (tmpNumber > 0) {
int digit = tmpNumber % 10;
// Check for the greatest digit in the given number
if (digit == i) {
sortedNumber *= 10;
sortedNumber += digit;
}
tmpNumber /= 10;
}
}
System.out.println(sortedNumber); // prints 54322.
}

I think that should work for you.

Is there any way to sort the digits of an Integer without any ARRAY in JAVA?

Sorry, But after applying so much effort, I figured it out.

int n=54321;char ch;String s=Integer.toString(n);int l= s.length();for(int i=48;i<=57;i++)    //ascii values from 0 - 9{    for(int j=0;j<l;j++)    {      ch=s.charAt(j);      if(ch==(char)i)    // checking if a digit equals a number      {        System.out.print(ch);      }    }}

Sorting an integer without using string methods and without using arrays

It actually complicated requirement and so does this answer. It's pure logic and as it is it's a question from a test you should try understanding the logic on your own as a homework.

function checkHowManyDigits(num) { //input: 642534, output: 6 (length of the integer)
let count = 0;

while (num > 0) {
let a = num % 10;
num = (num - a) / 10;
count++;
}

return count;
}

function sortDigit(numOriginal) {

let i = checkHowManyDigits(numOriginal);
let minCount = 0;
let min = 10;
let num = numOriginal;

while (num > 0) {
let d = num % 10;
num = (num - d) / 10;
if (d < min) {
min = d;
minCount = 0;
} else if (d === min) {
minCount++;
}
}

let result = 0;
while (minCount >= 0) {
result += min * Math.pow(10, i - minCount - 1);
minCount--;
}

let newNum = 0;
num = numOriginal;
while (num > 0) {
let d = num % 10;
num = (num - d) / 10;
if (d !== min) {
newNum = newNum * 10 + d;
}
}

if (newNum == 0) return result;
else return result += sortDigit(newNum);

}

console.log(sortDigit(642531));

Sorting an integer's digits in descending order

Try this:

const sortNumber = (a) => Number([...("" + a)].sort().reverse().join(""));
const test1 = 123456;console.log(sortNumber(test1));
const test2 = 53283940;console.log(sortNumber(test2));

Sorting digits of an integer

You can use a loop and % 10 to extract each digit.
An outer loop from 0 to 9 could be used to test if the digit exists. If it exists, print it.

In pseudo code:

n = integer // 51234
FOR digit = 0 TO 9
temp = n
REPEAT
IF temp % 10 = digit THEN PRINT digit
temp /= 10
UNTIL temp = 0

Edit: This test in gcc shows that it handles zeros and repeated digits:

$ cat sortdigits.c
#include <stdio.h>
main () {
int n,digit,temp;
n = 43042025;
for (digit=0;digit<9;digit++)
for (temp=n;temp>0;temp/=10)
if (temp%10==digit) printf("%d",digit);
printf("\n");
}
$ ./sortdigits
00223445

How to ascending order the following integers with maple (should not display any digit)

It's unclear whether you have to show it by steps, or whether you can simply get Maple to sort the quantities directly.

Consider these three comparisons, which should be enough for you to figure out the ascending order:

evalb( 80^(120) > 100^(100) );

true

evalb( 60^140 > 80^(120) );

true

evalb( 40^(160) > 60^140 );

true

Or, starting with a random rearrangement, you can get Maple to sort them.

restart;

# separate paragraph or execution group
interface(typesetting=extended):

S := [100%^(100), 40%^(160), 60%^(140), 80%^(120) ];

answer := sort(S, (a,b)->value(a)<value(b));


Related Topics



Leave a reply



Submit