def selection_sort(l): start = 0 end = len(l) while end - start >= 2: imin = select_argmin(l, start, end) swap(l, start, imin) start += 1
def rest(s): end = len(s) - 1 while end > 0: swap(s, end, 0) end = end - 1 sift_down(s, 0, end)
def rest(s): end = len(s)-1 while end >0: swap(s,end,0) end = end -1 sift_down(s,0,end)
def bubblesort_internal(l, start, end): flag = True for i in range(start, end - 1): if l[i] > l[i + 1]: swap(l, i, i + 1) flag = False return flag
def quick_sort(array, start, end, draw_array): if start < end: draw_array(array, ['white' if x == start else 'red' for x in range(len(array))]) time.sleep(0.1) draw_array(array, ['yellow' if x == start else 'red' for x in range(len(array))]) time.sleep(0.1) draw_array(array, ['white' if x == start else 'red' for x in range(len(array))]) time.sleep(0.1) draw_array(array, ['yellow' if x == start else 'red' for x in range(len(array))]) time.sleep(0.1) j = end+1 for i in range(end, start, -1): draw_array(array, ['purple' if x == start else 'white' for x in range(len(array))]) time.sleep(0.8) draw_array(array, ['green' if x == i else 'white' for x in range(len(array))]) time.sleep(0.8) if array[i] > array[start]: draw_array(array, ['white' if x == i else 'white' for x in range(len(array))]) time.sleep(0.1) draw_array(array, ['yellow' if x == i else 'white' for x in range(len(array))]) time.sleep(0.1) draw_array(array, ['white' if x == i else 'white' for x in range(len(array))]) time.sleep(0.1) draw_array(array, ['yellow' if x == i else 'white' for x in range(len(array))]) time.sleep(0.1) j -= 1 swap(array, i, j) j -= 1 swap(array, start, j) quick_sort(array, start, j-1, draw_array) quick_sort(array, j+1, end, draw_array) draw_array(array, ['blue' for x in range(len(array))])
def insertion_sort(array): for i in range(1, len(array)): j = i while j > 0 and array[j] < array[j - 1]: swap.swap(array, j, j - 1) j -= 1 print(array) return array
def bubble_sort(nums): n = len(nums) for i in range(n - 1): j = 1 while j <= n - i - 1: if nums[j - 1] > nums[j]: swap(nums, j - 1, j) j += 1
def selection_sort(array): n = len(array) for i in xrange(n): for j in xrange(i, n): if array[j] < array[i]: swap(array, i, j) return array
def bubbleSort(lyst): n = len(lyst) while n > 1: i = 1 while i < n: if (lyst[i] < lyst[i - 1]): swap(lyst, i, i - 1) i += 1 n -= 1
def insertion_sort(array): n = len(array) for i in xrange(1, n): j = i while array[j] < array[j-1] and j > 0: swap(array, j, j-1) j = j-1 return array
def bubbleSort(lyst): n = len(lyst) while n > 1: i = 1 print(lyst) while i < n: if lyst[i] < lyst[i-1]: swap(lyst, i, i-1) i += 1 n -= 1
def sort(a_list): '''Insertion sort in place, O(n^2) time''' for i in range(1, len(a_list)): element = a_list[i] for j in range(i - 1, -1, -1): sorted_element = a_list[j] if element < sorted_element: swap(a_list, (j, j + 1)) else: break
def sift_down(s,start,end): root = start child = root * 2 +1 while child <= end: if child + 1 <= end and s[child] <= s[child+1]: child = child +1 if s[root] < s[child]: swap(s,root,child) root = child else: return
def cocktail_sort_internal(l, start, end): flag = True for i in range(start, end - 1): if l[i] > l[i + 1]: swap(l, i, i + 1) flag = False for j in range(end - 1, start, -1): # decreasing index if l[j] < l[j - 1]: swap(l, j, j - 1) flag = False return flag
def min_heapify(self, i): n = len(self.data) if left(i) < n and self.less(self.data[left(i)], self.data[i]): mini = left(i) else: mini = i if right(i) < n and self.less(self.data[right(i)], self.data[i]): mini = right(i) if mini != i: swap(self.data, i, mini) self.min_heapify(mini)
def partition(integer_list, lo, hi): mid = integer_list[int((lo + hi) / 2)] i = lo j = hi while True: while integer_list[i] < mid: i = i + 1 while integer_list[j] > mid: j = j - 1 if i >= j: return j swap.swap(integer_list, i, j)
def sift_down(s, start, end): root = start child = root * 2 + 1 while child <= end: if child + 1 <= end and s[child] <= s[child + 1]: child = child + 1 if s[root] < s[child]: swap(s, root, child) root = child else: return
def partition(array, l, r): if l <= r: p = array[r] j = l for i in xrange(l, r): if array[i] < p: swap(array, i, j) j = j + 1 swap(array, j, r) return j
def min_heapify(self, i): lessThan = self.less smallest = i l_idx, r_idx = left(i), right(i) if l_idx < self.heap_size and lessThan(self.data[l_idx], self.data[i]): smallest = l_idx if r_idx < self.heap_size and lessThan(self.data[r_idx], self.data[smallest]): smallest = r_idx if smallest != i: swap(self.data, i, smallest) return self.min_heapify(smallest)
def selection_sort(array): """Function that takes a list and returns it sorted.""" for i in range(len(array)): min_index = i min_val = array[i] for j in range(i + 1, len(array)): if array[j] < min_val: min_val = array[j] min_index = j swap.swap(array, i, min_index) print(array) return array
def min_heapify(self, i): l = left(i) r = right(i) if l < self.heap_size and self.less(self.data[l], self.data[i]): smallest = l else: smallest = i if r < self.heap_size and self.less(self.data[r], self.data[smallest]): smallest = r if smallest != i: swap(self.data, i, smallest) self.min_heapify(smallest)
def selectionSort(lyst): # O(n^2) i = 0 while i < len(lyst) - 1: # Do n - 1 searches minIndex = i # for the smallest j = i + 1 while j < len(lyst): # start a search if lyst[j] < lyst[minIndex]: minIndex = j j += 1 if minIndex != i: # Exchange if needed swap(lyst, minIndex, i) i += 1
def selectionSort(lyst): i = 0 while i < len(lyst) - 1: minIndex = i j= i + 1 print(lyst) while j < len(lyst): if lyst[j] < lyst[minIndex]: minIndex = j j += 1 if minIndex != i: swap(lyst, minIndex, i) i += 1
def min_heapify(self, i): H = self.data n = len(H) # which of i, left or right is smallest? if left(i) < n and H[i].key > H[left(i)].key: smallest = left(i) else: smallest = i if right(i) < n and H[smallest].key > H[right(i)].key: smallest = right(i) if smallest != i: swap(H, i, smallest) self.min_heapify(smallest)
def biselection_sort(l): start = 0 end = len(l) while end - start >= 2: (imin, imax) = select_extremes(l, start, end) swap(l, imin, start) if l[imax] < l[imin]: # very important! imax = imin swap(l, imax, end - 1) start += 1 end -= 1
def min_heapify(self, i): n = len(self.data) l = left(i) r = right(i) if l < n and self.data[i] > self.data[l]: largest = l else: largest = i if r < n and self.data[largest] > self.data[r]: largest = r if largest != i: swap(self.data, i, largest) self.min_heapify(largest)
def min_heapify(self, i): mini = 0 if left(i) < len(self.data) and less_key(self.data[left(i)], self.data[i]): mini = left(i) else: mini = i if right(i) < len(self.data) and less_key(self.data[right(i)], self.data[i]): mini = right(i) if mini != i: swap(self.data, i, mini) self.min_heapify(mini)
def min_heapify(self, i): l = left(i) r = right(i) size = len(self.data) - 1 if l <= size and self.data[l].key < self.data[i].key: smallest = l else: smallest = i if r <= size and self.data[r].key < self.data[smallest].key: smallest = r if smallest != i: swap(self.data, i, smallest) self.min_heapify(smallest) return self.data
def bubble_sort_better(nums): n = len(nums) for i in range(n - 1): flag = False # flag 用于标记内层循环中,是否发生了交换。 j = 1 while j <= n - i - 1: if nums[j - 1] > nums[j]: swap(nums, j - 1, j) flag = True # 发生了交换。 j += 1 if flag == False: break
def heap_sort(nums): n = len(nums) ''' 建立最大堆 ''' for index in range(n//2-1, -1, -1): filter_down(nums, index, n) ''' 删除最大堆的顶部的最大项 将最大堆的最大项与最后 1 项交换位置, 然后将除最后 1 项的剩余部分,调整为最大堆; 重复上面的操作。 ''' for index in range(n-1, 0, -1): swap(nums, 0, index) filter_down(nums, 0, index)
def shell_sort(array): h = 1 # Define value for h, for h-sorting the list. while h < len(array)//3: h = h*3 + 1 while h >= 1: i = h while i < len(array): j = i while j >= h and array[j] < array[j - h]: swap.swap(array, j, j - h) j -= h i += h h = h//3 print(array) return array
def partition(lyst, left, right): """ """ # Find the pivot and exchange it with the last item middle = (left + right) // 2 pivot = lyst[middle] lyst[middle] = lyst[right] lyst[right] = pivot # Set boundary point to first position boundary = left # Move items less than pvot to the left for index in range(left, right): if lyst[index] < pivot: swap(lyst, index, boundary) boundary += 1 # Exchange the pivot item and the boundary item swap(lyst, right, boundary) return boundary
def insert(self, obj): self.heap_size += 1 idx = self.heap_size - 1 if self.heap_size >= len(self.data): # expand the array if insert would # overflow the array bounds self.data += [obj] else: self.data[idx] = obj lessThan = self.less while idx >= 0 and lessThan(self.data[idx], self.data[parent(idx)]): # start with data at the bottom, percolate up # while parent is greater than swap(self.data, idx, parent(idx)) idx = parent(idx)
def bubble_sort(lyst): """ Compares two objects. If the left is more than the right they swap, otherwise the comparison moves over one place and begins again. """ count = len(lyst) while count > 1: # Do n-1 bubbles i = 1 # Start each bubble while i < count: if lyst[i] < lyst[i - 1]: # Exchange if needed swap(lyst, i, i - 1) i += 1 count -= 1
def selection_sort(lyst): """ Takes first position and compares to whole list, swapping if necessary. Moves on to second position etc. """ i = 0 while i < len( lyst ) - 1: # This statement iterates through the list till all values are compared min_index = i j = i + 1 while j < len(lyst): # This statement does the comparison if lyst[j] < lyst[min_index]: min_index = j j += 1 if min_index != i: swap(lyst, min_index, i) i += 1
def selectionSort(lyst): ''' complexity analysis: O(n**2) :param lyst: sorted list :return: none ''' i = 0 while i < len(lyst) - 1: minIndex = i j = i + 1 while j < len(lyst): if lyst[j] < lyst[minIndex]: minIndex = j j += 1 if minIndex != i: swap(lyst, minIndex, i) i += 1
def main(pep8out=sys.stdin): file_errors = group_errors(pep8out) # Fix files for filename, errors in file_errors.iteritems(): with swap(filename) as swp: for line in correct_file(filename, errors): swp.write(line) # TODO: Change this code to be meaningful return 0
def min_heapify(self, i): L = left(i) R = right(i) smallest = i #find the smallest of the children or root if L < self.heap_length and self.less(self.data[L], self.data[smallest]): smallest = L if R < self.heap_length and self.less(self.data[R], self.data[smallest]): smallest = R def swap(A, i, j): temp = A[i] A[i] = A[j] A[j] = temp #swap the smallest with the root and recur if smallest != i: swap(self.data, i, smallest) self.min_heapify(smallest)
def heap_increase_key(self, i): while i > 0 and self.less(self.data[i], self.data[parent(i)]): swap(self.data, i, parent(i)) i = parent(i)
from max_one_num import count_max_one from max_serial_one_num import count_max_serial_one from swap import swap if __name__=="__main__": print count_max_one(23) print count_max_serial_one(23) print swap(2,3)
#!/usr/bin/python # -*- coding:UTF-8 -*- #filename: using_sys.py import sys from swap import swap from using_name import run print('命领行:') for i in sys.argv: print i print('the path', sys.path, '\\n') print "the python version: ", sys.copyright print swap(12,33) run()
def heap_sort(self): self.build_min_heap() for i in range(len(self.data)-1, 0, -1): swap(self.data, 0, i) self.heap_size -= 1 self.min_heapify(0)