defbubble_sort (list):for n inrange (len(list)-1,0,-1):for i inrange(n):if (list[i]>list[i+1]): list[i],list[i+1]=list[i+1],list[i]returnlist
defbubble_sort (list):for n inrange(0,len(list)):for i inrange (len(list)-n-1):if (list[i]>list[i+1]): list[i],list[i+1]=list[i+1],list[i]returnlist
Space O(1) (in place)
Time O( n2 )
Selection Sort 选择排序
n个元素,做n轮,每轮选出剩余元素最小值,归位。
For each pass, we will move from left to right looking for the next largest(smallest) value. Once that is found, it will be swapped into its final position.
Insertion sort is based on the idea that one element from the input elements is consumed in each iteration to find its correct position i.e, the position to which it belongs in a sorted array.
It iterates the input elements by growing the sorted array at each iteration. It compares the current element with the largest value in the sorted array. If the current element is greater, then it leaves the element in its place and moves on to the next element else it finds its correct position in the sorted array and moves it to that position. This is done by shifting all the elements, which are larger than the current element, in the sorted array to one position ahead.