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