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

后端 未结 12 2625
滥情空心
滥情空心 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条回答
  •  执念已碎
    2021-02-14 01:51

    int x[] = { 10, 30, 15, 69, 52, 89, 5 };
        int max, temp = 0, index = 0;
        for (int i = 0; i < x.length; i++) {
            int counter = 0;
            max = x[i];
            for (int j = i + 1; j < x.length; j++) {
    
                if (x[j] > max) {
                    max = x[j];
                    index = j;
                    counter++;
                }
    
            }
            if (counter > 0) {
                temp = x[index];
                x[index] = x[i];
                x[i] = temp;
            }
        }
        for (int i = 0; i < x.length; i++) {
            System.out.println(x[i]);
        }
    

提交回复
热议问题