python implement for selected problems

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值