C # data structure and algorithm secrets 6

Source: Internet
Author: User

In this section, we will discuss two types of data structures that use a large number of data structures-strings and arrays.

First of all, what is a string, here is not a beef string, a mutton string, but a string. The most frequently used type in an application is a string. String is short for string. It is a special linear table. Its particularity is that the data elements in the string are character by character. Strings are widely used in many computer fields. For example, in the Compilation Program of assembly and advanced language, both the source program and the target program are strings. In the transaction processing program, the customer's information, such as name and address, and the goods name, origin and specification, are all processed as strings. In addition, strings have some characteristics. Therefore, the string is studied as a data structure. The specific situation is the string of customer information processing.

 

What are the basic concepts of a string? A string is a finite sequence consisting of n (n ≥ 0) characters. It is generally recorded as: S = "c1c2... CN (n ≥ 0) where S is the string name, double quotation marks are used as the separator of the string, and the Character Sequence caused by double quotation marks is the string

Value. Ci (1 ≤ I ≤ n) can be letters, numbers or other characters. n is the length of a string. When n = 0, it is called an empty string ). A subsequence composed of any consecutive characters in a string is called a substring of the string ). The string containing the substring is called as the primary string. The position of the first character of a substring in the primary string is the position of the substring. For example, if the string S1 "abcdefg" has a length of 7, the string S2 "cdef" has a length of 4, S2 is a child string of S1, and S2 is 3.
If the two strings are of the same length and contain the same characters, the two strings are equal. In C #, compare the two strings to see their language and culture information. The so-called language culture refers to the comparison between Chinese strings and Chinese strings, and between English strings and English strings. Parent string:

How to store strings and define classes? Because the characters in strings are stored continuously, the strings in C # are constant, that is, once a string is created, it cannot be extended, shortened, or changed to any character. The result is as follows:

 

 

Therefore, we will not discuss the chain storage of strings here, nor use interfaces to represent string operations. Similarly, the string is treated as a class named stringds. The name stringds is used to distinguish it from the string class of C. Class
Stringds has only one field, that is, the array data that stores the character sequence in the string. Because there are many string operations, the stringds class only contains some basic operations such as increase, empty, and ball length. The source code implementation for stringds is as follows:

Public class stringds
{
Private char [] data; // character array

// Indexer
Public char this [int Index]
{
Get
{
Return data [Index];
}
}

// Constructor
Public stringds (char [] ARR)
{

Data = new char [arr. Length];
For (INT I = 0; I <arr. length; ++ I)
{
Data [I] = arr [I];
}
}

// Constructor
Public stringds (stringds S)
{
For (INT I = 0; I <arr. length; ++ I)
{
Data [I] = s [I];
}
}

// Constructor
Public stringds (INT Len)
{
Char [] arr = new char [Len];
Data = arr;
}

// Obtain the string length
Public int getlength ()
{
Return data. length;
}

The length of a string is the number of characters in the string. You can evaluate the length of the string by calculating the length of the array data. The time complexity of the algorithm is O (1 ,:

 

// String comparison
Public int compare (stringds S)
{
Int Len = (this. getlength () <= S. getlength ())?
This. getlength (): S. getlength ());
Int I = 0;
For (I = 0; I <Len; ++ I)
{
If (this [I]! = S [I])
{
Break;
}
}

If (I <= Len)

{
If (this [I] <s [I])
{
Return-1;
}
Else if (this [I]> S [I])
{
Return 1;
}
}
Else if (this. getlength () = S. getlength ())
{
Return 0;
}
Else if (this. getlength () <S. getlength ())
{
Return-1;
}

Return 1;
}

If the two strings are of the same length and contain the same characters, the strings are equal and 0 is returned; if the character at the corresponding position of the string S is greater than the character of the string or if the length of the string S is greater than that of the string, and the character at the corresponding position in the length of the string is the same, returns-1, which is less than string s; returns 1 for other cases, which is greater than string S. The time complexity of this algorithm is that O (n) involves the traversal of string arrays. Specific pseudocode ,:

// Obtain the substring
Public stringds substring (INT index, int Len)
{
If (index <0) | (index> This. getlength ()-1)
| (LEN <0) | (LEN> This. getlength ()-index ))
{
Console. writeline ("position or length is error! ");
Return NULL;
}

Stringds S = new stringds (LEN );

For (INT I = 0; I <Len; ++ I)
{
S [I] = This [I + index-1];
}

Return S;
}

{
Stringds S1 = new stringds (this. getlength () +
S. getlength ());

For (INT I = 0; I <this. getlength (); ++ I)
{
S1.data [I] = This [I];
}

For (Int J = 0; j <S. getlength (); ++ J)
{
S1.data [This. getlength () + J] = s [J];
}

Return S1;
}

From the index position of the Main string, find the substring with the length of Len. If it is found, return the substring. Otherwise, return an empty string. It involves string traversal, so the time complexity is O (n) corresponding graph:

