五种进程调度的算法实现(二)

程序设计

一、数据结构

1.1 事件类型

由于要求是基于事件的进程调度,所以必须创建一个存放事件的队列。

enum EventType{ ARRIVALEVENT, FINISHEVENT, TIMEREVENT };    //事件类型

1.2 任务结构(每一项数据都是输入):

struct Job
{
    char name[50];       //作业名
    int arriveTime;      //作业到达时间
    int needTime;        //作业所需运行时间
    int priority;        //作业优先级,数字越小,优先级越高
};

1.3 事件链表结点:

struct Event
{
    EventType type;            //事件类型
    int jobBeginTime;          //作业开始时间
    bool isFirstExe;           //判断是否第一次执行,用于记录作业开始时间
    int happenTime;            //发生时刻
    int remainTime;            //剩余运行时间
    double hrr;                //最高响应比
    Job job;                   //作业
    Event *next;               //事件指针
};

因为事件为队列存储,因而需要动态增删,所以较佳的数据结构是链表。因为是链表,所以要定义一套操作链表的函数。

二、程序流程图

2.1 非抢占式调度

2.2 抢占式调度

2.3 轮转调度

三、过程定义

3.1 事件队列相关函数

/* 作业复制函数 */
void CopyJob( Job *dest, const Job *sour );
/* 事件队列创建函数
 * 包含头节点
 * 返回头指针 */
Event *CreateEventQueue();
/* 抓取头结点之外的第一个事件
 * 作为返回值返回 */
Event *FetchFirstEvent( Event *head );
/* 如果值相同,时间将插在前方 */
void InsertFinishEvent( Event *head, Event *e );
/* 插入到队尾 */
void InsertTail( Event *head, Event *e );
/* 按作业名称删除第一个匹配项,
 * 删除成功返回TRUE,不存在该节点返回FALSE
 */
bool DeleteByJobName( Event *head, char *jobName );
/* 删除事件队列 */
void DestroyQueue( Event *head );

3.2 进程调度相关函数

/* 插入函数 */
void InsertByHappenTime( Event *head, Event *e );
void InsertByJobTime( Event *head, Event *e );
void InsertByPriority( Event *head, Event *e );
void InsertByRemainTime( Event *head, Event *e );
void InsertByHRR( Event *head, Event *e );
/* 排序函数 */
void SortByHRR( Event *head, int currentTime );
/* 调度函数 */
void SJF ( Event *eventHead );
void SRTF( Event *eventHead );
void HRRF( Event *eventHead );
void Priority(Event *eventHead);    //抢占式优先级调度
void RR  ( Event *eventHead );      //时间片大小为1

程序实现

一、插入函数(用于插入排序)

1.1 按开始时间插入

void InsertByHappenTime( Event *head, Event *e )
{
    if ( head == NULL || e == NULL )
    {
        cerr << "At: " << __FILE__ << "\nLine: " << __LINE__ << endl;
        throw "Head Point is NULL!\n";
    }

    Event *p = head;
    while ( ( p->next != NULL ) && ( e->happenTime >= (p->next)->happenTime ) )
    {
        p = p->next;
    }

    e->next = p->next;
    p->next = e;
}

1.2 按结束时间插入

void InsertFinishEvent( Event *head, Event *e )
{
    if ( head == NULL || e == NULL )
    {
        cerr << "At: " << __FILE__ << "\nLine: " << __LINE__ << endl;
        throw "Head Point is NULL!\n";
    }

    Event *p = head;
    while ( ( p->next != NULL ) && ( e->happenTime > (p->next)->happenTime ) )
    {
        p = p->next;
    }

    e->next = p->next;
    p->next = e;
}

1.3 按作业时间插入

void InsertByJobTime( Event *head, Event *e )
{
    if ( head == NULL || e == NULL )
    {
        cerr << "At: " << __FILE__ << "\nLine: " << __LINE__ << endl;
        throw "Head Point is NULL!\n";
    }

    Event *p = head;
    while ( ( p->next != NULL ) && ( (e->job).needTime >= (p->next)->job.needTime ) )
    {
        p = p->next;
    }

    e->next = p->next;
    p->next = e;
}

