The Code demonstrates the differences between string and StingBuilder memory in C,

Source: Internet
Author: User

The Code demonstrates the differences between string and StingBuilder memory in C,

For the differences between string and StringBuilder, refer to MSDN. This article uses programs to demonstrate their differences in memory and their behavior is different.

 

// Demo string memory model

Namespace ConsoleApplication2
{
Class Program
{
Static void Main (string [] args)
{

String a = "1234 ";

String B = a; // a, and B point to the same address

Console. WriteLine ();

Console. WriteLine (B );

 

A = "5678 ";

Console. WriteLine ();

Console. WriteLine (B); // That B's value is not changed means string's value cann't be changed


Console. ReadKey ();
}

}
}

Output:

1234

1234

5678; change a's value, B's value is not changed

1234

 

// Demo StringBuilder's memory model

Namespace ConsoleApplication3
{
Class Program
{
Static void Main (string [] args)
{

StringBuilder a = new StringBuilder ("1234 ");
StringBuilder B = new StringBuilder ();
B =;
A. Clear ();
A. Append ("5678 ");
Console. WriteLine ();
Console. WriteLine (B );
Console. ReadKey ();
}


}
}

 

Output:

5678

5678


DEMO code for implementing seven sort algorithms in C Language

(1) "Bubble Method"

Everyone is familiar with the bubble method. The principle is to compare a [0] with the following elements in sequence. If a [0]> a [I], they are exchanged, always compare to a [n]. Similarly, processing a [1], a [2],... a [n-1] completes sorting. The code is listed below:

Void bubble (int * a, int n)/* defines two parameters: the first address of the array and the size of the array */

{

Int I, j, temp;

For (I = 0; I <n-1; I ++)

For (j = I + 1; j <n; j ++)/* pay attention to the upper and lower limits of the loop */

If (a [I]> a [j]) {

Temp = a [I];

A [I] = a [j];

A [j] = temp;

}

}

The bubble method is simple in principle, but its disadvantage is that the exchange frequency is large and the efficiency is low.

The following describes a method for choosing from the bubble method but more efficient method ".

(2) "Choice Method"

The loop Process of the selection method is the same as that of the bubble method. It also defines the Mark k = I, and then compares a [k] with the following elements in sequence. If a [k]> a [j], then k = j. finally, let's see if k = I is still valid. If not, exchange a [k], a [I], which saves a lot of useless exchanges and improves efficiency than the bubble method.

Void choise (int * a, int n)

{

Int I, j, k, temp;

For (I = 0; I <n-1; I ++ ){

K = I;/* assign a value to the mark */

For (j = I + 1; j <n; j ++)

If (a [k]> a [j]) k = j;/* k always points to the smallest element */

If (I! = K) {/* When k! = I is exchanged, otherwise a [I] is the smallest */

Temp = a [I];

A [I] = a [k];

A [k] = temp;

}

}

}

The selection method is more efficient than the bubble method, but when it comes to efficiency, it is not a "quick method". Now let's understand it.

(3) "Quick Method"

The quick method defines three parameters (array first address * a, to sort the subscript I of the starting element of the array, to sort the end element subscript j of the array ). it first selects an array element (generally a [(I + j)/2], that is, an intermediate element) as a reference, and places elements smaller than it to its left, put it bigger than it on the right. Then, use recursion to sort the Left and Right sub-arrays, and finally sort the entire array. The following code is analyzed:

Void quick (int * a, int I, int j)

{

Int m, n, temp;

Int k;

M = I;

N = j;

K = a [(I + j)/2];/* selected reference */

Do {

While (a [m] <k & m <j) m ++;/* Find elements larger than k from left to right */

While (a [n]> k & n> I) n --;/* Find elements smaller than k from right to left */

If (m <= n) {/* if the conditions are found and met, switch */

Temp = a [m];

A [m] = a [n];

A [n] = temp;

M ++;

N --;

}

} While (m <= n );

If (m <j) quick (a, m, j);/* apply recursion */

If (n> I) quick (a, I, n );

}

(4) "insert method"

Insert is an intuitive sorting method. It first sorts the two elements in the array header in order, and then inserts the following elements into an appropriate position. After inserting the array elements, the sorting is completed.

Void ins... the remaining full text>

Qingta C language animation demonstration program

You are too busy. I want to run the demonstration animation of the tower in graphic mode. Leave a contact information and I will give you the source program.


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.