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 >>> 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) -> 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) <= 1:
11 return arr
12 else:
13 # 将第一个值做为基准
14 pivot = arr[0]
15 for i in arr:
16 # 将比急转小的值放到less数列
17 if i < pivot:
18 less.append(i)
19 # 将比基准打的值放到more数列
20 elif i > 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 sort
9 N = len(ARR)
10
11 # 3x+1 increment sequence: [1, 4, 13, 40, 121, 364, 1093, ...
12 ha = get_sort_seq(N, sort_seq)
13 print ha
14
15 for h in reversed(ha):
16 # h-sort the array (insertion sort)
17 for i in range(h,N):
18 j = i
19 while j >= 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 -= h
24 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, 0
117
118 # set up
119 max_number = max(arr)
120 count = [0] * (max_number+1) # is the array of "buckets" which starts at 0 and goes to max+1
121 output = [0] * len(arr)
122
123 # count occurrences of each number in arr and put it in 'bucket' in count
124 for number in arr: # the item at index number of count += 1 to found occurrence of that number
125 count[number] += 1
126
127 c1 += 1
128
129 # cumulative sum of occurrences
130 for i in range(1, len(count)): # cumulative sum
131 count[i] += count[i-1]
132
133 c2 += 1
134
135 # put into output stably
136 for j in range(len(arr)-1, -1, -1): # work backwards to keep stable
137 output_idx = count[arr[j]] - 1 # -1 as output len = arr len
138 output[output_idx] = arr[j] # put in right place in output
139 count[arr[j]] -= 1 # decrement value in count
140
141 print(output)
142 c3 += 1
143
144 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 7
147 the algorithm is
148 O(len) to count (and find max?)
149 O(range) for cumulative sum
150 O(len) to copy back
151
152 so O(3n + k) = O(n)
153
154 if the range is big (like in big_arr), the complexity is dominated by k
155
156 however in application, k usually small
157 """
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] = t
13 array.stats.assignments += 2

Related snippets