+ 1
How can I arrange an array of integers so it's order from bigger to lower, For example instead of having {8,9,2,7,6} having {2,6,7,8,9} without rewriting the or having a new array?
2 Réponses
+ 10
I recommend you have a look at some sorting algorithms if you want to write your own methods (instead of always using Collections / Arrays .sort().) Trying to code these I think can greatly improve your programming skills.
These are some: (easier)
* Insertion Sort
* Selection Sort
* Bubble sort (easiest, but never use for real use)
Harder ones, but much faster:
* Shell sort
* Quick sort
* Merge sort (requires new array)
I think Arrays.sort uses Tim sort.
+ 3
import java.util.Arrays;
Arrays.sort();
---------------------------------------------------------------
Example:
import java.util.*;
import java.util.Arrays;
class Dcoder
{
public static void main(String args[])
{
int[] nums = {1,9,5,2,8,3,7,6,4};
Arrays.sort(nums);
for(int i=0;i<nums.length;i++){
System.out.print(nums[i]);
}
}
}