C++ 经典算法 面试绝杀

1.链表逆序

2.链表合并

3.一棵树是否某条路径结点之和等于给定值。并描述算法复杂度

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

         快速排序

         归并排序

         堆排序

         选择排序

         插入排序

        冒泡排序

        折半插入排序

以下代码都能成功通过。


1.链表逆序


  1. #include <iostream>  
  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<<cur->value<<" ";  
  62.         cur=cur->next;  
  63.     }  
  64.     cout<<endl;  
  65. }  
#include <iostream>
using namespace std;

struct node
{
    int value;
    node * next;
};

node* make_link(void);
node* reverse(node*);
void display(node *);

int main()
{
    node *head=make_link();
    display(head);
    head=reverse(head);
    display(head);

    return 0;
}

node* make_link(void)
{
    node *head=new node();
    node *cur=head;
    for(int i=0;i<10;i++)
    {
        cur->value=rand()%10;
        cur->next=new node();
        cur=cur->next;
    }

    return head;
}

node* reverse(node *head)
{
    node *pre,*post,*cur;
    if(!head && !head->next)
        return head;
    pre=head;
    cur=pre->next;
    while(cur)
    {
        post=cur->next;
        cur->next=pre;
        pre=cur;
        cur=post;
    }
    head->next=NULL;
    return pre;
}

void display(node * head)
{
    node * cur=head;
    while(cur)
    {
        cout<<cur->value<<" ";
        cur=cur->next;
    }
    cout<<endl;
}




2.链表合并
  1. #include <iostream>  
  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<<cur->value<<" ";  
  53.         cur=cur->next;  
  54.     }  
  55.     cout<<endl;  
  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. }  
#include <iostream>

using namespace std;

struct node
{
    int value;
    node *next;
};

node *make_list(void);
void display(node *);
void sort(node *);
node *merge(node *,node *);

int main()
{
    node *node1=make_list();
    display(node1);
    sort(node1);

    node *node2=make_list();
    display(node2);
    sort(node2);

    node *mnode=merge(node1,node2);
     display(mnode);

    return 0;
}


node *make_list(void)
{
    node *head=new node();
    node *cur=head;
    for(int i=0;i<10;i++)
    {
        cur->value=rand()%10;
        cur->next=new node();
        cur=cur->next;
    }

    return head;
}

void display(node *head)
{
    node *cur=head;
    while(cur)
    {
        cout<<cur->value<<" ";
        cur=cur->next;
    }
    cout<<endl;
}


void sort(node *head)
{
    node *cur=head;
    while(cur)
    {
        node *min=cur;
        node *cur2=cur->next;
        while(cur2)
        {
            if(cur2->value < min->value)
                min=cur2;
            cur2=cur2->next;
        }

        int temp=cur->value;
        cur->value=min->value;
        min->value=temp;
        cur=cur->next;
    }
}

node *merge(node *h1,node *h2)
{
    node *mcur=new node();
    node *cur1=h1;
    node *cur2=h2;
    while(cur1&&cur2)
    {
        if(cur1->value < cur2->value)
        {
            mcur->next=cur1;
            mcur=mcur->next;
            cur1=cur1->next;
        }
        else
        {
            mcur->next=cur2;
            mcur=mcur->next;
            cur2=cur2->next;
        }
    }
    if(cur1)
        mcur->next=cur1;
    else
        mcur->next=cur2;
    return h1->value < h2->value ? h1:h2;
}



3.一棵树是否某条路径结点之和等于给定值。并描述算法复杂度
  1. #include <iostream>  
  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<<endl;  
  21.     cout<<find(tree,t)<<endl;  
  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"<<endl;  
  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. }  
#include <iostream>
using namespace std;

struct node
{
    int value;
    node *left;
    node *right;
};

node * build_tree(void);
bool find(node *,int);

int main()
{
    node *tree=build_tree();
    int t;
    cout<<"Enter your number:";
    cin>>t;
    cout<<endl;
    cout<<find(tree,t)<<endl;
}

node *build_tree()
{
    int a;
    cin>>a;
    if(a == 0)
        return NULL;
    node *root=new node();
    root->value=a;
    root->left=build_tree();
    root->right=build_tree();

    cout<<"build tree success"<<endl;
    return root;
}

bool find(node *root,int v)
{
    if(!root)
        return false;
    if(root->value == v)
        return true;
    else find(root->left,v-root->value) || find(root->right,v-root->value);
}



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

  1. #include <iostream>  
  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<<a[i]<<" ";  
  44.     cout<<endl;  
  45.   
  46.     return 0;  
  47. }  
#include <iostream>
using namespace std;

