1.sorts method python (7)counting_sort

计数排序Counting sort 原理:

https://blog.csdn.net/weililansehudiefei/article/details/71941188

my stupid code:

# -*- coding: utf-8 -*-
"""
Created on Wed Aug 14 14:47:01 2019

@author: zhang
"""

import numpy as np

def counting_sort(data):
        data_num = len(data)
        data_index = [0]*data_num
        data_final = list(np.full([data_num], np.nan))
        for i in range(data_num):
                for j in range(data_num):
                        if i!=j and data[i]>data[j]:
                                data_index[i] = data_index[i]+1
                data_temp = data_final[data_index[i]]                
                while  np.isnan(data_temp)==False:  #if data list have two same data
                        data_index[i] = data_index[i]+1
                        data_temp = data_final[data_index[i]]  
                data_final[data_index[i]] = data[i]

        return data_final
        
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,1]
         data_num = len(data)

         data = counting_sort(data)

answer:

"""
This is pure python implementation of counting sort algorithm
For doctests run following command:
python -m doctest -v counting_sort.py
or
python3 -m doctest -v counting_sort.py
For manual testing run:
python counting_sort.py
"""

from __future__ import print_function


def counting_sort(collection):
    """Pure implementation of counting sort algorithm in Python
    :param collection: some mutable ordered collection with heterogeneous
    comparable items inside
    :return: the same collection ordered by ascending
    Examples:
    >>> counting_sort([0, 5, 3, 2, 2])
    [0, 2, 2, 3, 5]
    >>> counting_sort([])
    []
    >>> counting_sort([-2, -5, -45])
    [-45, -5, -2]
    """
    # if the collection is empty, returns empty
    if collection == []:
        return []

    # get some information about the collection
    coll_len = len(collection)
    coll_max = max(collection)
    coll_min = min(collection)

    # create the counting array
    counting_arr_length = coll_max + 1 - coll_min
    counting_arr = [0] * counting_arr_length

    # count how much a number appears in the collection
    for number in collection:
        counting_arr[number - coll_min] += 1

    # sum each position with it's predecessors. now, counting_arr[i] tells
    # us how many elements <= i has in the collection
    for i in range(1, counting_arr_length):
        counting_arr[i] = counting_arr[i] + counting_arr[i-1]

    # create the output collection
    ordered = [0] * coll_len

    # place the elements in the output, respecting the original order (stable
    # sort) from end to begin, updating counting_arr
    for i in reversed(range(0, coll_len)):
        ordered[counting_arr[collection[i] - coll_min]-1] = collection[i]
        counting_arr[collection[i] - coll_min] -= 1

    return ordered

def counting_sort_string(string):
    return ''.join([chr(i) for i in counting_sort([ord(c) for c in string])])


if __name__ == '__main__':
    # Test string sort
    assert "eghhiiinrsssttt" == counting_sort_string("thisisthestring")

    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(counting_sort(unsorted))

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据你提供的代码片段,我注意到你正在使用MyBatis的动态SQL中的foreach标签来更新`equ_device_tsl_property`表的数据。然而,在你的代码中,存在一些语法错误和逻辑问题。 首先,你需要修复语法错误。在foreach标签的`open`、`separator`和`close`属性值中,你使用了括号,但是缺少了对应的闭合括号。你需要在每个属性值的末尾添加一个括号。此外,你在每个CASE语句中缺少了END关键字来结束它们。 另外,你在每个CASE语句中的写法不正确。应该是`WHEN id = #{item.id}`,而不是`when id = #{item.id}`。此外,你在每个CASE语句之间使用了逗号分隔,这是不正确的。 修正后的代码应如下所示: ```xml <update id="updateEquDeviceTslProperty" parameterType="YourParameterType"> UPDATE equ_device_tsl_property SET <foreach item="item" collection="array" open="(" separator="," close=")"> name = CASE WHEN id = #{item.id} THEN #{item.name} ELSE name END, device_code = CASE WHEN id = #{item.id} THEN #{item.deviceCode} ELSE device_code END, value_type = CASE WHEN id = #{item.id} THEN #{item.valueType} ELSE value_type END, property_id = CASE WHEN id = #{item.id} THEN #{item.propertyId} ELSE property_id END, property_name = CASE WHEN id = #{item.id} THEN #{item.propertyName} ELSE property_name END, sorts_index = CASE WHEN id = #{item.id} THEN #{item.sortsIndex} ELSE sorts_index END </foreach> </update> ``` 请注意,上述代码仅为修复语法错误和逻辑问题,并没有对你的业务逻辑进行验证。你需要根据你的实际需求和数据表结构进行适当调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值