Array of data structures and algorithms

Source: Internet
Author: User

Array of data structures and algorithms

The basic concept of array: array is the simplest and most commonly used data structure, but there are also some considerations:

(1) array allocation method and storage location;

(2) initialization;

(3) Advanced definition of arrays in different languages;

(4) multi-dimensional array;

Array allocation method in C/C ++:

(1) int a [10];

It is applicable to situations where the array length is known or not sensitive to the array length, such as defining a string.

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

It is used in C and is suitable for dynamically applying the length of an array. Remember to be free at last to prevent memory leakage.

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

Used in C ++. Delete.

Virtual address space of a process:

.

In the process of running a process, the address is divided into many segments. There are two important parts: runtime heap and user stack. The dynamically applied array (malloc and new) will be in the runtime heap, and the constant array (int a [10]) will be placed in the user stack. The array in the heap during running is permanent. If you do not release the array, it will exist permanently. Therefore, memory leakage will occur. The stack space is temporary and will be recycled as the function is called and returned. Therefore, we cannot return the address of a local variable, especially the first address of a constant array, because this address will be recycled after use. Therefore, if a function is modified inside the function and you want to continue using it outside the function, you need to use a dynamically allocated array.

Array initialization in C/C ++

In programming, many bugs are caused by direct reference of uninitialized variables. The initialization of an array is as follows:

(1) Automatic initialization of global arrays;

(2) If the malloc mode is not initialized, you can use memset (which can be initialized to 0 or-1) and fill for initialization;

(3) The new method must be determined by the compiler;

The basic array directly operates on the memory, which is highly efficient, but lacks rich APIs. Therefore, advanced languages often have corresponding advanced implementations. Use the vector in STL in C/C ++.

A multi-dimensional array is an extension of a one-dimensional array. A two-dimensional array can be used to describe a matrix or graph, as shown below:

 

Int a1 [10] [10]; // the two dimensions of the array must be determined; int ** a2; // The two dimensions are not determined; int * a3 [10]; // each dimension is allocated separately, and the length can be inconsistent. vector
 
  
A4 [10]; // can only be used in C ++; vector
  
   
> A5; // it can only be used in C ++;
  
 

When accessing multi-dimensional arrays, pay attention to the lack of cache. Arrays can be operated by row or by column. The row-based operation method is better than the column-based operation method. Because the two-dimensional array is loaded to the cache by row, when accessing a row, the elements of this row are already in the cache. However, column-based access is different. Generally, only one element is in the cache and other elements are missing. Therefore, column-based access may cause serious cache defects and seriously affect performance.

Functions of arrays in sorting

For the seven commonly used sorting tasks, I have mentioned in my previous blogs, including code implementation. Arrays play a very important role in sorting. Here is a simple sorting Overview:

Complexity is O (n ^ 2 ):

(1) Insert sorting-the highest complexity is O (n ^ 2;

(2) The selection and Bubble sorting are slow and usually appear in the interview questions.

Complexity is O (N * logN ):

(1) Fast sorting: the fastest speed in comparison-based sorting;

(2) Merge Sorting: it is rarely used in practice and often used in interviews;

(3) Heap sorting: it is rarely used in practice, but it is often used;

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

(1) Base sorting-complexity O (n + r), where r is the base;

(2) counting sorting-complexity O (n + k), where k is the element range;

However, in actual development, C/C ++ comes with the sorting function:

(1) There is a qsort function in the C language and a custom cmp comparison function is required. The function format is as follows:

Int cmp (const void * a, const void * B );

(2) In C ++, the STL comes with the sorting function sort. The call method is as follows:

Sort (a. begin (), a. end (), [greater () | Less ()]);

Related Article

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.