The basic idea of quick sorting:
By sorting the sorted data into separate two parts, one part of all data is smaller than the other part of the data, and then the two parts of the data are quickly sorted by this method, the entire sorting process can be recursive, so as to achieve the entire data into ordered order.
Cases:
arr = [49,38,04,97,76,13,27,49,55,65], set the first digit 49 as the key value, from right to left to find a number smaller than the key value, the number of the found assigned to the first digit;
arr = [27,38,04,97,76,13,27,49,55,65], and then from the left first bit to the right to find a number larger than the key value, the number of the found is assigned to the last right-to-left to find the number;
arr = [27,38,04,97,76,13,97,49,55,65], then right-to-left, left-to-right, until left=right, jumps out of the loop and assigns the key value to the index value. Finally, the groups on both sides are recursively recursive.
Code:
def quick_sort (lists, left, right): #快速排序 If left >= right: #当递归调用的分组为1个数时返回列表 return lists Key = Lists[left] #保存key值, at the end of a round call, save to middle value low = left high = right #供递归调用时使用 while left < right:< c10/> #通过下面两个循环依次交替赋值并使key值两侧为大小分组 while left < right and Lists[right] >= key: Right- = 1 lists[ Left] = Lists[right] While left < right and Lists[left] <= key: Left + + 1 lists[right] = lists[left]< C17/>lists[right] = key quick_sort (lists, low, left-1) #对key值左侧进行排序分组 quick_sort (lists, left+1, high) #对key值右侧进行排序分组 return lists