c++经典算法.docx

上传人:b****8 文档编号:10445491 上传时间:2023-02-11 格式:DOCX 页数:23 大小:20.33KB
下载 相关 举报
c++经典算法.docx_第1页
第1页 / 共23页
c++经典算法.docx_第2页
第2页 / 共23页
c++经典算法.docx_第3页
第3页 / 共23页
c++经典算法.docx_第4页
第4页 / 共23页
c++经典算法.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

c++经典算法.docx

《c++经典算法.docx》由会员分享,可在线阅读,更多相关《c++经典算法.docx(23页珍藏版)》请在冰豆网上搜索。

c++经典算法.docx

c++经典算法

C++经典算法

1.链表逆序

[cpp] viewplain copy

1.#include   

2.using namespace std;  

3.  

4.struct node  

5.{  

6.    int value;  

7.    node * next;  

8.};  

9.  

10.node* make_link(void);  

11.node* reverse(node*);  

12.void display(node *);  

13.  

14.int main()  

15.{  

16.    node *head=make_link();  

17.    display(head);  

18.    head=reverse(head);  

19.    display(head);  

20.  

21.    return 0;  

22.}  

23.  

24.node* make_link(void)  

25.{  

26.    node *head=new node();  

27.    node *cur=head;  

28.    for(int i=0;i<10;i++)  

29.    {  

30.        cur->value=rand()%10;  

31.        cur->next=new node();  

32.        cur=cur->next;  

33.    }  

34.  

35.    return head;  

36.}  

37.  

38.node* reverse(node *head)  

39.{  

40.    node *pre,*post,*cur;  

41.    if(!

head && !

head->next)  

42.        return head;  

43.    pre=head;  

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.    return pre;  

54.}  

55.  

56.void display(node * head)  

57.{  

58.    node * cur=head;  

59.    while(cur)  

60.    {  

61.        cout<value<<" ";  

62.        cur=cur->next;  

63.    }  

64.    cout<

65.}  

2.链表合并

[cpp] viewplain copy

1.#include   

2.  

3.using namespace std;  

4.  

5.struct node  

6.{  

7.    int value;  

8.    node *next;  

9.};  

10.  

11.node *make_list(void);  

12.void display(node *);  

13.void sort(node *);  

14.node *merge(node *,node *);  

15.  

16.int main()  

17.{  

18.    node *node1=make_list();  

19.    display(node1);  

20.    sort(node1);  

21.  

22.    node *node2=make_list();  

23.    display(node2);  

24.    sort(node2);  

25.  

26.    node *mnode=merge(node1,node2);  

27.     display(mnode);  

28.  

29.    return 0;  

30.}  

31.  

32.  

33.node *make_list(void)  

34.{  

35.    node *head=new node();  

36.    node *cur=head;  

37.    for(int i=0;i<10;i++)  

38.    {  

39.        cur->value=rand()%10;  

40.        cur->next=new node();  

41.        cur=cur->next;  

42.    }  

43.  

44.    return head;  

45.}  

46.  

47.void display(node *head)  

48.{  

49.    node *cur=head;  

50.    while(cur)  

51.    {  

52.        cout<value<<" ";  

53.        cur=cur->next;  

54.    }  

55.    cout<

56.}  

57.  

58.  

59.void sort(node *head)  

