1、c+经典算法C+经典算法1.链表逆序cppview plaincopy1. #include2. usingnamespacestd;3. 4. structnode5. 6. intvalue;7. node*next;8. ;9. 10. node*make_link(void);11. node*reverse(node*);12. voiddisplay(node*);13. 14. intmain()15. 16. node*head=make_link();17. display(head);18. head=reverse(head);19. display(head);20.
2、21. return0;22. 23. 24. node*make_link(void)25. 26. node*head=newnode();27. node*cur=head;28. for(inti=0;ivalue=rand()%10;31. cur-next=newnode();32. cur=cur-next;33. 34. 35. returnhead;36. 37. 38. node*reverse(node*head)39. 40. node*pre,*post,*cur;41. if(!head&!head-next)42. returnhead;43. pre=head;
3、44. cur=pre-next;45. while(cur)46. 47. post=cur-next;48. cur-next=pre;49. pre=cur;50. cur=post;51. 52. head-next=NULL;53. returnpre;54. 55. 56. voiddisplay(node*head)57. 58. node*cur=head;59. while(cur)60. 61. coutvaluenext;63. 64. coutendl;65. 2.链表合并cppview plaincopy1. #include2. 3. usingnamespaces
4、td;4. 5. structnode6. 7. intvalue;8. node*next;9. ;10. 11. node*make_list(void);12. voiddisplay(node*);13. voidsort(node*);14. node*merge(node*,node*);15. 16. intmain()17. 18. node*node1=make_list();19. display(node1);20. sort(node1);21. 22. node*node2=make_list();23. display(node2);24. sort(node2);
5、25. 26. node*mnode=merge(node1,node2);27. display(mnode);28. 29. return0;30. 31. 32. 33. node*make_list(void)34. 35. node*head=newnode();36. node*cur=head;37. for(inti=0;ivalue=rand()%10;40. cur-next=newnode();41. cur=cur-next;42. 43. 44. returnhead;45. 46. 47. voiddisplay(node*head)48. 49. node*cur
6、=head;50. while(cur)51. 52. coutvaluenext;54. 55. coutnext;66. while(cur2)67. 68. if(cur2-valuevalue)69. min=cur2;70. cur2=cur2-next;71. 72. 73. inttemp=cur-value;74. cur-value=min-value;75. min-value=temp;76. cur=cur-next;77. 78. 79. 80. node*merge(node*h1,node*h2)81. 82. node*mcur=newnode();83. no
7、de*cur1=h1;84. node*cur2=h2;85. while(cur1&cur2)86. 87. if(cur1-valuevalue)88. 89. mcur-next=cur1;90. mcur=mcur-next;91. cur1=cur1-next;92. 93. else94. 95. mcur-next=cur2;96. mcur=mcur-next;97. cur2=cur2-next;98. 99. 100. if(cur1)101. mcur-next=cur1;102. else103. mcur-next=cur2;104. returnh1-valueva
8、lue?h1:h2;105. 3.一棵树是否某条路径结点之和等于给定值。并描述算法复杂度cppview plaincopy1. #include2. usingnamespacestd;3. 4. structnode5. 6. intvalue;7. node*left;8. node*right;9. ;10. 11. node*build_tree(void);12. boolfind(node*,int);13. 14. intmain()15. 16. node*tree=build_tree();17. intt;18. coutt;20. coutendl;21. coutfin
9、d(tree,t)a;28. if(a=0)29. returnNULL;30. node*root=newnode();31. root-value=a;32. root-left=build_tree();33. root-right=build_tree();34. 35. coutbuildtreesuccessvalue=v)44. returntrue;45. elsefind(root-left,v-root-value)|find(root-right,v-root-value);46. 4.你熟悉的排序算法并描述算法复杂度。快速排序cppview plaincopy1. #i
10、nclude2. usingnamespacestd;3. 4. intpartition(inta,intlow,inthigh)5. 6. intkey=alow;/用子表的第一个记录作杻轴记录7. while(lowhigh)/从表的两端交替地向中间扫描8. 9. while(low=key)10. -high;11. /将比杻轴记录小的记录交换到低端12. inttemp=alow;13. alow=ahigh;14. ahigh=temp;15. 16. 17. while(lowhigh&alow=key)18. +low;19. /将比杻轴记录大的记录交换到低端20. intte
11、mp=alow;21. alow=ahigh;22. ahigh=temp;23. 24. 25. returnlow;/返回杻轴所在的位置26. 27. 28. voidqsort(inta,intb,inte)29. 30. if(be)31. 32. intm=partition(a,b,e);33. qsort(a,b,m-1);34. qsort(a,m+1,e);35. 36. 37. 38. intmain()39. 40. inta=2,3,7,8,3,5;41. qsort(a,0,5);42. for(inti=0;i6;i+)43. coutai;44. coutendl
12、;45. 46. return0;47. 归并排序cppview plaincopy1. #include2. usingnamespacestd;3. 4. voiddisplay(inta,intsize)5. 6. for(inti=0;isize;i+)7. coutai;8. coutendl;9. 10. 11. voidmmerge(int*a,intlow,intmiddle,inthigh)12. 13. intfronArray100,postArray100;14. intfront=middle-low+1;15. intpost=high-middle;16. for
13、(inti=0;ifront;i+)17. fronArrayi=alow+i;18. 19. for(intj=0;jpost;j+)20. postArrayj=amiddle+j+1;21. 22. fronArrayfront=9999;/哨兵23. postArraypost=9999;24. 25. inti=0,j=0;26. for(intk=low;k=high;k+)27. 28. if(fronArrayipostArrayj)29. ak=fronArrayi+;30. else31. ak=postArrayj+;32. 33. 34. 35. voidmerge_s
14、ort(int*a,intlow,inthigh)36. 37. if(lowhigh)38. 39. intmiddle=(low+high)/2;40. merge_sort(a,low,middle);41. merge_sort(a,middle+1,high);42. mmerge(a,low,middle,high);43. 44. 45. 46. intmain()47. 48. inta=9,3,5,7,6,8,10,22,21,34;49. display(a,10);50. merge_sort(a,0,9);51. display(a,10);52. 53. return
15、0;54. 堆排序cppview plaincopy1. /*2. 堆排序3. (1)用大根堆排序的基本思想4. 先将初始文件R1.n建成一个大根堆,此堆为初始的无序区5. 再将关键字最大的记录R1(即堆顶)和无序区的最后一个记录Rn交换,6. 由此得到新的无序区R1.n-1和有序区Rn,且满足R1.n-1.keysRn.key7. 由于交换后新的根R1可能违反堆性质,故应将当前无序区R1.n-1调整为堆。8. 然后再次将R1.n-1中关键字最大的记录R1和该区间的最后一个记录Rn-1交换,9. 由此得到新的无序区R1.n-2和有序区Rn-1.n,且仍满足关系R1.n-2.keysRn-1.n
16、.keys,10. 同样要将R1.n-2调整为堆。11. 12. 直到无序区只有一个元素为止。13. (2)大根堆排序算法的基本操作:14. 初始化操作:将R1.n构造为初始堆;15. 每一趟排序的基本操作:将当前无序区的堆顶记录R1和该区间的最后一个记录交换,然后将新的无序区调整为堆(亦称重建堆)。16. 注意:17. 只需做n-1趟排序,选出较大的n-1个关键字即可以使得文件递增有序。18. 用小根堆排序与利用大根堆类似,只不过其排序结果是递减有序的。19. 堆排序和直接选择排序相反:在任何时刻,堆排序中无序区总是在有序区之前,20. 且有序区是在原向量的尾部由后往前逐步扩大至整个向量为止
17、。21. */22. 23. #include24. 25. usingnamespacestd;26. 27. /生成大根堆28. voidHeapAdjust(intSortData,intStartIndex,intLength)29. 30. while(2*StartIndex+1Length)31. 32. intMaxChildrenIndex=2*StartIndex+1;33. if(2*StartIndex+2Length)34. 35. /比较左子树和右子树,记录最大值的Index36. if(SortData2*StartIndex+1SortData2*StartIndex+2)37. 38. MaxChildrenIndex=2*StartIndex+2;39. 40. 41. if(SortDataStartIndex=0;i-)65. 66. HeapAdjust(SortData,i,Length);67. 68. 69. for(i=Length-1;i0;i-)70. 71. /与最后一个记录交换72. inttmpData=SortData0;73. SortData0=SortDatai;74. SortDatai=tmpData;75. /将H.r0.i重新调整为大根堆76. HeapAdjust(Sort
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1