# 9 examples of 'python sort array' in Python

Every line of 'python sort array' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure. ## All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
``31def column_based_sort(array, column=0):32    """33    &gt;&gt;&gt; column_based_sort([(5, 1), (4, 2), (3, 0)], 1)34    [(3, 0), (5, 1), (4, 2)]35    """36    return sorted(array, key=lambda x: x[column])``
``455def Sort(self):456    r"""Sort(doubleArray self)"""457    return _array.doubleArray_Sort(self)``
``102def sorttest(A):103  bubblesort(A)``
``909""" Contains(self: Queue[T], item: T) -&gt; bool """910pass``
``38def sort(a):39      mergeSort(a,0,len(a)-1)``
``5def quick_sort(arr):6    less = []7    pivot_list = []8    more = []9    # 递归出口10    if len(arr) &lt;= 1:11        return arr12    else:13        # 将第一个值做为基准14        pivot = arr15        for i in arr:16            # 将比急转小的值放到less数列17            if i &lt; pivot:18                less.append(i)19            # 将比基准打的值放到more数列20            elif i &gt; pivot:21                more.append(i)22            # 将和基准相同的值保存在基准数列23            else:24                pivot_list.append(i)25        # 对less数列和more数列继续进行排序26        less = quick_sort(less)27        more = quick_sort(more)28        return less + pivot_list + more``
``6def Sort(ARR, array_history=None, sort_seq=None):7  """Rearranges the array, ARR, in ascending order, using the natural order."""8  # array_history; Used in tests. When true prints ASCII Art demonstrating the sort9  N = len(ARR)1011  # 3x+1 increment sequence:  [1, 4, 13, 40, 121, 364, 1093, ...12  ha = get_sort_seq(N, sort_seq)13  print ha1415  for h in reversed(ha):16    # h-sort the array (insertion sort)17    for i in range(h,N):18      j = i19      while j &gt;= h and __lt__(ARR[j], ARR[j-h]):20        if array_history is not None:21          array_history.add_history(ARR, {j:'*', j-h:'*'} )22        _exch(ARR, j, j-h)23        j -= h24    assert _isHsorted(ARR, h)25  assert _isSorted(ARR)26  if array_history is not None:27    array_history.add_history(ARR, None)``
``115def counting_sort(arr):116    c1, c2, c3 = 0, 0, 0117118    # set up119    max_number = max(arr)120    count =  * (max_number+1)            # is the array of "buckets" which starts at 0 and goes to max+1121    output =  * len(arr)122123    # count occurrences of each number in arr and put it in 'bucket' in count124    for number in arr:                      # the item at index number of count += 1 to found occurrence of that number125        count[number] += 1126127        c1 += 1128129    # cumulative sum of occurrences130    for i in range(1, len(count)):          # cumulative sum131        count[i] += count[i-1]132133        c2 += 1134135    # put into output stably136    for j in range(len(arr)-1, -1, -1):       # work backwards to keep stable137        output_idx = count[arr[j]] - 1      # -1 as output len = arr len138        output[output_idx] = arr[j]         # put in right place in output139        count[arr[j]] -= 1                  # decrement value in count140141        print(output)142        c3 += 1143144    print("first loop: " + str(c1) + "\nsecond loop: " + str(c2) + "\nthird loop: " + str(c3))145    """146    for array [7,1,5,2,2] len = 5, range of values from 0 = 0 to 7147    the algorithm is148    O(len) to count (and find max?)149    O(range) for cumulative sum150    O(len) to copy back151152    so O(3n + k) = O(n)153154    if the range is big (like in big_arr), the complexity is dominated by k155156    however in application, k usually small157    """158    return output``
``9def swap(self, array, index_a, index_b):10    t = array[index_a]11    array[index_a] = array[index_b]12    array[index_b] = t13    array.stats.assignments += 2``