1.4 按剩余时间插入

void InsertByRemainTime( Event *head, Event *e )
{
    if ( head == NULL || e == NULL )
    {
        cerr << "At: " << __FILE__ << "\nLine: " << __LINE__ << endl;
        throw "Head Point is NULL!\n";
    }

    Event *p = head;
    while ( ( p->next != NULL ) && ( e->remainTime >= p->next->remainTime ) )
    {
        p = p->next;
    }
    e->next = p->next;
    p->next = e;
}

1.5 按优先级插入

void InsertByPriority( Event *head, Event *e )
{
    if ( head == NULL || e == NULL )
    {
        cerr << "At: " << __FILE__ << "\nLine: " << __LINE__ << endl;
        throw "Head Point is NULL!\n";
    }

    Event *p = head;
    while ( ( p->next != NULL ) && ( e->job.priority >= p->next->job.priority ) )
    {
        p = p->next;
    }
    e->next = p->next;
    p->next = e;
}

1.6 按最高响应比插入

void InsertByHRR( Event *head, Event *e )
{
    if ( head == NULL || e == NULL )
    {
        cerr << "At: " << __FILE__ << "\nLine: " << __LINE__ << endl;
        throw "Head Point is NULL!\n";
    }

    Event *p = head;
    while ( ( p->next != NULL ) && ( e->hrr <= p->next->hrr ) )
    {
        p = p->next;
    }
    e->next = p->next;
    p->next = e;
}

1.7 最高响应比插入排序

void SortByHRR( Event *head, int currentTime )
{
    Event *addrValue = new Event;
    addrValue->next = head->next;
    Event *p = addrValue->next;
    head->next = NULL; //将节点放置另一个链表中
    while ( p != NULL )
    {
        p->happenTime = currentTime;
        p->hrr = 1 + ( p->happenTime - p->job.arriveTime ) / p->job.needTime * 0.1;
        p = p->next;
    }

    while ( p = FetchFirstEvent( addrValue ) )
    {
        InsertByHRR( head, p );
        p = addrValue->next;
    }

    delete addrValue;
    addrValue = NULL;
}

二、调度算法

2.1 最短工作时间优先

void SJF( Event *eventHead )
{
    if ( eventHead == NULL )
    {
        cerr << "At: " << __FILE__ << "\nLine: " << __LINE__ << endl;
        throw "Head Point is NULL!\n";
    }

    int currentTime = 0;            //当前时间
    bool  isCpuBusy = false;        //CPU忙碌状态
    Event *readyQueue = new Event;  //包含头结点,就绪队列
    readyQueue->next = NULL;

    while ( eventHead->next != NULL )
    {
        Event *firstEvent = FetchFirstEvent( eventHead );
        currentTime = firstEvent->happenTime;

        if ( firstEvent->type == ARRIVALEVENT )
        {
            if ( isCpuBusy == true )
            {
                InsertByJobTime( readyQueue, firstEvent );
            }
            else
            {
                isCpuBusy = true;

                Event *finishEvent = new Event;
                finishEvent->type = FINISHEVENT;
                finishEvent->jobBeginTime = currentTime;
                finishEvent->happenTime = currentTime + firstEvent->job.needTime;
                finishEvent->remainTime = 0;
                CopyJob( &(finishEvent->job),  &(firstEvent->job) );

                if ( firstEvent != NULL )
                    delete firstEvent;//删除正在执行事件节点
                firstEvent = NULL;

                InsertByHappenTime( eventHead, finishEvent );
            }
            continue ;
        }

        if ( firstEvent->type ==  FINISHEVENT )
        {
            ShowEvent( firstEvent );
            if ( firstEvent != NULL )
                delete firstEvent;//删除已执行事件节点
            firstEvent = NULL;

            isCpuBusy = false;
            Event *shortestEvent = FetchFirstEvent( readyQueue );

            if ( shortestEvent != NULL )
            {
                isCpuBusy = true;

                Event *finishEvent = new Event();
                finishEvent->type = FINISHEVENT;
                finishEvent->jobBeginTime = currentTime;
                finishEvent->happenTime = currentTime + shortestEvent->job.needTime;
                finishEvent->remainTime = shortestEvent->job.needTime;
                CopyJob( &(finishEvent->job),  &(shortestEvent->job) );

                if ( shortestEvent != NULL )
                    delete shortestEvent;//删除正在执行事件节点
                shortestEvent = NULL;

                InsertByHappenTime( eventHead, finishEvent );
            }
        }
    }
}

