-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDescendingOrder.java
74 lines (58 loc) · 2.07 KB
/
DescendingOrder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
public class DescendingOrder {
public static int sortDesc(final int num) {
// get the number's digits separated into an array.
int[] integerArray = getIntegerArray(num);
// sort the digits in a descending order.
bubbleSortDescending(integerArray);
// return the number's array assembled back into an int.
return assembleInt(integerArray);
}
/**
* This method returns an array of integers,
* consisting of individual digits in the original integer number.
*/
public static int[] getIntegerArray(int num){
// convert the integer number to a string.
String numString = String.valueOf(num);
// to store the individual digits in the integer number
int[] integerArray = new int[numString.length()];
for(int i = 0; i < numString.length(); i++){
// get the inidividual characters, and convert them to an int.
integerArray[i] = Character.getNumericValue(numString.charAt(i));
}
return integerArray;
}
/**
* This method sorts the input array of integers into
* descending order, using Bubble Sort.
*/
public static int[] bubbleSortDescending(int[] input){
for(int i = 0; i < input.length; i++){
for(int j = 0; j < input.length - 1; j++){
if(input[j] < input[j + 1]){
// move back the smaller number.
int temp = input[j];
input[j] = input[j + 1];
input[j + 1] = temp;
}
}
}
return input;
}
/**
* This method assembles the digits in the integer array
* into a number.
*/
public static int assembleInt(int[] integerArray){
// to store the digits as characters.
char[] numChar = new char[integerArray.length];
for(int i = 0; i < integerArray.length; i++){
// get the character equivalent of the int, and cast it to a character.
numChar[i] = (char)(integerArray[i] + '0');
}
// create a string form the character array.
String numString = new String(numChar);
// convert the string to an integer.
return Integer.parseInt(numString);
}
}