How to sort an array in a single loop?

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

    with python:

    def sort(array):
        n = len(array);
        i = 0;
        mod = 0;
        if(len(array)<= 1):
            return(array)
    
        while n-1:
            if array[mod] > array[mod+1]:
                array[mod], array[mod+1] = array[mod+1], array[mod]
    
            mod+=1
            if mod+1 >= n:
                n-=1
                mod = 0
    
        return array
    

提交回复
热议问题