http://www.tudou.com/programs/view/xoDQgPbCyQ0/?fr=rec1 strongly recommend this video to you
this is written by a bird with big mouth
in may first , I watch a wonderful open course video of algorithm.
this is has a great influence on me.
as a student learning math,although computer is not my major,but I deeply love it.
here is my comment and summary about the lessons .
<INTRODUCTION OF ALGORITHM> first lesson
the teacher use insertion sort as an example. he talks about the efficiency of this sort.
1. input (the already sort or reversed sort.this is different .)
2.size(we need to sort the length of the sort)
3.we put our attention on the worst time(or the max time it run) that the algorithm does .not concern the best time . we will give the customer how long that the programme wiil run.
4. what we concern is about the same algorithm on the same computer. RELATIVE SPEED.we seldom run a programme on different computer(such as a supercomputer or pc).
we use asymptotic analysis//渐进分析
what we analysis is the growth of running time ,not the specific times that it run. thie is really an huge idea.
we use the θ (called theta) indicate the efficiency. we need to drop low order terms./舍弃低阶项。ignore leading constant.//忽略常数因子。
take a example to make it more clear. this is 9n3+3n2 -5n+606.this is the steps that a programme runs. and we ues θ(n3) to represent it steps.only to
descripe its growth.
now let's describe the insertion sort we use the formular Σn2θ(j) to describe the growth of insertion sort and this is equal to Σn2θ(j) = θ(n2).
here is the insertion code written by me.
//insertion sort
#include<stdio.h>
#include<stdlib.h>
int a[100];
int main()
{
int n,i,j,temp;
scanf("%d",&n);
for( i=0;i<n;i++)
scanf("%d",&a[i]);
for( i=1;i<n;i++)
{
temp=a[i];
for(j=i-1;j>=0;j--)
{
if (temp>=a[j]){a[j+1]=a[j];} // the key of insertion whether big or small.
else break;
}
a[j+1]=temp; //this is from big to small.
}
for(i=0;i<n;i++)
printf("%d ",a[i]);
system("pause");
}
when the input numbers sums are small.this can finish the sort fast, but we can find another algorith to reduce its steps. this is called merge sort
in chinese we called 归并。
now here is the problem to sort a[1 - n],and we can sort it from a[1 - n/2 ]toa[n/2 - n] //the red parts pay attention. when it is odd or even.
the θ of this sort is θ (n lg n)。
I recode this merge sort . It is a little diffculty for me to understand how to relize this algorithm. I spent about two hours to memrize the recurision to relize this .
post my code:
in this code ,I output more information on how it divide it . and how to sort it . if you didn't understand this code .you can run it on yout dev c++;
#include<stdio.h>
#include<stdlib.h>
void mergesort(int a[],int b[],int f,int e);
void merge(int a[],int b[],int f,int mid,int e);
int main()
{
int n,i,a[10],b[10],f,e; // deal with the max size is nine.
scanf("%d",&n);
f=1;
e=n;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
mergesort(a,b,f,e); // divide the number and sort it.
for(i=1;i<=n;i++)
printf("%d ",a[i]);
system("pause");
}
void mergesort(int a[],int b[],int f,int e)
{
printf("f=%d e=%d\n",f,e);
int mid;
if(f+1>e) return; //divide it unitil it only contain one number. then return .
else{
mid=(f+e)/2;
mergesort(a,b,f,mid); //divid like a binary tree.
mergesort(a,b,mid+1,e);
merge(a,b,f,mid,e); //sort every two binary.
}
}
void merge(int a[],int b[],int f,int mid,int e)
{
int i=f,j=mid+1,k=f;
while(i<=mid&&j<=e)
{
if(b[i]>b[j])a[k++]=b[i++];
else a[k++]=b[j++];
}
while(i<=mid)a[k++]=b[i++];
while(j<=e) a[k++]=b[j++];
for(i=f;i<=e;i++)
{
b[i]=a[i];
printf("a[%d]=%d ",i,a[i]);
}
printf("\n");
}
you can searching more information by watching this video.