2.2 最短剩余时间优先

void SRTF( Event *eventHead )
{
    if ( eventHead == NULL )
    {
        cerr << "At: " << __FILE__ << "\nLine: " << __LINE__ << endl;
        throw "Head Point is NULL!\n";
    }

    int currentTime = 0;
    bool  isCpuBusy = false;
    Event *readyQueue = new Event;
    readyQueue->next = NULL;
    Event *runEvent = NULL;            //正在运行事件节点

    while ( eventHead->next != NULL )
    {
        Event *firstEvent = FetchFirstEvent( eventHead );
        int frontTime = currentTime;                      //上次事件发生时间
        currentTime = firstEvent->happenTime;

        if ( firstEvent->type == ARRIVALEVENT )
        {
            if ( isCpuBusy == true )
            {
                runEvent->happenTime = currentTime;
                runEvent->remainTime -= (currentTime - frontTime);//剩余时间 = 运行时间- 已运行时间(本次-上次时间)
                if ( firstEvent->remainTime < runEvent->remainTime )
                {                                                        //抢断
                    DeleteByJobName( eventHead, runEvent->job.name );    //删除上次事件的结束事件,PS.若上次事件先插入,将会被删除
                    InsertByRemainTime( readyQueue, runEvent );          //上次事件加入就绪队列

                    runEvent = firstEvent;                               //上次事件已插入继续队列,无须释放空间,更新本次事件
                    runEvent->isFirstExe = false;                        //

                    Event *finishEvent = new Event;
                    finishEvent->type = FINISHEVENT;
                    finishEvent->jobBeginTime = runEvent->jobBeginTime;//
                    finishEvent->happenTime = currentTime + runEvent->remainTime;
                    finishEvent->remainTime = 0;
                    CopyJob( &(finishEvent->job),  &(runEvent->job) );
                    InsertByHappenTime( eventHead, finishEvent );      //插入结束事件
                }
                else
                {
                    InsertByRemainTime( readyQueue, firstEvent );//等待,加入就绪事件队列
                }
            }

            if ( isCpuBusy == false )
            {
                isCpuBusy = true;
                firstEvent->isFirstExe = false;

                Event *finishEvent = new Event;
                finishEvent->type = FINISHEVENT;
                finishEvent->jobBeginTime = firstEvent->jobBeginTime;    //
                finishEvent->isFirstExe = false;
                finishEvent->happenTime = currentTime + firstEvent->remainTime;
                finishEvent->remainTime = 0;
                CopyJob( &(finishEvent->job),  &(firstEvent->job) );

                runEvent = firstEvent;

                InsertByHappenTime( eventHead, finishEvent );
            }

            continue ;
        }

        if ( firstEvent->type ==  FINISHEVENT )
        {
            ShowEvent( firstEvent );
            if ( runEvent != NULL )        //删除已运行事件
                delete runEvent;
            runEvent = NULL;

            isCpuBusy = false;
            Event *remainTimeEvent = FetchFirstEvent( readyQueue );

            if ( remainTimeEvent != NULL )
            {
                isCpuBusy = true;

                if ( remainTimeEvent->isFirstExe )    //在就绪队列中,若作业首次执行,必然是延迟发生的
                     remainTimeEvent->jobBeginTime = currentTime;
                remainTimeEvent->isFirstExe = false;

                Event *finishEvent = new Event();
                finishEvent->type = FINISHEVENT;
                finishEvent->jobBeginTime = remainTimeEvent->jobBeginTime;
                finishEvent->happenTime = currentTime + remainTimeEvent->remainTime;
                finishEvent->remainTime = 0;
                CopyJob( &(finishEvent->job),  &(remainTimeEvent->job) );

                runEvent = remainTimeEvent;
                InsertByHappenTime( eventHead, finishEvent );
             }
        }
    }
}