// String insertion
Public stringds insert (INT index, stringds S)
{
Int Len = S. getlength ();
Int len2 = Len + this. getlength ();
Stringds S1 = new stringds (len2 );

If (index <0 | index> This. getlength ()-1)
{
Console. writeline ("position is error! ");
Return NULL;
}

For (INT I = 0; I <index; ++ I)
{
S1 [I] = This [I];
}

For (INT I = index; I <index + Len; ++ I)
{
S1 [I] = s [I-Index];
}

For (INT I = index + Len; I <len2; ++ I)
{
S1 [I] = This [I-Len];

Return S1;
}

String insertion is to insert a string s at the index of a string. If the position meets the condition, the operation returns a new string. The length of the new string is the sum of the length of the string and the length of the string S, the first part of the new string is the character between the start character of the string and the index character, and the second part is the string S, part 2 is the character from the index position to the end position of the string. If the position does not match the condition, an empty string is returned. The time complexity is O (n). Specific operations:

// Delete a string
Public stringds Delete (INT index, int Len)
{
If (index <0) | (index> This. getlength ()-1)
| (LEN <0) | (LEN> This. getlength ()-index ))
{
Console. writeline ("position or length is error! ");
Return NULL;
}

Stringds S = new stringds (this. getlength ()-len );

For (INT I = 0; I <index; ++ I)
{
S [I] = This [I];
}

For (INT I = index + Len; I <this. getlength (); ++ I)
{
S [I] = This [I];
}

Return S;
}

The string is deleted from the main string from the consecutive Len characters starting from the index position of the string. If the position and length meet the conditions, this operation returns a new string. The length of the new string is the length of the original string minus Len, the first part of the new string is the character from the start of the original string to the index position, and the last part is the character from the index + Len position to the end of the original string. If the position and length do not match the condition, an empty string is returned. The corresponding time complexity is O (n ,:

 

}

This is my understanding of strings. Let's take a look at the understanding of arrays.

What is an array. Arrays are commonly used data structures and can be seen as the promotion of linear tables. As a data structure, an array is characterized by data elements in the structure that can be data with a certain structure or even an array, but belong to the same data type. Arrays are used as fixed types in many advanced languages.

An array is a finite sequence of n (n ≥ 1) data elements of the same data type. A one-dimensional array can be regarded as a linear table, while a two-dimensional array can be regarded as a one-dimensional array of "data elements are one-dimensional arrays". A three-dimensional array can be regarded as a one-dimensional array of "data elements are two-dimensional arrays, and so on. The figure is a two-dimensional array of m rows and n columns.

An array is a sorted set of data in a fixed format and quantity. Each data element is identified and accessed by a unique subscript. Generally, once an array is defined, the size and upper and lower bounds of each dimension cannot be changed. Therefore, you cannot insert or delete data elements in an array. Operations on arrays are generally:

1. Value operation: given a set of subscripts, read the corresponding data elements. The complexity of the algorithm is O (1)

2. Value assignment operation: a group of data elements is stored or modified. The complexity of the algorithm is O (1)
3. Clear operation: clears all data elements in the array. The complexity of the algorithm is O (1)
4. copy operation: Assign the data element of an array to another array. The complexity of the algorithm is O (n)
5. Sort operations: sort the data elements in the array, which requires that the data elements in the array are sortable; Hill sort, bubble sort, and so on. The complexity of the algorithm is O (n²)
6. Reverse operation: reverse the order of data elements in the array. Previously mentioned, please refer to C # data structure and algorithm secrets 1.

What is an array memory image,

Generally, the ordered storage structure is used to store the data elements in the array, because the elements in the array must be stored continuously. In essence, the computer memory is a one-dimensional array, and the memory address is the subscript of the array. Therefore, for a one-dimensional array, you can obtain its storage address based on the subscript of the array element, or access the elements in the one-dimensional array based on the subscript. For multi-dimensional arrays, You need to convert a multi-dimensional subscript expression into a one-dimensional subscript expression. When the rows and columns are fixed, a set of continuous storage units are used to store the elements in the array. There is an order Convention problem, which produces two storage methods: one is to store data in the order of row Order (followed by first), and the other is to store data in the order of column Order (followed by first column. The two storage methods of the Two-dimensional array in the figure are given.

The following uses the subscript of the element to calculate the address: When the row order is used for storage, the base address of the array is LOC (A11), and each data element occupies W storage units, then the physical address of A11 can be calculated by the following formula: LOC (AIJ) = LOC (A11) + (I-1) * n + J-1) * w this is because the array element AIJ has a I-1 row, each row has n data elements, and in row I there are J-1 elements before AIJ.

When storing in column order, the physical address of A11 can be calculated from the following formula: LOC (AIJ) = LOC (A11) + (J-1) * m + i-1) * w (4-2) this is because the array element AIJ has a J-1 column, each column has M data elements, and there are I-1 elements before AIJ in column J. The above formula shows that the storage position of an array element is a linear function under the underlying mark. Once the length of each dimension of the array is determined, the storage address of any element can be calculated and the time is equal. Therefore, the time required to access any element in an array is equal. Therefore, an array is a random storage structure. The time complexity is O (n2 ,:

This is my understanding of arrays.

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.