int partition(int a[],int low,int high)
{
    int key=a[low]; //用子表的第一个记录作杻轴记录
    while(low < high)   //从表的两端交替地向中间扫描
    {
        while(low < high && a[high] >= key)
            --high;
        {                  //将比杻轴记录小的记录交换到低端
            int temp=a[low];
            a[low]=a[high];
            a[high]=temp;
        }

        while(low < high && a[low] <= key)
            ++low;
        {                 //将比杻轴记录大的记录交换到低端
            int temp=a[low];
            a[low]=a[high];
            a[high]=temp;
        }
    }
    return low;   //返回杻轴所在的位置
}

void qsort(int a[],int b,int e)
{
    if(b < e)
    {
        int m=partition(a,b,e);
        qsort(a,b,m-1);
        qsort(a,m+1,e);
    }
}

int main()
{
    int a[]={2,3,7,8,3,5};
    qsort(a,0,5);
    for(int i=0;i<6;i++)
        cout<<a[i]<<" ";
    cout<<endl;

    return 0;
}



归并排序
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. void display(int a[],int size)  
  5. {  
  6.     for(int i=0;i<size;i++)  
  7.         cout<<a[i]<<" ";  
  8.     cout<<endl;  
  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<front;i++)  
  17.         fronArray[i]=a[low+i];  
  18.   
  19.     for(int j=0;j<post;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]<postArray[j])  
  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<high)  
  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. }  
#include <iostream>
using namespace std;

void display(int a[],int size)
{
    for(int i=0;i<size;i++)
        cout<<a[i]<<" ";
    cout<<endl;
}

void mmerge(int *a,int low,int middle ,int high )
{
    int fronArray[100],postArray[100];
    int front=middle-low+1;
    int post=high-middle;
    for(int i=0;i<front;i++)
        fronArray[i]=a[low+i];

    for(int j=0;j<post;j++)
        postArray[j]=a[middle+j+1];

    fronArray[front]=9999; //哨兵
    postArray[post]=9999;

    int i=0,j=0;
    for(int k=low;k<=high;k++)
    {
        if(fronArray[i]<postArray[j])
            a[k]=fronArray[i++];
        else
            a[k]=postArray[j++];
    }
}

void merge_sort(int *a,int low,int high)
{
    if(low<high)
    {
        int middle=(low+high)/2;
        merge_sort(a,low,middle);
        merge_sort(a,middle+1,high);
        mmerge(a,low,middle,high);
    }
}

int main()
{
    int a[]={9,3,5,7,6,8,10,22,21,34};
    display(a,10);
    merge_sort(a,0,9);
    display(a,10);

    return 0;
}



堆排序

  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 <iostream>  
  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]<SortData[2*StartIndex+2])  
  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(SortData, 0, i);  
  77.     }  
  78. }  
  79.   
  80. //TestCase  
  81. int main()  
  82. {  
  83.     int SortData[] ={12,36,24,85,47,30,53,91};  
  84.   
  85.     HeapSortData(SortData, 8);  
  86.   
  87.     for (int i=0; i<8; i++)  
  88.     {  
  89.         cout<<SortData[i]<<" ";  
  90.     }  
  91.     cout<<endl;  
  92.   
  93.     return 0;  
  94. }  
/*
堆排序
(1)用大根堆排序的基本思想
① 先将初始文件R[1..n]建成一个大根堆,此堆为初始的无序区
② 再将关键字最大的记录R[1](即堆顶)和无序区的最后一个记录R[n]交换,
由此得到新的无序区R[1..n-1]和有序区R[n],且满足R[1..n-1].keys≤R[n].key
③ 由于交换后新的根R[1]可能违反堆性质,故应将当前无序区R[1..n-1]调整为堆。
然后再次将R[1..n-1]中关键字最大的记录R[1]和该区间的最后一个记录R[n-1]交换,
由此得到新的无序区R[1..n-2]和有序区R[n-1..n],且仍满足关系R[1..n- 2].keys≤R[n-1..n].keys,
同样要将R[1..n-2]调整为堆。
……
直到无序区只有一个元素为止。
(2)大根堆排序算法的基本操作:
① 初始化操作:将R[1..n]构造为初始堆;
② 每一趟排序的基本操作:将当前无序区的堆顶记录R[1]和该区间的最后一个记录交换,然后将新的无序区调整为堆(亦称重建堆)。
注意:
①只需做n-1趟排序,选出较大的n-1个关键字即可以使得文件递增有序。
②用小根堆排序与利用大根堆类似,只不过其排序结果是递减有序的。
堆排序和直接选择排序相反:在任何时刻,堆排序中无序区总是在有序区之前,
且有序区是在原向量的尾部由后往前逐步扩大至整个向量为止。
*/

#include <iostream>

using namespace std;