2.3 时间片轮转(时间片大小为1)

void RR( Event *eventHead )
{
    if ( eventHead == NULL )
    {
        cerr << "At: " << __FILE__ << "\nLine: " << __LINE__ << endl;
        throw "Head Point is NULL!\n";
    }

    int currentTime = 0;
    bool isCpuBusy = false;
    Event *readyQueue = new Event;
    readyQueue->next = NULL;

    while ( eventHead->next != NULL )
    {
        Event *firstEvent = FetchFirstEvent( eventHead );
        if ( firstEvent->happenTime > currentTime )
            currentTime = firstEvent->happenTime;

        if ( firstEvent->type == ARRIVALEVENT )
        {
            if ( isCpuBusy )
            {
                InsertTail( readyQueue, firstEvent );
            }
            else
            {
                isCpuBusy = true;
                Event *nextEvent = new Event;
                nextEvent->jobBeginTime = firstEvent->jobBeginTime;
                CopyJob( &(nextEvent->job),  &(firstEvent->job) );
                if ( firstEvent->remainTime <= TIMER )    //FinishEvent
                {
                    nextEvent->type = FINISHEVENT;
                    nextEvent->happenTime = currentTime + firstEvent->remainTime;
                    nextEvent->remainTime = 0;
                    InsertFinishEvent( eventHead, nextEvent );
                }
                else    //TimerEvent
                {
                    nextEvent->type = TIMEREVENT;
                    nextEvent->happenTime = currentTime + TIMER;
                    nextEvent->remainTime = firstEvent->remainTime - TIMER;
                    InsertByHappenTime( eventHead, nextEvent );
                }

                if ( firstEvent != NULL )
                    delete firstEvent;//删除正在执行事件节点
                firstEvent = NULL;
            }
            continue ;
        }

        if ( firstEvent->type == TIMEREVENT )
        {
            InsertTail( readyQueue, firstEvent );
        }

        int isFinish = false;
        if ( firstEvent->type ==  FINISHEVENT )
        {
            ShowEvent( firstEvent );
            if ( firstEvent != NULL )
                delete firstEvent;//删除已执行事件节点
            firstEvent = NULL;

            isFinish = true;
        }

        while ( firstEvent = FetchFirstEvent( readyQueue ) )
        {
            if ( isFinish )
                isCpuBusy = true, isFinish = false;
            Event *nextEvent = new Event;
            if ( firstEvent->type == ARRIVALEVENT )
                nextEvent->jobBeginTime = currentTime;
            else if ( firstEvent->type == TIMEREVENT )
                nextEvent->jobBeginTime = firstEvent->jobBeginTime;
            CopyJob( &(nextEvent->job),  &(firstEvent->job) );
            if ( firstEvent->remainTime <= TIMER )    //FinishEvent
            {
                nextEvent->type = FINISHEVENT;
                nextEvent->happenTime = currentTime + firstEvent->remainTime;
                nextEvent->remainTime = 0;
                InsertFinishEvent( eventHead, nextEvent );
            }
            else    //TimerEvent
            {
                nextEvent->type = TIMEREVENT;
                nextEvent->happenTime = currentTime + TIMER;
                nextEvent->remainTime = firstEvent->remainTime - TIMER;
                InsertByHappenTime( eventHead, nextEvent );
            }

            currentTime = nextEvent->happenTime;

            if ( firstEvent != NULL )
                delete firstEvent;//删除正在执行事件节点
            firstEvent = NULL;
        }
    }
}

2.4 抢占式优先级调度