60.{  

61.    node *cur=head;  

62.    while(cur)  

63.    {  

64.        node *min=cur;  

65.        node *cur2=cur->next;  

66.        while(cur2)  

67.        {  

68.            if(cur2->value < min->value)  

69.                min=cur2;  

70.            cur2=cur2->next;  

71.        }  

72.  

73.        int temp=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=new node();  

83.    node *cur1=h1;  

84.    node *cur2=h2;  

85.    while(cur1&&cur2)  

86.    {  

87.        if(cur1->value < cur2->value)  

88.        {  

89.            mcur->next=cur1;  

90.            mcur=mcur->next;  

91.            cur1=cur1->next;  

92.        }  

93.        else  

94.        {  

95.            mcur->next=cur2;  

96.            mcur=mcur->next;  

97.            cur2=cur2->next;  

98.        }  

99.    }  

100.    if(cur1)  

101.        mcur->next=cur1;  

102.    else  

103.        mcur->next=cur2;  

104.    return h1->value < h2->value ?

 h1:

h2;  

105.}  

3.一棵树是否某条路径结点之和等于给定值。

并描述算法复杂度

[cpp] viewplain copy

1.#include   

2.using namespace std;  

3.  

4.struct node  

5.{  

6.    int value;  

7.    node *left;  

8.    node *right;  

9.};  

10.  

11.node * build_tree(void);  

12.bool find(node *,int);  

13.  

14.int main()  

15.{  

16.    node *tree=build_tree();  

17.    int t;  

18.    cout<<"Enter your number:

";  

19.    cin>>t;  

20.    cout<

21.    cout<

22.}  

23.  

24.node *build_tree()  

25.{  

26.    int a;  

27.    cin>>a;  

28.    if(a == 0)  

29.        return NULL;  

30.    node *root=new node();  

31.    root->value=a;  

32.    root->left=build_tree();  

33.    root->right=build_tree();  

34.  

35.    cout<<"build tree success"<

36.    return root;  

37.}  

38.  

39.bool find(node *root,int v)  

40.{  

41.    if(!

root)  

42.        return false;  

43.    if(root->value == v)  

44.        return true;  

45.    else find(root->left,v-root->value) || find(root->right,v-root->value);  

46.}  

4.你熟悉的排序算法并描述算法复杂度。

快速排序

[cpp] viewplain copy

1.#include   

2.using namespace std;  

3.  

4.int partition(int a[],int low,int high)  

5.{  

6.    int key=a[low]; //用子表的第一个记录作杻轴记录  

7.    while(low < high)   //从表的两端交替地向中间扫描  

8.    {  

9.        while(low < high && a[high] >= key)  

10.            --high;  

11.        {                  //将比杻轴记录小的记录交换到低端  

12.            int temp=a[low];  

13.            a[low]=a[high];  

14.            a[high]=temp;  

15.        }  

16.  

17.        while(low < high && a[low] <= key)  

18.            ++low;  

19.        {                 //将比杻轴记录大的记录交换到低端  

20.            int temp=a[low];  

21.            a[low]=a[high];  

22.            a[high]=temp;  

23.        }  

24.    }  

25.    return low;   //返回杻轴所在的位置  

26.}  

27.  

28.void qsort(int a[],int b,int e)  

29.{  

30.    if(b < e)  

31.    {  

32.        int m=partition(a,b,e);  

33.        qsort(a,b,m-1);  

34.        qsort(a,m+1,e);  

35.    }  

36.}  

37.  

38.int main()  

39.{  

40.    int a[]={2,3,7,8,3,5};  

41.    qsort(a,0,5);  

42.    for(int i=0;i<6;i++)  

43.        cout<

44.    cout<

45.  

46.    return 0;  

47.}  

归并排序

[cpp] viewplain copy

1.#include   

2.using namespace std;  

3.  

4.void display(int a[],int size)  

5.{  

6.    for(int i=0;i

7.        cout<

8.    cout<

9.}  

10.  

11.void mmerge(int *a,int low,int middle ,int high )  

12.{  

13.    int fronArray[100],postArray[100];  

14.    int front=middle-low+1;  

15.    int post=high-middle;  

16.    for(int i=0;i

17.        fronArray[i]=a[low+i];  

18.  

19.    for(int j=0;j

20.        postArray[j]=a[middle+j+1];  

21.  

22.    fronArray[front]=9999; //哨兵  

23.    postArray[post]=9999;  

24.  

25.    int i=0,j=0;  

26.    for(int k=low;k<=high;k++)  

27.    {  

28.        if(fronArray[i]

29.            a[k]=fronArray[i++];  

30.        else  

31.            a[k]=postArray[j++];  

32.    }  

33.}  

34.  

35.void merge_sort(int *a,int low,int high)  

36.{  

37.    if(low

38.    {  

39.        int middle=(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.int main()  

47.{  

48.    int a[]={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 0;  

54.}  

堆排序

[cpp] viewplain copy

1./* 

2.堆排序 

3.

(1)用大根堆排序的基本思想 

4.① 先将初始文件R[1..n]建成一个大根堆,此堆为初始的无序区 

5.② 再将关键字最大的记录R[1](即堆顶)和无序区的最后一个记录R[n]交换, 

6.由此得到新的无序区R[1..n-1]和有序区R[n],且满足R[1..n-1].keys≤R[n].key 

7.③ 由于交换后新的根R[1]可能违反堆性质,故应将当前无序区R[1..n-1]调整为堆。

 

8.然后再次将R[1..n-1]中关键字最大的记录R[1]和该区间的最后一个记录R[n-1]交换, 

9.由此得到新的无序区R[1..n-2]和有序区R[n-1..n],且仍满足关系R[1..n- 2].keys≤R[n-1..n].keys, 

10.同样要将R[1..n-2]调整为堆。

 

11.…… 

12.直到无序区只有一个元素为止。

 

13.

(2)大根堆排序算法的基本操作:

 

14.① 初始化操作:

将R[1..n]构造为初始堆; 

15.② 每一趟排序的基本操作:

将当前无序区的堆顶记录R[1]和该区间的最后一个记录交换,然后将新的无序区调整为堆(亦称重建堆)。

 

16.注意:

 

17.①只需做n-1趟排序,选出较大的n-1个关键字即可以使得文件递增有序。

 

18.②用小根堆排序与利用大根堆类似,只不过其排序结果是递减有序的。

 

19.堆排序和直接选择排序相反:

在任何时刻,堆排序中无序区总是在有序区之前, 

20.且有序区是在原向量的尾部由后往前逐步扩大至整个向量为止。

 

21.*/  

22.  

23.#include   

24.  

25.using namespace std;  

26.  

27.//生成大根堆  

28.void HeapAdjust(int SortData[],int StartIndex, int Length)  

29.{  

30.    while(2*StartIndex+1 < Length)  

31.    {  

32.        int MaxChildrenIndex = 2*StartIndex+1 ;  

33.        if(2*StartIndex+2 < Length )  

34.        {  

35.            //比较左子树和右子树,记录最大值的Index  

36.            if(SortData[2*StartIndex+1]

37.            {  

38.                MaxChildrenIndex = 2*StartIndex+2;  

39.            }  

40.        }  

41.        if(SortData[StartIndex] < SortData[MaxChildrenIndex])  

42.        {  

43.            //交换i与MinChildrenIndex的数据  

44.            int tmpData =SortData[StartIndex];  

45.            SortData[StartIndex] =SortData[MaxChildrenIndex];  

46.            SortData[MaxChildrenIndex] =tmpData;  

47.            //堆被破坏,需要重新调整  

48.            StartIndex = MaxChildrenIndex ;  

49.        }  

50.        else  

51.        {  

52.            //比较左右孩子均大则堆未破坏,不再需要调整  

53.            break;  

54.        }  

55.    }  

56.}  

57.  

58.//堆排序  

59.void HeapSortData(int SortData[], int Length)  

60.{  

61.    int i=0;  

62.  

63.    //将Hr[0,Length-1]建成大根堆  

64.    for (i=Length/2-1; i>=0; i--)  

65.    {  

66.        HeapAdjust(SortData, i, Length);  

67.    }  

68.  

69.    for (i=Length-1; i>0; i--)  

70.    {  

71.        //与最后一个记录交换  

72.        int tmpData =SortData[0];  

73.        SortData[0] =SortData[i];  

74.        SortData[i] =tmpData;  

75.        //将H.r[0..i]重新调整为大根堆  

76.        HeapAdjust(Sort

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 管理学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1