【算法】插入排序 insert sort algorithm

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void main()
{
        int a[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
        int key = 0;
        int j = 0;
        for (j = 1; j < sizeof(a)/sizeof(int); j++)
        {
                key = a[j];
                int i = j-1;
                while ((i>=0)&&(a[i]>key))
                {
                        a[i+1]=a[i];
                        i--;
                }
                a[i+1] = key;
        }

        for (j=0; j < sizeof(a)/sizeof(int); j++)
        {
                printf("%2d\n", a[j]);
        }
        return;
}

以下是一个示例的自适应滑动窗算法航班排序的 Python 代码: ```python def adaptive_sliding_window_sort(flights, window_size=5, tolerance=2): """ Sorts a list of flights using an adaptive sliding window algorithm. :param flights: A list of Flight objects to be sorted :param window_size: The size of the sliding window (default is 5) :param tolerance: The tolerance level for the window (default is 2) :return: A sorted list of Flight objects """ sorted_flights = [flights[0]] # Start with the first flight already sorted for i in range(1, len(flights)): curr_flight = flights[i] # Find the best position for the current flight in the sorted list best_pos = find_best_position(sorted_flights, curr_flight, window_size, tolerance) # Insert the current flight into the sorted list at the best position sorted_flights.insert(best_pos, curr_flight) return sorted_flights def find_best_position(sorted_flights, curr_flight, window_size, tolerance): """ Finds the best position for the current flight in the sorted list using a sliding window algorithm. :param sorted_flights: A list of Flight objects already sorted :param curr_flight: The current Flight object to be inserted into the sorted list :param window_size: The size of the sliding window :param tolerance: The tolerance level for the window :return: The index of the best position for the current flight in the sorted list """ best_pos = len(sorted_flights) # Start with the end of the sorted list as the best position min_distance = float('inf') # Start with the maximum possible distance for i in range(len(sorted_flights) - window_size, len(sorted_flights)): # Calculate the distance between the current flight and each flight in the sliding window distance = calc_distance(curr_flight, sorted_flights[i]) # If the distance is within the tolerance level and smaller than the minimum distance so far, # update the minimum distance and the best position accordingly if distance <= tolerance and distance < min_distance: min_distance = distance best_pos = i return best_pos def calc_distance(flight1, flight2): """ Calculates the distance between two Flight objects based on their departure and arrival times. :param flight1: The first Flight object :param flight2: The second Flight object :return: The absolute difference between the departure time of flight1 and the arrival time of flight2 """ return abs(flight1.departure_time - flight2.arrival_time) class Flight: """ A simple Flight class with departure and arrival times. """ def __init__(self, departure_time, arrival_time): self.departure_time = departure_time self.arrival_time = arrival_time ``` 该算法的主要思路是,从前往后遍历航班列表,对于每个航班,找到在已排序的航班列表中最适合它的位置,并将其插入该位置。找到最适合位置的方法是使用滑动窗口算法,在已排序的航班列表中取最后 $n$ 个航班作为滑动窗口,并计算当前航班与滑动窗口中每个航班的时间差,如果时间差小于等于容忍度 $t$,则认为当前航班可以插入到这个位置。在滑动窗口中找到多个可插入位置时,选择时间差最小的位置作为最适合位置。 注意,该算法中的航班类 `Flight` 只包含了起飞时间和降落时间两个属性,实际应用中可能需要更多信息。另外,该算法只考虑了航班的起飞时间和降落时间,没有考虑到其他因素(如航班时长、中转时间等)可能对排序的影响。因此,该算法只是一个简单的示例,实际应用时需要根据具体情况进行调整和改进。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值