Arrays of data structures and algorithms

Source: Internet
Author: User

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

(1) How the array is allocated and where it is stored;

(2) initialization;

(3) High definition of arrays in different languages;

(4) multidimensional arrays;


Array allocation methods in C + +:

(1) int a[10];

Applies to cases where the array length is known or is not sensitive to the length of the arrays, 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 + +. The last to delete.


Virtual address space for the process:



The process will divide the address into many segments as it runs, with two important: the runtime heap and the user stack. The array of dynamic requests (malloc and new) will be in the runtime heap, and the constant array (int a[10]) will be placed inside the user stack. Arrays placed in the run-time heap are permanent, and are permanently present without active release, leading to memory leaks. While the user stack space is temporary, it is recycled as the function is called and returned. So we cannot return the address of the local variable, especially the first address of a constant array, because this address will be recycled after it is exhausted. So a function should use a dynamically allocated array if it is modified inside the function and wants to continue using it outside of the function.


Initialization of arrays in C + +

Many of the bugs in programming are caused by direct reference to uninitialized variables, and the general array's initial words are as follows:

(1) Automatic initialization of global array;

(2) The malloc mode is not initialized, it can be initialized with memset (can be initialized to 0 or-1), fill;

(3) New mode needs to be judged by compiler;


The underlying array directly operates memory, is highly efficient, but lacks a rich API, so high-level languages tend to have a corresponding high-level implementation. The vector in STL is used in C + +.

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 that the two-dimensional of the array be determined;    int **a2;//Two-dimensional is uncertain;    int* a3[10];//Each dimension is assigned 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 arrays in the access process to pay attention to the cache missing problem, the array can have in-line operation and column operation mode. It's better to do it by row than by column mode. Because the process of loading a two-dimensional array into the cache is by line, the elements of this line are already in the cache when you access a row. But access by column is different, usually there is only one element in the cache, other elements are missing, so column access can cause serious cache loss problems that can seriously affect performance.


The role of arrays in sorting

For the seven most commonly used sorts, I have mentioned in several previous blogs, including code implementation. Where arrays have a very big role in sorting, here's a simple sort overview:

Complexity of O (n^2):

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

(2) Choose Sort, bubble sort is slow, usually appear in the interview question.


Complexity of O (N*LOGN):

(1) Quick sorting: Based on the comparison of the fastest in the ranking;

(2) Merge sort: Less use in practice, often in interview;

(3) Heap sorting: The actual use is less, but the heap is very common;


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

(1) Cardinal rank-complexity O (n+r), R as Radix;

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


However, in real-world development, a sort function is brought in C/D + +:

(1) In C, there is a qsort function, you need to customize the CMP comparison function, the function is as follows:

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

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

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



Arrays of data structures and algorithms

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.