algorithm ch2 insertsort

刚开始看到insertsort,思路就是使用新来的元素与前述已经排好序的元素比较。然后进行插入或者跳到下一次比较。

实现的代码如下:

void InsertSort(int *pArray, int iSortNum)
{
    int *pTemp = pArray;
    int iLoop = 0;
    int jLoop = 1;

    for(; jLoop != iSortNum ; ++jLoop)
    {
        int x = pTemp[jLoop];
        iLoop = jLoop - 1;
        while(iLoop != -1)
        {
            if(x < pTemp[iLoop])
            {
                pTemp[iLoop+1] = pTemp[iLoop];
            }
            --iLoop;
        }
    }
}

这种排序跟冒泡法时间复杂度都是o(n^2)。

下面附上冒泡法的程序:

void BubbleSort(int *pArray, int iArrayNum)
{
    int temp = 0;
    for(int i = 0; i != iArrayNum;  ++i)
    {
        for(int j = i + 1; j != iArrayNum; ++ j)
        {
            if(pArray[i] > pArray[j])
            {
                temp = pArray[i];
                pArray[i] = pArray[j];
                pArray[j] = temp;
            }
        }
    }
}
时间: 2024-07-29 18:37:04

algorithm ch2 insertsort的相关文章

java面试题003

各种排序算法:冒择路(入)兮(稀)快归堆,桶式排序,基数排序 冒泡排序,选择排序,插入排序,稀尔排序,快速排序,归并排序,堆排序,桶式排序,基数排序 一.冒泡排序(BubbleSort) 1. 基本思想: 两两比较待排序数据元素的大小,发现两个数据元素的次序相反时即进行交换,直到没有反序的数据元素为止. 2. 排序过程: 设想被排序的数组R[1..N]垂直竖立,将每个数据元素看作有重量的气泡,根据轻气泡不能在重气泡之下的原则,从下往上扫描数组R,凡扫描到违反本原则的轻气泡,就使其向上”漂浮”,如

Java Sort算法

//插入排序: package org.rut.util.algorithm.support; import org.rut.util.algorithm.SortUtil; /** * @author treeroot * @since 2006-2-2 * @version 1.0 */ public class InsertSort implements SortUtil.Sort{ /** (non-Javadoc) * @see org.rut.util.algorithm.SortU

用Java实现几种常见的排序算法

用Java语言实现的各种排序,包括插入排序.冒泡排序.选择排序.Shell排序.快速排序.归并排序.堆排序.SortUtil等. 插入排序: package org.rut.util.algorithm.support; import org.rut.util.algorithm.SortUtil; /** * @author treeroot * @since 2006-2-2 * @version 1.0 */ public class InsertSort implements SortU

Java算法-希尔排序

希尔排序的诞生是由于插入排序在处理大规模数组的时候会遇到需要移动太多元素的问题.希尔排序的思想是将一个大的数组“分而治之”,划分为若干个小的数组,以 gap 来划分,比如数组 [1, 2, 3, 4, 5, 6, 7, 8] ,如果以 gap = 2 来划分,可以分为 [1, 3, 5, 7] 和 [2, 4, 6, 8] 两个数组(对应的,如 gap = 3 ,则划分的数组为: [1, 4, 7] . [2, 5, 8] . [3, 6] )然后分别对划分出来的数组进行插入排序,待各个子数组排

面试题12月28-积累篇

1.简述synchroized和java.util.concurrent.locks.Lock的异同? 1.synchronized 用在方法和代码块的区别? a. 可以只对需要同步的使用 b.与wait(),notify()和notifyall()方法使用比较方便 2.wait() a.释放持有的对象锁,线程进入等待池,释放cpu,其他正在等待的线程可以获得锁,而sleep方法,线程会休眠一段时间,线程不会释放锁. 3.ReentrantLock 还包括了中断锁等待和定时锁等待, 在并发量小的

hihoCoder #1198 Memory Allocating Algorithm

Description Little Hi is studying on memory allocating algorithms this summer. He starts his experiments with a very simple algorithm. By this algorithm memory is considered as a sequence of M consecutive storage units, numbered from 0 to M-1. Whenev

Algorithm | Sort

Bubble sort Bubble sort, sometimes incorrectly referred to as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wron

PLA Percentron Learning Algorithm #台大 Machine learning #

Percentron Learning Algorithm 于垃圾邮件的鉴别 这里肯定会预先给定一个关于垃圾邮件词汇的集合(keyword set),然后根据四组不通过的输入样本里面垃圾词汇出现的频率来鉴别是否是垃圾邮件.系统输出+1判定为垃圾邮件,否则不是.这里答案是第二组. 拿二维数据来做例子.我们要选取一条线来划分红色的叉叉,和蓝色的圈圈样本点(线性划分).怎么做呢?这里的困难之处就在于,其实可行的解可能存在无数条直线可以划分这些样本点.很难全部求解,或许实际生活中并不需要全部求解.于是,

STL algorithm算法is_partitioned(26)

is_partitioned原型: std::is_partitioned template <class InputIterator, class UnaryPredicate> bool is_partitioned (InputIterator first, InputIterator last, UnaryPredicate pred); 测试范围内的元素是否是以pred为准则的一个划分.如果是,则返回true,否则返回false. 划分的意思是说,对每个元素进行pred(*it),得