Introduction to Algorithms _ Chapter II (1)

Source: Internet
Author: User

Years ago when I went to the bookstore, the introduction of the algorithm introduced the book's name after seeing also bought down. Went home to see a period of time, found that the progress of reading is really very slow, the book after the class many questions, those who will not be the problem is through the Internet search other people's answers to be resolved. So, I would like to read this book of my experience and after-class answers to share with you. It is also a driving force for me to stick to the book of Algorithmic introduction ^_^

Because the first chapter of this book is equivalent to an introduction, skip it, and start with the second chapter!

The second chapter mainly introduces the insertion sort and merge sort:

The so-called insertion sort is like a poker stage at the beginning of a game, the sort of sorting you do for poker in your hands. At first, our left hand was empty and all the cards on the table were facing down. Then, each time we touch a card from the table and insert it in a certain order (from the big to the small to the big) to the right left hand position. Each time we want to find the right position, we need to compare it to the left hand side of the card from right to left. When the cards on the table are empty, the cards in our left hand are lined up.

Give the code:

//Insert algorithm: Enter n number in ascending order#include<stdio.h>#include<stdlib.h>voidMain () {//Enter 10 numbers    intnums[Ten] = { -, A,98,7, -, $, the,1, the, - }; intkey; //starting from 1 is because when i = 0 o'clock, only one card is not sorted     for(inti =1; I <sizeof(nums)/sizeof(int); i++)    {        //key is equivalent to a newly-touched card.Key =Nums[i]; intj = i-1; //Compare the newly touched card key with the card in front of it until it is smaller than key         while(J >=0&& Nums[j] >key) {            //If the previous card is larger than key, move the position of the front handNums[j +1] =Nums[j]; J= J-1; }        //insert key into the j+1 positionNums[j +1] =key; } printf ("%d %d%d%d%d%d%d%d%d%d", nums[0], nums[1], nums[2], nums[3], nums[4], nums[5], nums[6], nums[7], nums[8], nums[9]); System ("Pause");}

Merge sort uses the idea of divide-and-conquer law, which refers to the decomposition of the original problem into several smaller but similar to the original problem, the recursive solution of these sub-problems, and then merge the solutions of these sub-problems to establish the original problem solution.

This picture in the book can be more intuitive to see the process of merging sort

We first divide the data in the array into small independent units, and then through the sorting of two independent units (for example: two independent units 5, 2, combined to form a new independent unit 2, 5, independent units 2, 5 and 4, 7, merged to form a new independent unit 2, 4, 5, 7, and so on) to sort the entire array

Given the code, the best way to understand this is to copy the code into VS, and the next breakpoint is followed by the following steps:

//Merge Sort#include<stdlib.h>#include<stdio.h>#include<limits.h>voidMergeint* Nums,intStartnum,intMidnum,intlastnum) {    //defines two arrays for storing the left and right halves of a nums split    intlnums[6] = {0 }; intrnums[6] = {0 };    //find out how many numbers will be in the left half of the array lnums    intNumleft = Midnum-startnum +1;    //find out how many numbers will be in the right half of the array rnums    intNumright = Lastnum-Midnum; inti; intJ;    //Place the number of the left half into Lnums     for(i =0; i < Numleft; i++) {Lnums[i]= Nums[startnum +i]; }    //put the number of the right half into the rnums     for(j =0; J < Numright; J + +) {Rnums[j]= Nums[midnum + j +1]; }    //The significance of setting the last number of the array to the maximum value is that when one of the lnums or rnums finishes sorting, you can ensure that the other array continues to sort normallyLnums[i] =Int_max; RNUMS[J]=Int_max; I=0; J=0;    //sort the Nums     for(intK = Startnum; K <= Lastnum; k++)    {        //here is the equivalent of two stacks of cards lnums and rnums (here note lnums and rnums itself is from small to large order), each comparison of the top of two stacks of cards, choose smaller deposit nums        if(Lnums[i] <=Rnums[j]) {Nums[k]=Lnums[i]; I+=1; }        Else{Nums[k]=Rnums[j]; J+=1; }    }}voidMerge_sort (int* Nums,intStartnum,intlastnum) {    if(Startnum <lastnum) {        //take the midpoint of the array to divide the array into two left and right parts        intMidnum = (startnum + lastnum)/2;        //personal understanding is recursive split-left arrayMerge_sort (Nums, Startnum, midnum);//Personal understanding isrecursive split-right arrayMerge_sort (nums, Midnum +1, lastnum);        //sort and merge the left and right two arraysmerge (Nums, Startnum, Midnum, lastnum); }}voidMain () {intnums[Ten] = {9,6,7,2,5,4,3,0,1,8 }; Merge_sort (Nums,0,9); printf ("%d %d%d%d%d%d%d%d%d%d", nums[0], nums[1], nums[2], nums[3], nums[4], nums[5], nums[6], nums[7], nums[8], nums[9]); System ("Pause");}

Next update the second chapter of the exercises section

Introduction to Algorithms _ Chapter II (1)

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.