How to sort an array in a single loop?

后端 未结 22 2956
面向向阳花
面向向阳花 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:33

    Single loop array sort:

    for(int i = 0, j=i+1; i < arr.length && j arr[j])
            {
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;              
                i=0;
                j=i+1;
            } 
            else
            {
                i++;
                j++;
            }
        }
    

提交回复
热议问题