插入排序和归并排序的总结

    插入排序的pesudocode 和c语言的实现,并且注释有详细的algorithms的时间的分析

/*
pesudocode

for j <- 2 to n
	do key <- A[j]
		i <- j -1
		while i > 0 and A[i] > key
		 do A[i + 1] <- A[i]
		  	i <- i - 1
		A[i + 1] <- key

Running time 
	Depends on input sizes 
	 -- params in input size 
	upper bounds
	 -- guaranet the users 
	 
Kinds of analysis
	T(n)[this is a realtion to functions] = max time on any input of size n 
	T(n) = expected time over all input of size n 
	  {Need assumption of uniform distribution}

Best-case(bagus)
	cheat with with a slow algorithms

The time 
	Depends on computer 
	 -- relative speed(on same machine)
	 -- absolute speed(on diff machine)

THe big ideas of algorithms !! relay on asymptotic analysis(jian jin fenxi)
    1. ignore machine- dependent constants 
    2. Look at growth of T(n) as n -> wuqiong
    
    Asymptotic notations
     theta -- drop slow terms 
           --ignore the leading constants 
      
    worest case 
    	arithmetic 
     faster ?
        --  Not at all for large n 
    
Merge sort 
 		1) Merge sort A[1 .. n]
    	 if n = 1 done 
    	 Recursively sort A[1 .. n/2] and A[n/2 + 1 .. n]
    	 Merge them sort lists 
    	  key subroutine -- Merge
    	    20 13 7 2 
    	    12 11 9 1  TIme = theta(n) on n total elems liner time
    	    recursive  theta(nlgn) time faster than insertion sort
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>	
void insetsort(int *A, int length)
{
	int i = 0;
	int j = 0;
	int key;
	for (j = 1; j < length; j++)
	{
		key = A[j];
		i = j - 1;
		
		while(i > 0 && A[i] > key)
		{
			A[i + 1] = A[i];
			i = i - 1;
		}
		
		A[i + 1] = key;
	}
}

void main()
{
	int i = 0;
	int a[5] = {1, 5, 2, 4, 3};
	insetsort(a, 5);
	for(i = 0; i < 5; i++)
   		printf("%d\n", a[i]);
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值