C # basic parsing-IV (array and sorting)

Source: Internet
Author: User

Today, let's discuss arrays and sorting in C. First of all, I would like to thank you for your discussion and attention. This is undoubtedly my greatest support. We can find and solve the problem only after discussing it, in this way, we can constantly improve ourselves. Here, I want to say a few more words. In my previous message, I saw that my article was relatively simple. I 'd like to explain it to you, my articles are the basis of some basic information and are suitable for newcomers to study and learn (I am also a newbie). I hope that the old birds and masters can give correct guidance and help, because we all climb up one step...
Okay! Let's go to today's topic ----- array and sorting. Today's content is not much, but some things are more important, such as Bubble sorting. Bubble Sorting seems to have asked a lot during the interview! Haha! Let's take a look at the one-dimensional array. What is array? An array refers to a group of variables with the same name and type. each variable in the array is called an array element. With arrays, You can reference a series of variables with the same name and use the index number (subscript) to identify them. In many cases, Arrays can be used to shorten and simplify the program.
We use examples to talk about it:
1 class Program
2 {
3 static void Main (string [] args)
4 {
5 string name1 = "People Union ";
6 string name2 = "Orc tribe ";
7 string name3 = "undead ";
8 string name4 = "Night Genie ";
9 Console. WriteLine ("First player race: {0}", name1 );
10 Console. WriteLine ("second player race: {0}", name2 );
11 Console. WriteLine ("third player race: {0}", name3 );
12 Console. WriteLine ("fourth player race: {0}", name4 );
13 Console. ReadKey ();
14}
15}

Running result:



 

As you can see, the above Code is troublesome and awkward. Let's write it using Arrays:
1 class Program
2 {
3 static void Main (string [] args)
4 {
5 // declare four worthwhile Arrays
6 string [] name = new string [4] {"clan alliance", "Orc tribe", "undead", "Night Genie "};
7 for (int I = 0; I <4; I ++) // cyclically 4 times
8 {
9 // output each player
10 Console. WriteLine ("the {0} player race: {1}", I + 1, name [I]);
11}
12 Console. ReadKey ();
13}
14}

Running result:



 

In this example, we can clearly see the benefits of using arrays, which not only greatly reduces the number of code, but also allows more values to be operated through loops, in this way, we can write more beautiful code.
To better understand the array, let's take a look at its syntax structure:
Array type [] array name = new data type [array length];
The preceding example shows how to create an array. For example, int [] num = new int [5]; // creates an array that contains five integers.
New is the keyword, that is, the array is allocated a storage space in the memory. The values (elements) in the array must be of the same type. The array Length indicates the maximum value (element) in the array ). We can also initialize the array, for example, int [] num = new int [5] {12, 32, 24, 55, 13, 33}. The array contains several values, so the length of the array is. If the array length is 5 and the array contains 6th values, the program will report an error. This is what we call the array out of bounds.
It is necessary to briefly describe the Length of the array. Generally, the Length of the array is represented as array name. Length, so that we can get the Length of the array. Let's look at an example:
1 class Program
2 {
3 static void Main (string [] args)
4 {
5 // initialize a String Array
6 string [] name = {"clan alliance", "Orc tribe", "undead", "Night Genie "};
7 // The cycle starts from 0, representing the subscript www.2cto.com
8 for (int I = 0; I <name. Length; I ++) // (^ o ^) note! Length is here!
9 {
10 // determine whether the value of an element in this array is an orc tribe.
11 if (name [I] = "Orc tribe ")
12 {
13 Console. WriteLine ("number {0} is {1 }. ", I + 1, name [I]);
14 break;
15}
16}
17 Console. ReadKey ();
18}
19}

Running result:



 

