How to sort an array in a single loop?
问题 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 & Insertion sort is O(n) for Best case but is O(n^2) as worst case which again requires 2 loops. Is there a way to sort an array in a single loop? 回答1: Here, a single-loop Bubble Sort in Python: def bubbly_sortish(data): for _ in xrange(len(data)**2): i, j = _/len(data), _%len(data) if i<j and data[i] > data[j]: data[i], data[j] =