Java : Sort integer array without using Arrays.sort()

后端 未结 12 2637
滥情空心
滥情空心 2021-02-14 00:59

This is the instruction in one of the exercises in our Java class. Before anything else, I would like to say that I \'do my homework\' and I\'m not just being lazy asking someon

12条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-14 01:31

    This will surely help you.

    int n[] = {4,6,9,1,7};
    
        for(int i=n.length;i>=0;i--){
            for(int j=0;j n[j+1]){
                    swapNumbers(j,j+1,n);
                }
            }
    
        }
        printNumbers(n);
    }
    private static void swapNumbers(int i, int j, int[] array) {
    
        int temp;
        temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
    
    private static void printNumbers(int[] input) {
    
        for (int i = 0; i < input.length; i++) {
            System.out.print(input[i] + ", ");
        }
        System.out.println("\n");
    }
    

提交回复
热议问题