Introduction to the Young's Matrix implementation code (c + +) algorithm 6.3

Source: Internet
Author: User

Personal Summary:

The difference between 1.int **p and int a[m][n]:

1) int * * Pointer to pointer, the latter type is the array name, the type is int (*) [N], which points to the entire row.

2) (a+1) address increase m*sizeof (int), one thing to note is that a[i] is the address at the beginning of line I, &a and A are the same value. The array is of a size, and the pointer is a variable that stores the address. Deliberately to look at the declaration of the Assembly code of the array, one of the instructions is MOV%gs:0x14,%eax (array size 20 that is 0x14), and finally also seems to check whether the array overflow, overflow will call a stack function (not very understand, it should be a protection mechanism). In particular, when sizeof (array name) is called, the assembly code does not increase because the size of the array is stored in a position on the stack.

When transferring parameters, it is important to note whether the transfer should be a pointer or an array. I thought I could do this at first. int **p=a; naive! )

#include <iostream>#include<algorithm>using namespacestd;#defineInt_max 100000classy_matrix{ Public: Y_matrix (intRowsintcols); ~Y_matrix (); voidYm_build (int*a,intN); voidPrintm ()Const; voidDecrease_key (intXintYintkey); intextract_min (); voidInsert_key (intkey) {        if(Y_matrix[r-1][c-1] !=Int_max) {cout<<"Over flow"<<Endl; return; } decrease_key (R-1C1, key); }    voidSort (int*array,intN); BOOLFind (intelem); voidClear ();Private:    Const intR, C; int**Y_matrix; voidYm_heapify (intXinty);}; Y_matrix::y_matrix (intRowsintcols): R (rows), C (cols) {Y_matrix=New int*[rows];  for(inti =0; i < rows; ++i) {Y_matrix[i]=New int[cols];  for(intj =0; J < cols; ++j) {Y_matrix[i][j]=Int_max; }    }}voidy_matrix::clear () { for(inti =0; i < R; ++i) for(intj =0; J < C; ++j) Y_matrix[i][j]=Int_max;}voidY_matrix::ym_build (int*a,intN) {    inttemp = MIN (n, r*b);  for(inti =0; I < temp; ++i)    {Insert_key (a[i]); }}voidY_matrix::ym_heapify (intXinty) {    intR = x, c =y; if(x +1< R&&y_matrix[r][c] > y_matrix[x +1][y])++x; if(Y +1< C&&y_matrix[x][y] > y_matrix[r][c +1]) {x=R; ++y; }    if(! (r = = X&&c = =y))        {Swap (Y_matrix[x][y], y_matrix[r][c]);    Ym_heapify (x, y); }}voidY_matrix::D Ecrease_key (intXintYintkey) {    if(X > R-1|| Y > C-1)        return; if(key>Y_matrix[x][y]) {cout<<"new element is bigger"<<Endl; return; }     while(1){        intR = x, c =y; Y_matrix[x][y]=key; if(X >0&& Key < Y_matrix[x-1][y]) {--x; }        if(Y >0&& Y_matrix[x][y] < y_matrix[r][c-1]) {x=R; --y; }        if(! (r = = X&&c = =y))        {Swap (Y_matrix[x][y], y_matrix[r][c]); }        Else{Y_matrix[x][y]=key;  Break; }    }}inty_matrix::extract_min () {inttemp = y_matrix[0][0]; y_matrix[0][0] =Int_max; Ym_heapify (0,0); returntemp;}voidY_matrix::sort (int*array,intN)    {Clear ();    Ym_build (array, n); intk = min (n, r*C);  for(inti =0; I < K; ++i) {Array[i]=extract_min (); }}BOOLY_matrix::find (intkey) {    intx = R-1; inty =0;  while(x >=0&& y <C) {        if(Y_matrix[x][y] >key)--x; Else if(Y_matrix[x][y] <key)++y; Else return true; }    return false;}voidY_matrix::P Rintm ()Const {     for(inti =0; i < R; ++i) {         for(intj =0; J < C; ++j) cout<< Y_matrix[i][j] <<"\ t"; cout<<Endl; } cout<<Endl;} Y_matrix::~Y_matrix () { for(inti =0; i < R; ++i)Delete[]y_matrix[i]; Delete[]y_matrix;}intMain () {inta[ +] = {4,6,8,2,1,0,7,4,2,1,9,5,1,111,2224, at,545,134,1122}; Y_matrix ym (5,5); Ym. Ym_build (A, +); Ym.    Printm (); Ym. Sort (A, +);  for(inti =0; I < +; ++i) cout<< A[i] <<" ";}

Introduction to the Young's Matrix implementation code (c + +) algorithm 6.3

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.