The above is the usage of Length. Length is used to get the Length of a number. In many cases, we cannot know the Length of the array, so we have an array name. length is much more convenient.
Next, let's take a look at sorting. for sorting, the first thing we think of is the Bubble sorting. Next, let's take a look at the Bubble sorting, first, let's take a look at the concept of Bubble Sorting (the concept comes from Baidu Library ):
Compare two adjacent numbers in sequence, place decimal places in front, and place large numbers in the back. That is, first compare the numbers of 1st and 2nd, and put the decimal places before and after the large numbers. Then compare the numbers of 2nd and 3rd, place the decimal places before and after the large number, and continue until the last two digits are compared. Place the decimal places before and after the large number. Repeat the above process and compare it from the first logarithm (because of the exchange of 2nd numbers and 3rd numbers, the number of 1st numbers is no longer less than 2nd, always compare to the adjacent number before the maximum number. Place the decimal number before, place the large number, and end the second row. In the second to last number, a new maximum number is obtained. So on until the sorting is completed.
Because the sorting process always places decimal places forward and large numbers backward, it is equivalent to bubbles rising, so it is called Bubble sorting.
The above is the concept of Bubble sorting. After all, it is a concept. I don't think it can be explained more clearly. Well, let's talk about it using examples:
1 class Program
2 {
3 static void Main (string [] args)
4 {
5 int [] change = {23, 11, 54, 33, 7 };
6 int length = change. Length; // obtain the length of the array.
7 int I;
8 Console. WriteLine ("before Bubble Sorting :");
9 for (I = 0; I <length; I ++) // The value before sorting is output cyclically.
10 {
11 Console. WriteLine ("{0}", change [I]);
12}
13 // sort bubbles in descending order)
14 for (I = 0; I <length-1; I ++)
15 {
16 for (int j = 0; j <length-I-1; j ++)
17 {
18 // if the two adjacent elements in the array are larger than those in the latter, they are exchanged.
19 if (change [j] <change [j + 1])
20 {
21 int empty = change [j];
22 change [j] = change [j + 1];
23 change [j + 1] = empty;
24}
25}
26}
27 Console. WriteLine ("after Bubble Sorting :");
28 for (I = 0; I <length; I ++)
29 {
30 Console. WriteLine ("{0}", change [I]); // cyclically outputs the Bubble sorting result
31}
32 Console. ReadKey ();
33}
34}

Running result:



 

There are many ways to sort arrays. The above is the most common method. Of course, there are two methods in the Array class to complete sorting. Let's take a look at these two methods later, next let's take a look at the syntax. As long as you understand the syntax, you can sort it by yourself.
Bubble sort Syntax:
For (int I = 0; I <array length-1; I ++)
{
For (int j = 0; j <array length-I-1; j ++)
{
If (array name [j] <array name [j + 1])
{
Int empty = array name [j];
Array name [j] = array name [j + 1];
Array name [j + 1] = empty;
}
}
}

The above syntax is sort in descending order. if you want to sort in ascending order, set if (array name [j] <array name [j = 1]). you can change the minor sign "<" to the minor sign ">.
We have just mentioned two sorting methods in the Array class. Next we will briefly introduce these two methods in the Array class.
The method for implementing the Ascending Order in Array is Array. sort (Array name), such as Array. sort (change ). of course, there are also arrays in descending order. reverse (Array name), such as Array. reverse (change), which is used to Reverse the sorting of arrays.
Next we will put the two methods into the instance to see the effect:
1 class Program
2 {
3 static void Main (string [] args)
4 {
5 int [] change = {23, 11, 54, 33, 7 };
6 int length = change. Length; // obtain the length of the array.
7 int I;
8 Console. WriteLine ("before Array sorting :");
9 for (I = 0; I <length; I ++) // The value before sorting is output cyclically.
10 {
11 Console. WriteLine ("{0}", change [I]);
12}
13 // The following is an Array. Sort in ascending order
14 Array. Sort (change );
15 Console. WriteLine ("Array. Sort after sorting :");
16 for (I = 0; I <length; I ++)
17 {
18 Console. WriteLine ("{0}", change [I]); // cyclically output the results of Array. Srot Ascending Order
19}
20 // The following is the descending order of Array. Reverse
21 Array. Reverse (change );
22 Console. WriteLine ("Array. Reverse :");
23 for (I = 0; I <length; I ++)
24 {
25 Console. WriteLine ("{0}", change [I]); // cyclically outputs Array. Reverse descending order results
26}
27 Console. ReadKey ();
28}
29}

Running result:



 

The two methods in the Bubble sorting and Array classes achieve the same effect. However, compared with the Bubble sorting method, the Bubble sorting method is widely used, we still hope that everyone will adopt the Bubble sorting.


Conclusion: 1. Create an array;

2. Length of the array;

3. Bubble Sorting;

4. Two Methods of the Array class;

The above is the sharing. It may have a small amount of content, but as long as you have mastered it well, I think it is also a kind of progress. The accumulation of knowledge lies in it! Right! OK! The end!

This article is my personal opinion. If there are any imperfections or inaccuracies, you are welcome to criticize them.

 

From apple

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.