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

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;
}

这里写图片描述

内容概要:该论文深入研究了液压挖掘机动臂下降势能回收技术,旨在解决传统液压挖掘机能耗高的问题。提出了一种新型闭式回路势能回收系统,利用模糊PI自整定控制算法控制永磁无刷直流电动机,实现了变转速容积调速控制,消除了节流和溢流损失。通过建立数学模型和仿真模型,分析了不同负载下的系统性能,并开发了试验平台验证系统的高效性和节能效果。研究还涵盖了执行机构能量分布分析、系统元件参数匹配及电机控制性能优化,为液压挖掘机节能技术提供了理论和实践依据。此外,通过实验验证,该系统相比传统方案可降低28%的能耗,控制系统响应时间缩短40%,为工程机械的绿色化、智能化发展提供了关键技术支撑。 适合人群:从事工程机械设计、制造及维护的工程师和技术人员,以及对液压系统节能技术感兴趣的科研人员。 使用场景及目标:①理解液压挖掘机闭式回路动臂势能回收系统的原理和优势;②掌握模糊PI自整定控制算法的具体实现;③学习如何通过理论建模、仿真和实验验证来评估和优化液压系统的性能。 其他说明:此研究不仅提供了详细的理论分析和数学建模,还给出了具体的仿真代码和实验数据,便于读者在实际工作中进行参考和应用。研究结果表明,该系统不仅能显著提高能源利用效率,还能延长设备使用寿命,降低维护成本,具有重要的工程应用价值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值