Array of data structures and algorithms __ data structure

Source: Internet
Author: User
Tags data structures

The basic concept of arrays: arrays are the simplest and most commonly used data structures, but there are some caveats:

(1) The distribution mode and storage location of the array;

(2) initialization;

(3) Advanced definition of array in different languages;

(4) Multidimensional array;


Array allocation methods in C + +:

(1) int a[10];

Applies to situations where the length of the array is known or is not sensitive to the length of the log, such as defining a string.

(2) int *a = (int *) malloc (sizeof (int) *n);

Used in C, applies to the length of the dynamic request array. Remember to finally free, to prevent memory leaks.

(3) int[] A = new int[n];

Used in C + +. Last to delete.


Virtual address space for the process:



When a process is running, it divides the addresses into segments, with two important: the runtime heap and the user stack. The dynamically requested array (malloc and new) will be in the runtime heap, and the constant array (int a[10]) will be placed inside the user stack. Arrays that are placed in the runtime heap are permanent and will persist without active release, causing memory leaks. While the user stack space is temporary, it is reclaimed as the function is invoked and returned. So we can't return the address of a local variable, especially the first address of a constant array, because the address is recycled after it's used up. So if a function is modified inside the function, and you want to continue to use it outside of the function, use the dynamically allocated array.


Initialization of arrays in C + +

Many bugs in programming are caused by a direct reference to an uninitialized variable, and the initial words of a generic array are as follows:

(1) Automatic initialization of global array;

(2) malloc mode is not initialized, you can use memset (can be initialized to 0 or-1), fill to initialize;

(3) The new method needs to be judged by the compiler;


The basis of the array of direct operation of memory, high efficiency, but lack of rich APIs, so the high-level language often has a corresponding advanced implementation. The vector in C + + is used in STL.

Multidimensional arrays are extensions of one-dimensional arrays, and two-dimensional arrays can be used to describe matrices or graphs, as follows:

    The int a1[10][10];//requires two dimensions of the array, the
    int **a2;//Two dimensions are indeterminate
    , int* a3[10];//each dimension is allocated separately, the length can be inconsistent;
    vector<int> a4[10 ];//can only be used in C + +,
    vector<vector<int>> a5;//can only be used in C + +;

Multidimensional array in the access process to pay attention to the cache missing problem, the array can be operated in rows and by column operation mode. It's better to do it in rows than by column. Because the process of loading a two-dimensional array into the cache is in line, the elements of this line are already in the cache when you visit a row. However, by column access is different, usually only one element in the cache, the other elements will be missing, so access by column will cause serious cache loss problem, can seriously affect performance.


The role of arrays in sorting

For the seven most common sorts, I've talked about it in previous blogs, including code implementations. Where arrays have a very large effect in sorting, here's a simple sort overview:

Degree of Complexity O (n^2):

(1) Insert sort-The complexity is the fastest in O (n^2);

(2) Select sort, bubble sort is slower, usually appear in the interview question.


Degree of Complexity O (N*LOGN):

(1) Quick sort: The fastest in the ranking based on comparison;

(2) Merge sort: In actual use less, often in the interview;

(3) heap sorting: Less used in practice, but the heap is very common;


The complexity is O (n) and can be sorted only for positive integers:

(1) Cardinal Order-Complexity O (n+r), r as base;

(2) Count sort-complexity O (n+k), K for element range;


However, in the actual development, C + + has its own sorting function:

(1) There is a qsort function in C, which requires a custom CMP comparison function, which forms the following functions:

int cmp (const void *a,const void *b);

(2) in C + +, the STL has the sort function in its own order, and the method is called as follows:

Sort (A.begin (), A.end (),[greater<int> () | less<int> ());



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.