您的位置:首页 > 编程语言 > Go语言

Note of introduction of Algorithms (Lecture 1)

2013-05-05 23:05 459 查看
This is the note for introduction of Algorithms, based on the MIT open class. The lecture 1 focus on how to measured the performance of program, and two sort algorithms has been mentioned, which is insertion sort and merging sort.

Here is the c program I wrote.I wish it could be a nice start, and whole open classes could been finished in 2013 finnally.

insertion sort

View Code

/*
*  from   : the beginning of the array
*  to     : the end of the array
*  len    : the length of input array
*/
void merging_sort(int from, int to){
int i,j,p = 0;

if (from < to)
{
int mid = (from + to)/2;

//recursively sort
merging_sort(from, mid);
merging_sort(mid + 1, to);

//merge
i = from;
j = mid + 1;
p = from;
while ((i <= mid) || (j <= to))
{
if (i > mid)
{
g_output[p] = g_input[j];
j++;
p++;
}
else if (j > to)
{
g_output[p] = g_input[i];
i++;
p++;
}else{
if (g_input[i] < g_input[j])
{
g_output[p] = g_input[i];
i++;
p++;
}
else
{
g_output[p] = g_input[j];
j++;
p++;
}
}
}//end of while

for (int t = from; t <= to; t++)
{
g_input[t] = g_output[t];
}
}

return;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: