昨天参加了华为赛门铁克科技有限公司的面试。分别经历了笔试、技术面试、电话面试、等待结果...
笔试题主要是考察C语言,下来之后,发现自己的基础还不是很牢固,做错了很多不应该错的题。提醒自己做题首先要认真读题,理解题意以后再动笔,如果基础牢固,就可以很容易地看出此题考察的是什么,还要注意平时的积累,注意一些看似不起眼的问题,永远要牢记“天下大事必作于细”。此篇文章中,主要总结一下其中的一道编程题——冒泡排序,题目不难,但有很多细节需要注意。
cout<<endl<<"after bubblesort, the iarr is:"<<endl;
bubblesort(iarr,ARRAYLEN(iarr));
for (i=0; i!=ARRAYLEN(iarr); ++i)
{
cout<<*(iarr+i)<<" ";
}
cout<<endl;
return 0;
}
void bubblesort(int r[], int n)
{
for (int j=n-1; j!=0; --j)// the times that each pass should be compared, the maxtime is n-1
{
bool bSwap=false;// if in one pass there is no SWAP, the bubblesort will stop
for (int i=n-1; i!=n-1-j; --i)// in each pass, compare adjacent element form front to end.
{
if (r[i-1]>r[i])
{
SWAP(r[i-1],r[i]);
bSwap=true;
}
}
if (!bSwap)
{
return;// the bubblesort stop
}
}
}
/*
输出:
before bubblesort, the iarr is:
1 2 3 5 4
after bubblesort, the iarr is:
1 2 3 4 5
Press any key to continue
*/