//生成大根堆
void HeapAdjust(int SortData[],int StartIndex, int Length)
{
    while(2*StartIndex+1 < Length)
    {
        int MaxChildrenIndex = 2*StartIndex+1 ;
        if(2*StartIndex+2 < Length )
        {
            //比较左子树和右子树,记录最大值的Index
            if(SortData[2*StartIndex+1]<SortData[2*StartIndex+2])
            {
                MaxChildrenIndex = 2*StartIndex+2;
            }
        }
        if(SortData[StartIndex] < SortData[MaxChildrenIndex])
        {
            //交换i与MinChildrenIndex的数据
            int tmpData =SortData[StartIndex];
            SortData[StartIndex] =SortData[MaxChildrenIndex];
            SortData[MaxChildrenIndex] =tmpData;
            //堆被破坏,需要重新调整
            StartIndex = MaxChildrenIndex ;
        }
        else
        {
            //比较左右孩子均大则堆未破坏,不再需要调整
            break;
        }
    }
}

//堆排序
void HeapSortData(int SortData[], int Length)
{
    int i=0;

    //将Hr[0,Length-1]建成大根堆
    for (i=Length/2-1; i>=0; i--)
    {
        HeapAdjust(SortData, i, Length);
    }

    for (i=Length-1; i>0; i--)
    {
        //与最后一个记录交换
        int tmpData =SortData[0];
        SortData[0] =SortData[i];
        SortData[i] =tmpData;
        //将H.r[0..i]重新调整为大根堆
        HeapAdjust(SortData, 0, i);
    }
}

//TestCase
int main()
{
    int SortData[] ={12,36,24,85,47,30,53,91};

    HeapSortData(SortData, 8);

    for (int i=0; i<8; i++)
    {
        cout<<SortData[i]<<" ";
    }
    cout<<endl;

    return 0;
}



选择排序
  1. //每一趟在n-i+1(i=1,2,...,n-1)个记录中选取关键字最小的记录作为有序序列中第i个记录  
  2. #include <iostream>  
  3. using namespace std;  
  4.   
  5. void selectSort(int a[],int size)  
  6. {  
  7.     for(int i=0;i<size-1;i++)  
  8.     {  
  9.         int lowIndex =i;  
  10.         for(int j=size-1;j>i;j--)  
  11.             if(a[j]<a[lowIndex])  
  12.                 lowIndex=j;  
  13.         int temp=a[i];  
  14.         a[i]=a[lowIndex];  
  15.         a[lowIndex]=temp;  
  16.     }  
  17. }  
  18.   
  19. int main()  
  20. {  
  21.     int a[]={12,36,24,85,47,30,53,91};  
  22.     for(int i=0;i<8;i++)  
  23.         cout<<a[i]<<" ";  
  24.     cout<<endl;  
  25.   
  26.     selectSort(a,8);  
  27.     for(int i=0;i<8;i++)  
  28.         cout<<a[i]<<" ";  
  29.     cout<<endl;  
  30.   
  31.     return 0;  
  32. }  
//每一趟在n-i+1(i=1,2,...,n-1)个记录中选取关键字最小的记录作为有序序列中第i个记录
#include <iostream>
using namespace std;

void selectSort(int a[],int size)
{
    for(int i=0;i<size-1;i++)
    {
        int lowIndex =i;
        for(int j=size-1;j>i;j--)
            if(a[j]<a[lowIndex])
                lowIndex=j;
        int temp=a[i];
        a[i]=a[lowIndex];
        a[lowIndex]=temp;
    }
}

int main()
{
    int a[]={12,36,24,85,47,30,53,91};
    for(int i=0;i<8;i++)
        cout<<a[i]<<" ";
    cout<<endl;

    selectSort(a,8);
    for(int i=0;i<8;i++)
        cout<<a[i]<<" ";
    cout<<endl;

    return 0;
}


插入排序
  1. //将一个记录插入到已经排好序的有序表中,从而得到一个新的 、记录数增1的有序表  
  2. #include <iostream>  
  3. using namespace std;  
  4.   
  5. void insertSort(int a[],int size)  
  6. {  
  7.     for(int i=1;i<size;i++)  
  8.         for(int j=i;(j>0)&&(a[j]<a[j-1]);j--)  
  9.         {  
  10.             int temp=a[j];  
  11.             a[j]=a[j-1];  
  12.             a[j-1]=temp;  
  13.         }  
  14. }  
  15.   
  16. int main()  
  17. {  
  18.     int a[]={12,36,24,85,47,30,53,91};  
  19.     for(int i=0;i<8;i++)  
  20.         cout<<a[i]<<" ";  
  21.     cout<<endl;  
  22.   
  23.     insertSort(a,8);  
  24.     for(int i=0;i<8;i++)  
  25.         cout<<a[i]<<" ";  
  26.     cout<<endl;  
  27.   
  28.     return 0;  
  29. }  
//将一个记录插入到已经排好序的有序表中,从而得到一个新的 、记录数增1的有序表
#include <iostream>
using namespace std;

