# 7 examples of 'python sort array of integers' in Python

Every line of 'python sort array of integers' 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
``145def counting_sort_ints(arr):146    """147    for array [7,1,5,2,2] len = 5, range of values from 0 = 0 to 7148    the algorithm is149    O(len) to count (assuming that finding max and min is O(1))150    O(range) for cumulative sum151    O(len) to copy back152153    so O(2n + k) = O(n) where k is the range of items and assumed to be less than the input size.154155    if the range is big (like in big_arr), the complexity is dominated by k156157    However in application, k usually small.158    """159    c1, c2, c3 = 0, 0, 0  # Use to look at the counts.160161    # Set up162    max_number = max(arr)163    count = [0] * (max_number+1)            # is the array of "buckets" which starts at 0 and goes to max+1164    output = [0] * len(arr)165166    # Count occurrences of each number in arr and put it in 'bucket' in count.167    for number in arr:                      # the item at index number of count += 1 to found occurrence of that number168        count[number] += 1169        c1 += 1170171    # Cumulative sum of occurrences.172    for i in range(1, len(count)):          # cumulative sum173        count[i] += count[i-1]174        c2 += 1175176    # Put into output stably.177    for j in range(len(arr)-1, -1, -1):     # work backwards to keep stable178        output_idx = count[arr[j]] - 1      # -1 as output len = arr len179        output[output_idx] = arr[j]         # put in right place in output180        count[arr[j]] -= 1                  # decrement value in count181        c3 += 1182    print("first loop counting: " + str(c1) + "\nsecond loop summing: " + str(c2) + "\nthird loop copying: " + str(c3))183    return output``
``16def CombSort(array):17 18    # The inc starts from the highest value19    inc = len(array)2021    # This boolean tracks whether a swap occured in the current iteration22    swap = True23 24    # if inc is 1 and no swaps are happening, the array has been sorted25    # if either of them are true, trudge forward because work needs to be done26    while not inc == 1 or swap == True:2728        # Get a new value for the inc29        inc = RecalibrateInc(inc)3031        # Set swap to False32        swap = False33 34        # Do the bubb.. bubb.. CombSort35        # len(array) - inc because len(array) - inc + inc onwards, no elements exist36        for i in range(0, len(array) - inc): 3738            if array[i] &gt; array[i + inc]:39            40                # swap the array elements41                array[i], array[i + inc] = array[i + inc], array[i]42            43                swap = True``
``115def counting_sort(arr):116    c1, c2, c3 = 0, 0, 0117118    # set up119    max_number = max(arr)120    count = [0] * (max_number+1)            # is the array of "buckets" which starts at 0 and goes to max+1121    output = [0] * 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``
``102def sorttest(A):103  bubblesort(A)``
``38def sort(a):39      mergeSort(a,0,len(a)-1)``
``113def sort(packed, ref, reverse=True):114    """115    Sort a series of packed list, according to a ref list.116    Also return the original index before the sort.117    """118    assert (isinstance(packed, tuple) or isinstance(packed, list)) and isinstance(ref, list)119    packed = [ref] + [range(len(ref))] + list(packed)120    sorted_packed = [list(t) for t in zip(*sorted(zip(*packed), reverse=reverse))]121    return tuple(sorted_packed[1:])``
``7def BitonicSort(direction, arr):89    '''10    11    The obvious condition that we have over here is that is the length of the array is less than or equal to 1, then it makes no sense to call the BitonicMerge function on the array as it contains only one element.12    13    '''1415    if len(arr) &lt;= 1:1617        return arr1819    else: 2021        '''2223        When you split the array to form a Bitonic Sequence, you want the first half to be in an increasing order and the second half to be in a decreasing order. The booleans True and False respresent increasing and decreasing order of values respectively and this will become more obvious in the BitonicCompare functions. 2425        '''2627        first = BitonicSort(True, arr[:len(arr) // 2])28        second = BitonicSort(False, arr[len(arr) // 2:])2930        '''3132        This is the only line that might be a little confusing and the reason for this is very simple. When we looked at the example, we executed the increasing and decreasing sorting of the array to form Bitonic Sequences simultaneously. As it turns out, we are not following the same procedures here. In fact, these operations are being done at very different times in the execution of the array but don't let that confuse you. This is the part in the process where you move to the next stage and increase the size of the Bitonic Sequence in the array.3334        '''3536        return BitonicMerge(direction, first + second)``