其他语言

本类阅读TOP10

·基于Solaris 开发环境的整体构思
·使用AutoMake轻松生成Makefile
·BCB数据库图像保存技术
·GNU中的Makefile
·射频芯片nRF401天线设计的分析
·iframe 的自适应高度
·BCB之Socket通信
·软件企业如何实施CMM
·入门系列--OpenGL最简单的入门
·WIN95中日志钩子(JournalRecord Hook)的使用

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
2.1 Insertion Sort

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

<< Introduction to algorithms >> ( Second Edition )


2.1 Insertion Sort

We start with insertion sort, which is an efficient algorithm for sorting a small
number of elements. Insertion sor works the way many people sort a hand of
playing cards. We start with an empty left hand and the cards face down a 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 caess held in the left hand are sorted, and these cards
werer originally the top cards of the pile on the table.


Solution:

// 声明:本代码旨在实现原文的思想
// copyleft 2004    http://blog.csdn.net/mskia
// email:    [email protected]

#ifndef    Insertion_Sort_by_mskia
#define    Insertion_Sort_by_mskia

namespace te {
    template< class T >
    void insertion_sort( T *first , T *last ) {
        for ( T *i = first + 1; i <= last; ++i ) {
            T *j = i - 1;
            while ( j >= first && *j > *i ) {
                T t = *i;
                *i = *j;
                *j = t;
                i = j--;
            }
        }
 
        return;
    }
}

#endif




相关文章

相关软件