sorting integers in order lowest to highest java

后端 未结 7 1292
刺人心
刺人心 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: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;
    

提交回复
热议问题