1.sorts method python (6)comb_sort

梳排序原理:

https://blog.csdn.net/heyuchang666/article/details/50554251

my stupid code:

# -*- coding: utf-8 -*-
"""
Created on Wed Jul 31 10:14:28 2019

@author: yue zhang

E-mails: yuezh2015@163.com
""" 
def Comb_sort(data,data_num):
         Defort = 1.3
         data_temp = int(data_num/Defort)
         data_len = len(data)
         for i in range(0,data_len-data_temp):
               if data[i]>data[i+data_temp]:
                     data[i],data[i+data_temp] = data[i+data_temp],data[i]
         print(data)
         if data_temp>1:
               Comb_sort(data,data_temp)
         #print(data)
         return data
      
                 
if __name__ == '__main__':
         data = [10,20,50,3,5,7,9,11,88,55,77,56,1,2,4,555,19, 25, 26, 29 ,38 ,50 ,80, 91 ,86, 78 ,66 ,46, 32 ,19 ,15 , 7]
         #data = [8,4, 3, 7, 6, 5, 2 ,1]
         data_num = len(data)

         data = Comb_sort(data,data_num)

answer:

"""
Comb sort is a relatively simple sorting algorithm originally designed by Wlodzimierz Dobosiewicz in 1980.
Later it was rediscovered by Stephen Lacey and Richard Box in 1991. Comb sort improves on bubble sort.
This is pure python implementation of comb sort algorithm
For doctests run following command:
python -m doctest -v comb_sort.py
or
python3 -m doctest -v comb_sort.py
For manual testing run:
python comb_sort.py
"""

def comb_sort(data):
    """Pure implementation of comb sort algorithm in Python
    :param collection: some mutable ordered collection with heterogeneous
    comparable items inside
    :return: the same collection ordered by ascending
    Examples:
    >>> comb_sort([0, 5, 3, 2, 2])
    [0, 2, 2, 3, 5]
    >>> comb_sort([])
    []
    >>> comb_sort([-2, -5, -45])
    [-45, -5, -2]
    """
    shrink_factor = 1.3
    gap = len(data)
    swapped = True
    i = 0

    while gap > 1 or swapped:
        # Update the gap value for a next comb
        gap = int(float(gap) / shrink_factor)

        swapped = False
        i = 0

        while gap + i < len(data):
            if data[i] > data[i+gap]:
                # Swap values
                data[i], data[i+gap] = data[i+gap], data[i]
                swapped = True
            i += 1

    return data


if __name__ == '__main__':
    try:
        raw_input          # Python 2
    except NameError:
        raw_input = input  # Python 3

    user_input = raw_input('Enter numbers separated by a comma:\n').strip()
    unsorted = [int(item) for item in user_input.split(',')]
    print(comb_sort(unsorted))

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值