void Priority( Event *eventHead )
{
    if ( eventHead == NULL )
    {
        cerr << "At: " << __FILE__ << "\nLine: " << __LINE__ << endl;
        throw "Head Point is NULL!\n";
    }

    int currentTime = 0;
    bool  isCpuBusy = false;
    Event *readyQueue = new Event;
    readyQueue->next = NULL;
    Event *runEvent = NULL;            //正在运行事件节点

    while ( eventHead->next != NULL )
    {
        Event *firstEvent = FetchFirstEvent( eventHead );
        int frontTime = currentTime;                      //上次事件发生时间
        currentTime = firstEvent->happenTime;

        if ( firstEvent->type == ARRIVALEVENT )
        {
            if ( isCpuBusy == true )
            {
                runEvent->happenTime = currentTime;
                runEvent->remainTime -= (currentTime - frontTime);//剩余时间 = 运行时间- 已运行时间(本次-上次时间)
                if ( firstEvent->job.priority < runEvent->job.priority )
                {                                                      //抢断
                    DeleteByJobName( eventHead, runEvent->job.name ); //删除上次事件的结束事件,PS.若上次事件先插入,将会被删除
                    InsertByPriority( readyQueue, runEvent );          //上次事件加入就绪队列

                    runEvent = firstEvent;                              //上次事件已插入继续队列,无须释放空间,更新本次事件
                    runEvent->isFirstExe = false;                      //

                    Event *finishEvent = new Event;
                    finishEvent->type = FINISHEVENT;
                    finishEvent->jobBeginTime = runEvent->jobBeginTime;//
                    finishEvent->happenTime = currentTime + runEvent->remainTime;
                    finishEvent->remainTime = 0;
                    CopyJob( &(finishEvent->job),  &(runEvent->job) );
                    InsertByHappenTime( eventHead, finishEvent );      //插入结束事件
                }
                else
                {
                    InsertByRemainTime( readyQueue, firstEvent );//等待,加入就绪事件队列
                }
            }

            if ( isCpuBusy == false )
            {
                isCpuBusy = true;
                firstEvent->isFirstExe = false;

                Event *finishEvent = new Event;
                finishEvent->type = FINISHEVENT;
                finishEvent->jobBeginTime = firstEvent->jobBeginTime;
                finishEvent->happenTime = currentTime + firstEvent->remainTime;
                finishEvent->remainTime = 0;
                CopyJob( &(finishEvent->job),  &(firstEvent->job) );

                runEvent = firstEvent;

                InsertByHappenTime( eventHead, finishEvent );
            }

            continue ;
        }

        if ( firstEvent->type ==  FINISHEVENT )
        {
            ShowEvent( firstEvent );
            if ( runEvent != NULL )        //删除已运行事件
                delete runEvent;
            runEvent = NULL;

            isCpuBusy = false;
            Event *priorityEvent = FetchFirstEvent( readyQueue );

            if ( priorityEvent != NULL )
            {
                isCpuBusy = true;

                if ( priorityEvent->isFirstExe )    //在就绪队列中,若作业首次执行,必然是延迟发生的
                    priorityEvent->jobBeginTime = currentTime;

                Event *finishEvent = new Event();
                finishEvent->type = FINISHEVENT;
                finishEvent->jobBeginTime = priorityEvent->jobBeginTime;
                finishEvent->happenTime = currentTime + priorityEvent->remainTime;
                finishEvent->remainTime = 0;
                CopyJob( &(finishEvent->job),  &(priorityEvent->job) );

                runEvent = priorityEvent;
                InsertByHappenTime( eventHead, finishEvent );
            }
        }
    }
}

2.5 最高响应比优先

