1.sorts method python (5)cocktail_shaker_sort

本文深入探讨了鸡尾酒排序算法,一种改进版的冒泡排序,通过双向遍历来提高效率。提供了详细的代码实现和运行示例,展示了如何在Python中实现鸡尾酒排序。

鸡尾酒排序原理:http://bubkoo.com/2014/01/15/sort-algorithm/shaker-sort/

双向冒泡排序 (Bidirectional Bubble Sort)、波浪排序 (Ripple Sort)、摇曳排序 (Shuffle Sort)、飞梭排序 (Shuttle Sort) 和欢乐时光排序 (Happy Hour Sort)

my stupid code: same problem to bubble sort: 如果直接输入一个排好的序列, 答案可以直接输出,而我的代码需要运行一定的次数。

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

@author: yue zhang

E-mails: yuezh2015@163.com
"""
import numpy as np
            
def cocktail_shaker_sort(data):
      data_num = len(data)      
      if data_num <2:
           return data 
      
      for m in range(0,int(data_num/2+1)):
              

              for i in range(m,data_num-m-1):
                   if data[i]>data[i+1]:
                           data[i],data[i+1] = data[i+1],data[i]
              print(data)
              for j in range(data_num-m-1,m,-1):
                   if data[j]<data[j-1]:
                           data[j],data[j-1] = data[j-1],data[j]      
              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 =[45, 19, 77, 81, 13, 28, 18, 19, 77]        
         data= cocktail_shaker_sort(data)

answer:

from __future__ import print_function

def cocktail_shaker_sort(unsorted):
    """
    Pure implementation of the cocktail shaker sort algorithm in Python.
    """
    for i in range(len(unsorted)-1, 0, -1):
        swapped = False
        
        for j in range(i, 0, -1):
            if unsorted[j] < unsorted[j-1]:
                unsorted[j], unsorted[j-1] = unsorted[j-1], unsorted[j]
                swapped = True

        for j in range(i):
            if unsorted[j] > unsorted[j+1]:
                unsorted[j], unsorted[j+1] = unsorted[j+1], unsorted[j]
                swapped = True
        
        if not swapped:
            return unsorted
            
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(',')]
    cocktail_shaker_sort(unsorted)
    print(unsorted)

https://github.com/TheAlgorithms/Python/blob/master/sorts/cocktail_shaker_sort.py

 

改:

import numpy as np
            

def cocktail_shaker_sort(data):
      data_num = len(data)      
      if data_num <2:
           return data 
     
      for m in range(0,int(data_num/2+1)):
              aaa = False

              for i in range(m,data_num-m-1):
                   if data[i]>data[i+1]:
                           data[i],data[i+1] = data[i+1],data[i]
                           aaa = True
             # print(data)
              for j in range(data_num-m-1,m,-1):
                   if data[j]<data[j-1]:
                           data[j],data[j-1] = data[j-1],data[j]  
                           aaa = True
              print(data)
              if aaa == False:
                       return 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 =[45, 19, 77, 81, 13, 28, 18, 19, 77]
         data =[ 13,15 , 18, 19, 77,88,99]
         data= cocktail_shaker_sort(data)

 

void SdNavigationHighway::ManageJunctionEvent(int &current_target_junction_index) { current_target_junction_index = -1; /// 基于历史更新本周期的passed int last_passed_junction_index = -1; if (JunctionEvent_last_passed_junction_id_ != 0) { for (unsigned i = 0; i < sd_junction_infos_.size(); i++) { if (JunctionEvent_last_passed_junction_id_ == sd_junction_infos_[i].junction_id) { last_passed_junction_index = i; break; } } } if (last_passed_junction_index != -1) { for (unsigned i = 0; i < sd_junction_infos_.size(); i++) { if (i <= last_passed_junction_index) { sd_junction_infos_[i].has_passed_flag = true; } else { sd_junction_infos_[i].has_passed_flag = false; } } } if (last_passed_junction_index != -1 && last_passed_junction_index + 1 < sd_junction_infos_.size()) { current_target_junction_index = last_passed_junction_index + 1; } else if (last_passed_junction_index == -1 && !sd_junction_infos_.empty()) { current_target_junction_index = 0; } else { current_target_junction_index = -1; } /// 使用min_passed_dist更新本周期passed if (current_target_junction_index != -1) { for (unsigned i = current_target_junction_index; i < sd_junction_infos_.size(); i++) { if (sd_junction_infos_[i].offset < 0) { if (JunctionEvent_min_passed_dist_map_.find(sd_junction_infos_[i].junction_type) != JunctionEvent_min_passed_dist_map_.end()) { if (sd_junction_infos_[i].offset < JunctionEvent_min_passed_dist_map_[sd_junction_infos_[i].junction_type]) { sd_junction_infos_[i].has_passed_flag = true; if (current_target_junction_index + 1 < sd_junction_infos_.size()) { current_target_junction_index += 1; } else { current_target_junction_index = -1; break; } } } } } } /// 使用max_start_effect_dist更新本周期的effected if (current_target_junction_index != -1) { for (unsigned i = current_target_junction_index; i < sd_junction_infos_.size(); i++) { if (JunctionEvent_max_start_effect_dist_map_more_lane_num_.find(sd_junction_infos_[i].junction_type) != JunctionEvent_max_start_effect_dist_map_more_lane_num_.end() && sd_junction_infos_[i].main_road_lane_nums >= 4) { if (sd_junction_infos_[i].offset < JunctionEvent_max_start_effect_dist_map_more_lane_num_[sd_junction_infos_[i].junction_type]) { sd_junction_infos_[i].has_effected_flag = true; } } else if (JunctionEvent_max_start_effect_dist_map_less_lane_num_.find(sd_junction_infos_[i].junction_type) != JunctionEvent_max_start_effect_dist_map_less_lane_num_.end() && (sd_junction_infos_[i].main_road_lane_nums == 1 || sd_junction_infos_[i].main_road_lane_nums == 2)) { if (sd_junction_infos_[i].offset < JunctionEvent_max_start_effect_dist_map_less_lane_num_[sd_junction_infos_[i].junction_type]) { sd_junction_infos_[i].has_effected_flag = true; } } else if (JunctionEvent_max_start_effect_dist_map_.find(sd_junction_infos_[i].junction_type) != JunctionEvent_max_start_effect_dist_map_.end()) { if (sd_junction_infos_[i].offset < JunctionEvent_max_start_effect_dist_map_[sd_junction_infos_[i].junction_type]) { sd_junction_infos_[i].has_effected_flag = true; } } } } /// 针对首个非passed且effected的 RampMerge ApproachRampMerge,更新本周期passed if (current_target_junction_index != -1 && sd_junction_infos_[current_target_junction_index].has_effected_flag && current_target_junction_index + 1 < sd_junction_infos_.size()) { auto &junction_info_effected = sd_junction_infos_[current_target_junction_index]; if (junction_info_effected.offset < -50 && (junction_info_effected.junction_type == JunctionType::RampMerge || junction_info_effected.junction_type == JunctionType::ApproachRampMerge)) { if ((sd_junction_infos_[current_target_junction_index + 1].junction_type == JunctionType::RampInto && sd_junction_infos_[current_target_junction_index + 1].offset < 240) || sd_junction_infos_[current_target_junction_index + 1].offset < 30) { junction_info_effected.has_passed_flag = true; current_target_junction_index += 1; } } } /// 使用感知特征变化 只针对首个非passed且effected 更新本周期的passed if (current_target_junction_index != -1 && sd_junction_infos_[current_target_junction_index].has_effected_flag) { auto &junction_info_effected = sd_junction_infos_[current_target_junction_index]; auto check_static3roadedges_turnRight = [&]() -> bool { if (!junction_info_effected.has_passed_flag) { if (bev_left_road_edge_indexs_.size() == 2 && bev_right_road_edge_indexs_.size() == 1) { if (bev_egoRoadEdgeIndexPair_.first == bev_left_road_edge_indexs_.back() && bev_egoRoadEdgeIndexPair_.second == bev_right_road_edge_indexs_.front()) { auto L1Geos = INTERNAL_PARAMS.raw_bev_data.GetRoadEdgeById(bev_line_sorts_[bev_left_road_edge_indexs_.front()].id)->geos; auto L2Geos = INTERNAL_PARAMS.raw_bev_data.GetRoadEdgeById(bev_line_sorts_[bev_egoRoadEdgeIndexPair_.first].id)->geos; double Gap_L1_L2 = LaneGeometry::GetDistanceBetweenLinesThin(*L1Geos, *L2Geos); if (L2Geos->front().x() - L1Geos->front().x() > 10 && Gap_L1_L2 > 5) { junction_info_effected.has_passed_flag = true; junction_info_effected.has_effected_flag = false; if (current_target_junction_index + 1 < sd_junction_infos_.size()) { current_target_junction_index += 1; } else { current_target_junction_index = -1; } return true; } } } } return false; }; auto check_static3roadedges_turnLeft = [&]() -> bool { if (!junction_info_effected.has_passed_flag) { if (bev_left_road_edge_indexs_.size() == 1 && bev_right_road_edge_indexs_.size() == 2) { if (bev_egoRoadEdgeIndexPair_.second == bev_right_road_edge_indexs_.front() && bev_egoRoadEdgeIndexPair_.first == bev_left_road_edge_indexs_.front()) { auto R1Geos = INTERNAL_PARAMS.raw_bev_data.GetRoadEdgeById(bev_line_sorts_[bev_right_road_edge_indexs_.front()].id)->geos; auto R2Geos = INTERNAL_PARAMS.raw_bev_data.GetRoadEdgeById(bev_line_sorts_[bev_right_road_edge_indexs_.back()].id)->geos; double Gap_R1_R2 = LaneGeometry::GetDistanceBetweenLinesThin(*R1Geos, *R2Geos); if (R1Geos->front().x() - R2Geos->front().x() > 10 && Gap_R1_R2 > 5) { junction_info_effected.has_passed_flag = true; junction_info_effected.has_effected_flag = false; if (current_target_junction_index + 1 < sd_junction_infos_.size()) { current_target_junction_index += 1; } else { current_target_junction_index = -1; } return true; } } } } return false; }; auto check_change_egoleftroadedge = [&]() -> bool { if (!junction_info_effected.has_passed_flag) { if (JunctionEvent_last_junction_bev_feature_.junction_id == junction_info_effected.junction_id && JunctionEvent_last_junction_bev_feature_.junction_type == junction_info_effected.junction_type) { if (bev_egoRoadEdgeIndexPair_.first != -1 && bev_egoRoadEdgeIndexPair_.second != -1) { auto ego_left_road_edge_id = bev_line_sorts_[bev_egoRoadEdgeIndexPair_.first].id; auto ego_right_road_edge_id = bev_line_sorts_[bev_egoRoadEdgeIndexPair_.second].id; if (ego_right_road_edge_id == JunctionEvent_last_junction_bev_feature_.ego_right_road_edge_id && ego_left_road_edge_id != JunctionEvent_last_junction_bev_feature_.ego_left_road_edge_id && JunctionEvent_last_junction_bev_feature_.ego_left_road_edge_id != 0) { auto roadEdge_tmp = INTERNAL_PARAMS.raw_bev_data.GetRoadEdgeById(ego_left_road_edge_id); int nearest_index = roadEdge_tmp->indexed_geos.FindNearestPoint(0, 0); if (nearest_index != -1 && nearest_index < roadEdge_tmp->geos->size() && JunctionEvent_last_junction_bev_feature_.ego_left_road_edge_point.y() - roadEdge_tmp->geos->at(nearest_index).y() > 5) { junction_info_effected.has_passed_flag = true; junction_info_effected.has_effected_flag = false; if (current_target_junction_index + 1 < sd_junction_infos_.size()) { current_target_junction_index += 1; } else { current_target_junction_index = -1; } return true; } } } } } return false; }; auto check_change_egorightroadedge = [&]() -> bool { if (!junction_info_effected.has_passed_flag) { if (JunctionEvent_last_junction_bev_feature_.junction_id == junction_info_effected.junction_id && JunctionEvent_last_junction_bev_feature_.junction_type == junction_info_effected.junction_type) { if (bev_egoRoadEdgeIndexPair_.first != -1 && bev_egoRoadEdgeIndexPair_.second != -1) { auto ego_left_road_edge_id = bev_line_sorts_[bev_egoRoadEdgeIndexPair_.first].id; auto ego_right_road_edge_id = bev_line_sorts_[bev_egoRoadEdgeIndexPair_.second].id; if (ego_right_road_edge_id != JunctionEvent_last_junction_bev_feature_.ego_right_road_edge_id && JunctionEvent_last_junction_bev_feature_.ego_right_road_edge_id != 0 && ego_left_road_edge_id == JunctionEvent_last_junction_bev_feature_.ego_left_road_edge_id) { auto roadEdge_tmp = INTERNAL_PARAMS.raw_bev_data.GetRoadEdgeById(ego_right_road_edge_id); int nearest_index = roadEdge_tmp->indexed_geos.FindNearestPoint(0, 0); if (nearest_index != -1 && nearest_index < roadEdge_tmp->geos->size() && roadEdge_tmp->geos->at(nearest_index).y() - JunctionEvent_last_junction_bev_feature_.ego_right_road_edge_point.y() > 5) { junction_info_effected.has_passed_flag = true; junction_info_effected.has_effected_flag = false; if (current_target_junction_index + 1 < sd_junction_infos_.size()) { current_target_junction_index += 1; } else { current_target_junction_index = -1; } return true; } } } } } return false; }; if (junction_info_effected.junction_type == JunctionType::RampInto) { if (junction_info_effected.offset < 0) { /// 3 road boundary bool res1 = check_static3roadedges_turnRight(); /// ego left roadboundary changed bool res2 = check_change_egoleftroadedge(); // AINFO << "[SDNOA] BevFeatureEvent: " << (int)res1 << "," << (int)res2; } } else if (junction_info_effected.junction_type == JunctionType::RampSplitLeft) { if (junction_info_effected.offset < 10) { bool res1 = check_static3roadedges_turnLeft(); bool res2 = check_change_egorightroadedge(); // AINFO << "[SDNOA] BevFeatureEvent: " << (int)res1 << "," << (int)res2; } } else if (junction_info_effected.junction_type == JunctionType::RampSplitRight) { if (junction_info_effected.offset < 10) { bool res1 = check_static3roadedges_turnRight(); bool res2 = check_change_egoleftroadedge(); // AINFO << "[SDNOA] BevFeatureEvent: " << (int)res1 << "," << (int)res2; } } } /// 更新本周期JunctionBevFeature if (current_target_junction_index != -1 && sd_junction_infos_[current_target_junction_index].has_effected_flag) { auto &junction_info_effected = sd_junction_infos_[current_target_junction_index]; if (bev_egoRoadEdgeIndexPair_.first != -1) { JunctionEvent_last_junction_bev_feature_.left_update_offset = junction_info_effected.offset; JunctionEvent_last_junction_bev_feature_.ego_left_road_edge_id = bev_line_sorts_[bev_egoRoadEdgeIndexPair_.first].id; auto roadEdge_tmp = INTERNAL_PARAMS.raw_bev_data.GetRoadEdgeById(JunctionEvent_last_junction_bev_feature_.ego_left_road_edge_id); int nearest_index = roadEdge_tmp->indexed_geos.FindNearestPoint(0, 0); if (nearest_index != -1 && nearest_index < roadEdge_tmp->geos->size()) { JunctionEvent_last_junction_bev_feature_.ego_left_road_edge_point = roadEdge_tmp->geos->at(nearest_index); } else { JunctionEvent_last_junction_bev_feature_.ego_left_road_edge_point = {0, 0}; } } else { if (junction_info_effected.junction_id != JunctionEvent_last_junction_bev_feature_.junction_id) { JunctionEvent_last_junction_bev_feature_.left_update_offset = 0; JunctionEvent_last_junction_bev_feature_.ego_left_road_edge_id = 0; JunctionEvent_last_junction_bev_feature_.ego_left_road_edge_point = {0, 0}; } } if (bev_egoRoadEdgeIndexPair_.second != -1) { JunctionEvent_last_junction_bev_feature_.right_update_offset = junction_info_effected.offset; JunctionEvent_last_junction_bev_feature_.ego_right_road_edge_id = bev_line_sorts_[bev_egoRoadEdgeIndexPair_.second].id; JunctionEvent_last_junction_bev_feature_.ego_right_road_edge_point = INTERNAL_PARAMS.raw_bev_data.GetRoadEdgeById(JunctionEvent_last_junction_bev_feature_.ego_right_road_edge_id)->geos->back(); } else { if (junction_info_effected.junction_id != JunctionEvent_last_junction_bev_feature_.junction_id) { JunctionEvent_last_junction_bev_feature_.right_update_offset = 0; JunctionEvent_last_junction_bev_feature_.ego_right_road_edge_id = 0; JunctionEvent_last_junction_bev_feature_.ego_right_road_edge_point = {0, 0}; } } JunctionEvent_last_junction_bev_feature_.junction_id = junction_info_effected.junction_id; JunctionEvent_last_junction_bev_feature_.junction_type = junction_info_effected.junction_type; } else { JunctionEvent_last_junction_bev_feature_.junction_id = 0; JunctionEvent_last_junction_bev_feature_.junction_type = JunctionType::Unknow; JunctionEvent_last_junction_bev_feature_.left_update_offset = 0; JunctionEvent_last_junction_bev_feature_.right_update_offset = 0; JunctionEvent_last_junction_bev_feature_.ego_left_road_edge_id = 0; JunctionEvent_last_junction_bev_feature_.ego_left_road_edge_point = {0, 0}; JunctionEvent_last_junction_bev_feature_.ego_right_road_edge_id = 0; JunctionEvent_last_junction_bev_feature_.ego_right_road_edge_point = {0, 0}; } // AINFO << "[SDNOA FEATURE LEFT] id:" << JunctionEvent_last_junction_bev_feature_.junction_id << ",left_id:" << JunctionEvent_last_junction_bev_feature_.ego_left_road_edge_id << ", x:" << // JunctionEvent_last_junction_bev_feature_.ego_left_road_edge_point.x() << ", y:" << JunctionEvent_last_junction_bev_feature_.ego_left_road_edge_point.y(); } 详细解释下,加上注释,解释原理和流程
07-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值