void insertSort(int a[],int size)
{
    for(int i=1;i<size;i++)
        for(int j=i;(j>0)&&(a[j]<a[j-1]);j--)
        {
            int temp=a[j];
            a[j]=a[j-1];
            a[j-1]=temp;
        }
}

int main()
{
    int a[]={12,36,24,85,47,30,53,91};
    for(int i=0;i<8;i++)
        cout<<a[i]<<" ";
    cout<<endl;

    insertSort(a,8);
    for(int i=0;i<8;i++)
        cout<<a[i]<<" ";
    cout<<endl;

    return 0;
}



冒泡排序
  1. //在冒泡排序的过程中,关键字较小的记录好比水中的气泡逐趟向上漂浮,而关键字较大的记录好比石块往下沉,每一趟有一块“最大”的石头沉到水底。  
  2. #include <iostream>  
  3. using namespace std;  
  4.   
  5. void busort(int a[],int size)  
  6. {  
  7.     for(int i=0;i<size-1;i++)  
  8.         for(int j=size-1;j>i;j--)  
  9.         if(a[j]<a[j-1])  
  10.         {  
  11.             int temp=a[j];  
  12.             a[j]=a[j-1];  
  13.             a[j-1]=temp;  
  14.         }  
  15. }  
  16.   
  17. int main()  
  18. {  
  19.     int a[]={12,36,24,85,47,30,53,91};  
  20.     for(int i=0;i<8;i++)  
  21.         cout<<a[i]<<" ";  
  22.     cout<<endl;  
  23.   
  24.     busort(a,8);  
  25.     for(int i=0;i<8;i++)  
  26.         cout<<a[i]<<" ";  
  27.     cout<<endl;  
  28.   
  29.     return 0;  
  30. }  
//在冒泡排序的过程中,关键字较小的记录好比水中的气泡逐趟向上漂浮,而关键字较大的记录好比石块往下沉,每一趟有一块“最大”的石头沉到水底。
#include <iostream>
using namespace std;

void busort(int a[],int size)
{
    for(int i=0;i<size-1;i++)
        for(int j=size-1;j>i;j--)
        if(a[j]<a[j-1])
        {
            int temp=a[j];
            a[j]=a[j-1];
            a[j-1]=temp;
        }
}

int main()
{
    int a[]={12,36,24,85,47,30,53,91};
    for(int i=0;i<8;i++)
        cout<<a[i]<<" ";
    cout<<endl;

    busort(a,8);
    for(int i=0;i<8;i++)
        cout<<a[i]<<" ";
    cout<<endl;

    return 0;
}



折半插入排序
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. void BInsertSort(int a[],int size)  
  5. {  
  6.     for(int i=1;i<size;i++)  
  7.     {  
  8.         int temp=a[i];               //暂存a[i]  
  9.         int low=0;  
  10.         int high=i-1;  
  11.         while(low<=high)  
  12.         {  
  13.             int middle=(low+high)/2;  
  14.             if(temp<=a[middle])  
  15.                 high=middle-1;  
  16.             else  
  17.                 low=middle+1;  
  18.         }  
  19.   
  20.         for(int j=i-1;j>=high+1;--j)   //记录后移  
  21.             a[j+1]=a[j];  
  22.         a[high+1]=temp;                //插入,注意没有补齐的地方要注意,不然是野指针指向莫名的值。  
  23.     }  
  24. }  
  25.   
  26. int main()  
  27. {  
  28.     int a[]={12,36,24,53,53,30,53,91};  
  29.     for(int i=0;i<8;i++)  
  30.         cout<<a[i]<<" ";  
  31.     cout<<endl;  
  32.   
  33.     BInsertSort(a,8);  
  34.     for(int i=0;i<8;i++)  
  35.          cout<<a[i]<<" ";  
  36.     cout<<endl;  
  37.   
  38.     return 0;  
  39. }  
#include <iostream>
using namespace std;

void BInsertSort(int a[],int size)
{
    for(int i=1;i<size;i++)
    {
        int temp=a[i];               //暂存a[i]
        int low=0;
        int high=i-1;
        while(low<=high)
        {
            int middle=(low+high)/2;
            if(temp<=a[middle])
                high=middle-1;
            else
                low=middle+1;
        }

        for(int j=i-1;j>=high+1;--j)   //记录后移
            a[j+1]=a[j];
        a[high+1]=temp;                //插入,注意没有补齐的地方要注意,不然是野指针指向莫名的值。
    }
}

int main()
{
    int a[]={12,36,24,53,53,30,53,91};
    for(int i=0;i<8;i++)
        cout<<a[i]<<" ";
    cout<<endl;

    BInsertSort(a,8);
    for(int i=0;i<8;i++)
         cout<<a[i]<<" ";
    cout<<endl;

    return 0;
}


  • 10
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值