void HRRF( Event *eventHead )
{
    if ( eventHead == NULL )
    {
        cerr << "At: " << __FILE__ << "\nLine: " << __LINE__ << endl;
        throw "Head Point is NULL!\n";
    }

    int currentTime = 0;            //当前时间
    bool  isCpuBusy = false;        //CPU忙碌状态
    Event *readyQueue = new Event;  //包含头结点,就绪队列
    readyQueue->next = NULL;

    while ( eventHead->next != NULL )
    {
        Event *firstEvent = FetchFirstEvent( eventHead );
        currentTime = firstEvent->happenTime;

        if ( firstEvent->type == ARRIVALEVENT )
        {
            if ( isCpuBusy == true )
            {
                InsertTail( readyQueue, firstEvent );
            }
            else
            {
                isCpuBusy = true;

                Event *finishEvent = new Event;
                finishEvent->type = FINISHEVENT;
                finishEvent->jobBeginTime = currentTime;
                finishEvent->happenTime = currentTime + firstEvent->job.needTime;
                finishEvent->remainTime = 0;
                CopyJob( &(finishEvent->job),  &(firstEvent->job) );

                if ( firstEvent != NULL )
                    delete firstEvent;//删除正在执行事件节点
               firstEvent = NULL;

                InsertByHappenTime( eventHead, finishEvent );
            }
            continue ;
        }

        if ( firstEvent->type ==  FINISHEVENT )
        {
            ShowEvent( firstEvent );
            if ( firstEvent != NULL )
                delete firstEvent;//删除已执行事件节点
            firstEvent = NULL;

            isCpuBusy = false;
            SortByHRR( readyQueue, currentTime );
            Event *hrrEvent = FetchFirstEvent( readyQueue );

            if ( hrrEvent != NULL )
            {
                isCpuBusy = true;
                Event *finishEvent = new Event();
                finishEvent->type = FINISHEVENT;
                finishEvent->jobBeginTime = currentTime;
                finishEvent->happenTime = currentTime + hrrEvent->job.needTime;
                finishEvent->remainTime = hrrEvent->job.needTime;
                CopyJob( &(finishEvent->job),  &(hrrEvent->job) );

                if ( hrrEvent != NULL )
                    delete hrrEvent;//删除正在执行事件节点
                  hrrEvent = NULL;

                InsertByHappenTime( eventHead, finishEvent );
            }
        }
    }
}

测试数据


进程


到达时间


服务时间


优先级


A


0


3


5


B


2


6


2


C


4


4


3


D


6


5


4


E


8


2


1

运行结果

一、最短作业时间优先

二、最短剩余时间优先

三、最高响应比优先

四、优先级调度

五、时间片轮转

时间片=1

时间片=4

总结与体会

根据流程图,一步一步实现功能,思路清晰,就不会出错。PS:C#程序的可视化及其调度算法采用LINQ实现,将在下章讲解。

时间: 2024-10-10 12:25:15

五种进程调度的算法实现(二)的相关文章

五种进程调度的算法实现(一)

实验要求 1.基于Event-Driven(事件驱动)实现模拟进程调度,包括 最短工作优先(SJF): 最短剩余时间优先(SRTF): 最高响应比优先(HRRF): 优先级调度(Priority): 轮转调度(RR). 其中,SJF.SRTF为非抢占式调度,其余为抢占式调度. 2.要求用C语言实现这五种调度算法.(方便起见,引入了C++头文件使用cout进行输出) 基础知识 一.进程 1.1 进程的含义 广义地说,进程是一个具有独立功能的程序关于某个数据集合的一次运行活动. 进程是一种抽象的概念

五种常用的算法设计技巧之二:分治算法

一,介绍 分治算法主要包含两个步骤:分.治.分,就是递归地将原问题分解成小问题:治则是:在解决了各个小问题之后(各个击破之后)合并小问题的解,从而得到整个问题的解 二,分治递归表达式 分治算法一般都可以写出一个递归表达式:比如经典的归并排序的递归表达式:T(N)=2T(N/2)+O(N) T(N)代表整个原问题,采用了分治解决方案后,它可以表示成: ①分解成了两个规模只有原来一半(N/2)的子问题:T(N/2) ②当解决完这两个子问题T(N/2)之后,再合并这两个子问题需要的代价是 O(N) 递

Android系统的五种数据存储形式(二)

