sorting integers in order lowest to highest java

后端 未结 7 1152
刺人心
刺人心 2020-11-30 13:03

These numbers are stored in the same integer variable. How would I go about sorting the integers in order lowest to highest?

11367
11358
11421
11530
11491
11218
11         


        
相关标签:
7条回答
  • 2020-11-30 13:31
    import java.util.Arrays;
    
    public class sortNumber {
    
    public static void main(String[] args) {
        // Our array contains 13 elements 
        int[] array = {9,  238,  248,  138,  118,  45,  180,  212,  103,  230,  104,  41,  49}; 
    
        Arrays.sort(array); 
    
        System.out.printf(" The result : %s", Arrays.toString(array)); 
      }
    }
    
    0 讨论(0)
  • 2020-11-30 13:36

    You can put them into a list and then sort them using their natural ordering, like so:

    final List<Integer> list = Arrays.asList(11367, 11358, 11421, 11530, 11491, 11218, 11789);
    Collections.sort( list );
    // Use the sorted list
    

    If the numbers are stored in the same variable, then you'll have to somehow put them into a List and then call sort, like so:

    final List<Integer> list = new ArrayList<Integer>();
    list.add( myVariable );
    // Change myVariable to another number...
    list.add( myVariable );
    // etc...
    
    Collections.sort( list );
    // Use the sorted list
    
    • Collections.sort( List )
    0 讨论(0)
  • 2020-11-30 13:42

    There are two options, really:

    1. Use standard collections, as explained by Shakedown
    2. Use Arrays.sort

    E.g.,

    int[] ints = {11367, 11358, 11421, 11530, 11491, 11218, 11789};
    Arrays.sort(ints);
    System.out.println(Arrays.asList(ints));
    

    That of course assumes that you already have your integers as an array. If you need to parse those first, look for String.split and Integer.parseInt.

    0 讨论(0)
  • 2020-11-30 13:42

    Well, if you want to do it using an algorithm. There are a plethora of sorting algorithms out there. If you aren't concerned too much about efficiency and more about readability and understandability. I recommend Insertion Sort. Here is the psudo code, it is trivial to translate this into java.

    begin
        for i := 1 to length(A)-1 do
        begin
            value := A[i];
            j := i - 1;
            done := false;
            repeat
                { To sort in descending order simply reverse
                  the operator i.e. A[j] < value }
                if A[j] > value then
                begin
                    A[j + 1] := A[j];
                    j := j - 1;
                    if j < 0 then
                        done := true;
                end
                else
                    done := true;
            until done;
            A[j + 1] := value;
        end;
    end;
    
    0 讨论(0)
  • 2020-11-30 13:49

    if array.sort doesn't have what your looking for you can try this:

    package drawFramePackage;
    import java.awt.geom.AffineTransform;
    import java.util.ArrayList;
    import java.util.ListIterator;
    import java.util.Random;
    public class QuicksortAlgorithm {
        ArrayList<AffineTransform> affs;
        ListIterator<AffineTransform> li;
        Integer count, count2;
        /**
         * @param args
         */
        public static void main(String[] args) {
            new QuicksortAlgorithm();
        }
        public QuicksortAlgorithm(){
            count = new Integer(0);
            count2 = new Integer(1);
            affs = new ArrayList<AffineTransform>();
            for (int i = 0; i <= 128; i++){
                affs.add(new AffineTransform(1, 0, 0, 1, new Random().nextInt(1024), 0));
            }
            affs = arrangeNumbers(affs);
            printNumbers();
        }
        public ArrayList<AffineTransform> arrangeNumbers(ArrayList<AffineTransform> list){
            while (list.size() > 1 && count != list.size() - 1){
                if (list.get(count2).getTranslateX() > list.get(count).getTranslateX()){
                    list.add(count, list.get(count2));
                    list.remove(count2 + 1);
                }
                if (count2 == list.size() - 1){
                    count++;
                    count2 = count + 1;
                }
                else{
                count2++;
                }
            }
            return list;
        }
        public void printNumbers(){
            li = affs.listIterator();
            while (li.hasNext()){
                System.out.println(li.next());
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 13:50

    For sorting narrow range of integers try Counting sort, which has a complexity of O(range + n), where n is number of items to be sorted. If you'd like to sort something not discrete use optimal n*log(n) algorithms (quicksort, heapsort, mergesort). Merge sort is also used in a method already mentioned by other responses Arrays.sort. There is no simple way how to recommend some algorithm or function call, because there are dozens of special cases, where you would use some sort, but not the other.

    So please specify the exact purpose of your application (to learn something (well - start with the insertion sort or bubble sort), effectivity for integers (use counting sort), effectivity and reusability for structures (use n*log(n) algorithms), or zou just want it to be somehow sorted - use Arrays.sort :-)). If you'd like to sort string representations of integers, than u might be interrested in radix sort....

    0 讨论(0)
提交回复
热议问题