def partition(items, low, high): pivot_index = (low + high) / 2 pivot_value = items[pivot_index] items = swap_item(items, pivot_index, high) store_index = low for k in range(low, high): if items[k] < pivot_value: items = swap_item(items, k, store_index) store_index += 1 items = swap_item(items, store_index, high) return store_index
def selection_sort(items): num_items = len(items) if num_items < 2: return items curr_min = 0 for j in range(num_items): # Assign minimum to j, initially curr_min = j # Loop through all elements /after/ j, # checking to find the new smallest item. for i in range(j + 1, num_items): # Update current min if this one is smaller. if items[i] < items[curr_min]: curr_min = i # After the internal loop finishes, # check (on each outer iteration) if j is less than the new curr_min. # If so, then a smaller item was found and needs to be swapped. if curr_min != j: swap_item(items, j, curr_min) return items