arr = [2, 6, 7, 4, 5, -2, 0, -98, 55, 66, 1] def improved_bubble_sort(array): assert len(array) != 0 for _ in range(len(array) - 1): is_ok = True for i in range(len(array) - 1 - _): if array[i] > array[i + 1]: array[i] ^= array[i + 1] array[i + 1] ^= array[i] array[i] ^= array[i + 1] is_ok = False if not is_ok: print 'one iteration:', array if is_ok: break print 'after sort:', array def improved_bubble_sort2(array): """ stop with pos :param array: :return: """ assert len(array) != 0 pos = len(array) - 1 for _ in range(len(array) - 1): is_ok = True for i in range(pos): if array[i] > array[i + 1]: array[i] ^= array[i + 1] array[i + 1] ^= array[i] array[i] ^= array[i + 1] pos = i + 1 is_ok = False if not is_ok: print 'one iteration:', array if is_ok: break print 'after sort:', array def bubble_sort(array): assert len(array) != 0 for _ in range(len(array) - 1): for i in range(len(array) - 1 - _): if array[i] > array[i + 1]: array[i] ^= array[i + 1] array[i + 1] ^= array[i] array[i] ^= array[i + 1] print 'one iteration:', array print 'after sort:', array def bubble_1(a, n): unsorted_last_pos = n - 1 while unsorted_last_pos > 0: update_pos = 0 for j in range(unsorted_last_pos): if a[j] > a[j + 1]: a[j] ^= a[j + 1] a[j + 1] ^= a[j] a[j] ^= a[j + 1] update_pos = j unsorted_last_pos = update_pos print 'after sort:', a def bubble_2(a, n): """ bubble on bidirectional :param a: :param n: :return: """ low = 0 high = n - 1 while low < high: for j in range(low, high): if a[j] < a[j + 1]: a[j] ^= a[j + 1] a[j + 1] ^= a[j] a[j] ^= a[j + 1] high -= 1 indexes = range(low, high) indexes.reverse() for i, v in enumerate(indexes): if i == len(indexes) - 1: break i_cnt = indexes[i] i_cnt_next = indexes[i + 1] if a[i_cnt] > a[i_cnt_next]: a[i_cnt] ^= a[i_cnt_next] a[i_cnt_next] ^= a[i_cnt] a[i_cnt] ^= a[i_cnt_next] low += 1 print 'after sort:', a def bubble_sort_by_size(array, size): assert len(array) != 0 for _ in range(size - 1): for i in range(size - 1 - _): if array[i] > array[i + 1]: array[i] ^= array[i + 1] array[i + 1] ^= array[i] array[i] ^= array[i + 1] print 'one iteration:', array print 'after sort:', array def bubble_sort2(a, n): for i in range(n - 1): for j in range(n - i - 1): if a[j] > a[j + 1]: a[j + 1] ^= a[j] a[j] ^= a[j + 1] a[j + 1] ^= a[j] # print 'one iteration:', a print 'after sort:', a def get_kth_elem(k): assert len(arr) > k bubble_2(arr, 11) ret = arr[k - 1] print 'find', k, '-th, value:', ret return ret def get_kth_elem2(k): """ this algorithm sort the sorted k elem, then eat each elem, and set this elem in suitable pos on sorted list :param k: :return: """ assert len(arr) > k bubble_2(arr, k) unsorted_array = arr[k:] ret = arr[k - 1] new_sorted_array = arr[:k] for v in unsorted_array: if v <= ret: continue else: new_sorted_array = set_elem_into_sorted_array(new_sorted_array, v) ret = new_sorted_array[k - 1] print 'find', k, '-th, value:', ret return ret def set_elem_into_sorted_array(sorted_array, elem): ret = [] for i, v in enumerate(sorted_array): if elem > v: for j in range(i): ret.append(sorted_array[j]) ret.append(elem) boundary = range(i, len(sorted_array)) boundary.reverse() for j in range(len(boundary)): ret.append(sorted_array[j]) if len(ret) == len(sorted_array): return ret return ret
python implement for selected problems
最新推荐文章于 2021-07-11 20:30:49 发布