之前介绍了Android系统下三种数据存储形式,今天补充介绍另外两种,分别是内容提供者和网络存储.有些人可能认为内存提供者和网络存储更偏向于对数据的操作而不是数据的存储,但这两种方式确实与数据有关,所以这里还是将这两种形式简要的说明一下. Content Provider: Content Provider,中文名是内存提供者,Android四大组件之一,内容提供者是应用程序之间共享数据的接口,以数据库形式存入手机内存,可以共享自己的数据给其他应用使用.之所以需要设计一个单独的控件来操作数据,是

加密解密五种算法的实现

1.  实验目的 通过五种加密解密算法的分析与设计,用高级语言实现加密解密过程.通过实验过程理解系统中加密解密的基本思想和实现方法. 2.  实验基本原理与方法 ①单字母替换加密方法——恺撒密码 加密方法是把英文字母按字母表的顺序编号作为明文,将密钥定为m,加密算法为将明文加上密钥m,得到密码表,通过相反的过程由密文得到明文. ②单字母替换加密方法——字母倒排序 在加密.解密的过程中明文和密文按照字母表的顺序倒排对应,即A对应Z,B对应Y. ③单字母替换加密方法——单表置换密码       由密

每日算法之二十五:Divide Two Integers

Divide two integers without using multiplication, division and mod operator. 不使用乘法.除法和求模运算求两个数相除. class Solution { public: long long internalDivide(unsigned long long dividend,unsigned long long divisor) { if(dividend<divisor) return 0; int result =

关于心理的二十五种倾向(查理&amp;#183;芒格)-2

5)避免不一致倾向避免不一致倾向实际上就是人天生就害怕改变.相同是由于人类大脑的生理机制决定的.由于这样的倾向能够带来节省运算空间和能量的优点.这样的抗改变模式的形成,可能的原因例如以下:A) 迅速作出决定对生存来说至关重要,而这样的抗改变模式有助于更快的作出决定;     这里有第四个倾向的因素,对于生存至关重要的反应(遇到生存威胁时),反应一定要快,必须形成固化的模式.这也是我们对于紧急事件的处理常常採用不断反复模拟训练的原因.B) 能够通过群体协作来获得生存优势,假设每一个人总是不停地改变

关于心理的二十五种倾向(查理&amp;#183;芒格)-3

9)回馈倾向人们早就发现.和猿类,猴类,狗类等其它很多认知能力较为低下的物种同样,人类身上也有以德报德,以牙还牙的极端倾向:这样的倾向明显能够促进有利于成员利益的团体合作.这跟非常多社会性的动物的基因程序非常类似.回馈倾向是群体生活进行协作的基础. 以牙还牙非常正常,非常普遍.那以德报怨呢?A)大自然并没有普遍的法则使得动物内部以德报怨的行为能推动物种的繁荣:B)假设一个国家对外交往放弃以牙还牙的做法,这个国家是否有好的前景是不确定的:(说的是中国和日本?)C)假设国与国之间都觉得以德报怨是最好

算法整理(二)---快速排序的两种实现方式:双边扫描和单边扫描

首先简单谈下快速排序的特点,时间复杂度O(nLog n),最差时间复杂度O(n^2),平均时间O(nLog n).因为用到了函数栈,空间复杂度为O(lg n),最差为O(n).是一种不稳定的排序方法.基本思想是分治法,这位大大的http://blog.csdn.net/morewindows/article/details/6684558 讲的非常清楚了,分治法+挖坑法,我就不多说了.就是以某个数为参照,使得左边的都小于他,右边的数都大于他.然后对他的左右两个区间采取同样的方法进行递归. 就其整

算法整理(二)---高速排序的两种实现方式:双边扫描和单边扫描

首先简单谈下高速排序的特点,时间复杂度O(nLog n),最差时间复杂度O(n^2),平均时间O(nLog n).由于用到了函数栈,空间复杂度为O(lg n),最差为O(n).是一种不稳定的排序方法.基本思想是分治法,这位大大的http://blog.csdn.net/morewindows/article/details/6684558 讲的很清楚了,分治法+挖坑法,我就不多说了.就是以某个数为參照,使得左边的都小于他,右边的数都大于他.然后对他的左右两个区间採取相同的方法进行递归. 就其总体