int[] array (sort lowest to highest)

后端 未结 11 1515
悲哀的现实
悲哀的现实 2020-12-01 13:32

So I am not sure why this is becoming so hard for me, but I need to sort high to low and low to high.

For high to low I have:

int a, b;
int temp;
int         


        
11条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 13:56

    You can try with bubble sort: Example shown below

    int[] numbers = { 4, 7, 20, 2, 56 };
    int temp;
    
    for (int i = 0; i < numbers.length; i++)
    {
           for(int j = 0; j < numbers.length; j++)
           {
                    if(numbers[i] > numbers[j + 1])
                    {
                                temp = numbers [j + 1];
                                numbers [j + 1]= numbers [i];
                                numbers [i] = temp;
                    }
            }
    }
    
    for (int i = 0; i < numbers.length; i++)
    {
             System.out.println(numbers[i].toString());
    }
    

提交回复
热议问题