Introduction to c ++ Algorithm Implementation, Merge Sorting, and introduction to Algorithms

Source: Internet
Author: User

Introduction to c ++ Algorithm Implementation, Merge Sorting, and introduction to Algorithms

Suppose there are two stacked cards on the table, each of which is sorted, and the smallest card is on top. We hope to combine these two cards into a single output heap with the cards placed down to the table. Our basic steps include selecting a smaller card from the top two cards with two stacked cards facing up, remove the card from its heap (a new card will be displayed on top of the heap) and place the card to the output heap facing down.

There are many online texts related to Merge Sorting, which are not described here.

Cpp Code

#include
 
  using namespace std;void merge(int num[],int beg,int mid,int end);void mergeSort(int num[],int beg,int end);int main(){    int data[] = {3,6,7,2,1,4,5,9,8};    int length = 9;    cout << "before sorted:" << endl;    for(int i = 0;i < length;++i)        cout << data[i] << "  ";    cout << endl;    cout << "after sorted:" << endl;    mergeSort(data,0,length-1);    for(int i = 0;i < length;++i)        cout << data[i] << "  ";    cout << endl;    return 0;}void merge(int num[],int beg,int mid,int end){  int temp[10];  int t=beg;  int i=beg,j=mid+1;  while(i<=mid&&j<=end)  {    if(num[i]
  
 
Running result

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.