Chapter 2 --- insertion-sort, merge-sort (INTRODUCTION TO ALGORITHMS THIRD EDITION)

本文介绍了两种经典的排序算法:插入排序和归并排序。插入排序适用于小规模数据集,通过将每个元素插入到已排序序列中的正确位置来实现排序。归并排序采用分治策略,递归地将序列分为两半,再将两个有序子序列合并为一个有序序列。文章还提供了这两种排序算法的C++实现。

INSERTION-SORT

We start with insertion sort, which is an efficient algorithm for sorting a small
number of elements. Insertion sort works the way many people sort a hand of
playing cards. We start with an empty left hand and the cards face down on the
table. We then remove one card at a time from the table and insert it into the
correct position in the left hand. To find the correct position for a card, we compare
it with each of the cards already in the hand, from right to left, as illustrated in
Figure 2.1. At all times, the cards held in the left hand are sorted, and these cards
were originally the top cards of the pile on the table.

这里写图片描述
这里写图片描述

这里写图片描述

#include<iostream>

using namespace std;


void insertionSort(int a[],int aLen){
    int i,j,key;
    for(int i=1;i<aLen;i++){
        key=a[i];
        j=i-1;
        while(j>=0 && key<a[j]){
            a[j+1]=a[j];
            j--;
        }
        a[j+1]=key;
    }
}

void print(int a[],int aLen){
    for(int i=0;i<aLen;i++){
        cout<<a[i]<<" ";
    }
    cout<<endl;
}

int main()
{
    int a[6]={5,2,4,6,1,3};
    cout<<"排序前:";
    print(a,6);
    cout<<"排序后:";
    insertionSort(a,6);
    print(a,6);
    return 0;
}

这里写图片描述

The divide-and-conquer approach—— merge sort

The divide-and-conquer paradigm involves three steps at each level of the recur-
sion:
Divide the problem into a number of subproblems that are smaller instances of the same problem.
Conquer the subproblems by solving them recursively. If the subproblem sizes are small enough, however, just solve the subproblems in a straightforward manner.
Combine the solutions to the subproblems into the solution for the original problem.

merge-sort

The merge sort algorithm closely follows the divide-and-conquer paradigm. In-
tuitively, it operates as follows.
Divide: Divide the n-element sequence to be sorted into two subsequences of n/2 elements each.
Conquer: Sort the two subsequences recursively using merge sort.
Combine: Merge the two sorted subsequences to produce the sorted answer.

The recursion “bottoms out” when the sequence to be sorted has length 1, in which
case there is no work to be done, since every sequence of length 1 is already in
sorted order.

Returning to our card-playing motif, suppose we have two piles of cards face up on a table. Each pile is sorted, with the smallest cards on top. We wish to merge the two piles into a single sorted output pile, which is to be face down on the table. Our basic step consists of choosing the smaller of the two cards on top of the face-up piles, removing it from its pile (which exposes a new top card), and placing this card face down onto the output pile. We repeat this step until one input pile is empty, at which time we just take the remaining input pile and place it face down onto the output pile.

#include<iostream>

using namespace std;


//把数组st到mid部分(包含mid)和mid(不包含mid)到en部分进行合并
void merge(int a[],int st,int mid,int en){
    int n1=mid-st+1;
    int n2=en-mid;
    int *l=new int[n1];
    int *r=new int[n2];
    for(int i=0;i<n1;i++){
        l[i]=a[st+i];
    }
    for(int i=0;i<n2;i++){
        r[i]=a[mid+1+i];
    }
    int j1=0,j2=0,k=st;
    while(j1<n1 && j2<n2 && k<=en){
        if(l[j1]<r[j2]){
            a[k++]=l[j1++];
        }
        else{
            a[k++]=r[j2++];
        }
    }
    while(j1<n1){
        a[k++]=l[j1++];
    }
    while(j2<n2){
        a[k++]=r[j2++];
    }
}

void merge_sort(int a[],int st,int en){
    if(st<en){
        int mid=(st+en)/2;
        merge_sort(a,st,mid);
        merge_sort(a,mid+1,en);
        merge(a,st,mid,en);
    }
}

void print(int a[],int aLen){
    for(int i=0;i<aLen;i++){
        cout<<a[i]<<" ";
    }
    cout<<endl;
}

int main()
{
    int a[6]={5,2,4,6,1,3};
    cout<<"Before sorting: ";
    print(a,6);
    cout<<"After merge-sorting: ";
    merge_sort(a,0,6-1);
    print(a,6);
    return 0;
}

这里写图片描述

【四轴飞行器】非线性三自由度四轴飞行器模拟器研究(Matlab代码实现)内容概要:本文围绕非线性三自由度四轴飞行器的建模与仿真展开,重点介绍了基于Matlab的飞行器动力学模型构建与控制系统设计方法。通过对四轴飞行器非线性运动方程的推导,建立其在三维空间中的姿态与位置动态模型,并采用数值仿真手段实现飞行器在复杂环境下的行为模拟。文中详细阐述了系统状态方程的构建、控制输入设计以及仿真参数设置,并结合具体代码实现展示了如何对飞行器进行稳定控制与轨迹跟踪。此外,文章还提到了多种优化与控制策略的应用背景,如模型预测控制、PID控制等,突出了Matlab工具在无人机系统仿真中的强大功能。; 适合人群:具备一定自动控制理论基础和Matlab编程能力的高校学生、科研人员及从事无人机系统开发的工程师;尤其适合从事飞行器建模、控制算法研究及相关领域研究的专业人士。; 使用场景及目标:①用于四轴飞行器非线性动力学建模的教学与科研实践;②为无人机控制系统设计(如姿态控制、轨迹跟踪)提供仿真验证平台;③支持高级控制算法(如MPC、LQR、PID)的研究与对比分析; 阅读建议:建议读者结合文中提到的Matlab代码与仿真模型,动手实践飞行器建模与控制流程,重点关注动力学方程的实现与控制器参数调优,同时可拓展至多自由度或复杂环境下的飞行仿真研究。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值