How to sort an array in a single loop?

后端 未结 22 2948
面向向阳花
面向向阳花 2020-12-19 09:14

So I was going through different sorting algorithms. But almost all the sorting algorithms require 2 loops to sort the array. The time complexity of Bubble sort & Insert

22条回答
  •  鱼传尺愫
    2020-12-19 09:45

    public int find2ndLargest() {
        int[] arr = {1,3,14,25,7,20,11,30};
        int temp;
    
        // sort array
        for (int i=0;iarr[i+1]) {
                temp = arr[i];
                arr[i]=arr[i+1];
                arr[i+1]=temp;
                i=0;
            }
        }
        return arr[arr.length-2];
    }
    

提交回复
热议问题