Set in C,

[Unlike arrays, a set is a combination of Variable-type and variable-quantity elements. These elements may share certain features and need to be operated in a certain way. Generally, to facilitate operations on these elements, the types are the same]

[Difference between a set and an array: An array is a continuous area of the same type of data, and a set can be discontinuous. Multiple Data Types]

[Foreach () is also applicable in the Collection]

1. Set definition:**ArrayList al = new ArrayList ();**// Define a set. The set is a class and needs to be referenced in the using System. Collections Library.

2. Set assignment:

Double fenshu = 0;

Al. add (fenshu = double. parse (Console. readLine (); // if you want to save a number, you need to compare the size in the future. You need to convert it to the Value Type before adding it to the set. Otherwise, it will compare the size as a string encoding, and an error will occur!

(You can also use. Add (); to assign values such as: al. Add (2); // data is in parentheses. The index number of the first data is 0 by default, and so on)

3. insert data in the collection :. insert (,); // the index number before the comma and the data after the comma (when there are three data in the Set and the inserted index number is 1, then the data with the original index number 1 will be 2, followed by one digit backward)

4. Remove Data from the set :. remove (); // The data to be removed from the set is filled in brackets (if there are two duplicates in the set during removal. remove () only Remove the number that appears for the first time)

. RemoveAt (); // The Index Number of the data to be removed from the set in parentheses.

5 ·. count; // view the length of the set. int type is returned.

6. Sort in the Set:. Sort (); // This Is A Sort in ascending order. If it is a Sort in descending order, use the Reverse (FLIP ---. Reverse ();) after the Sort method in ascending order ();)

7. Search for the index number of the element in the set: (Be sure to check whether the data type matches. If the returned value is-1, the index number of this element is not found)

Int s = al. IndexOf (); // The element to be searched in parentheses. The index number of the first appearance of this element

Int s1 = al. LastIndexOf (); // The element to be searched in parentheses. The index number of the last occurrence of this element

8. Clear the set:. Clear ();

9. Obtain the number of elements in the Set: Console. WriteLine (at. Count); // The number of output sets.

10. Copy the element data in the set and load it into the new set:

ArrayList xal = new ArrayList ();

Xal = (ArrayList) al. Clone ();

11. Determine whether a set contains the element data and return the bool value:

Bool B = al. Contains (); // you can check whether the elements in the set are included in the brackets.

----- Special set: Stack, Queue, and hash table (Hashtable)

The meaning of Stack heap: first-in-first-out, and later-in-first-out (the heap has no index)

1. Build Stack s = new. Stack ();

2. Value assignment: s. Push (1); // Push data into the heap

3. Output: Console. WriteLine (s. Pop ());

4. clear the set:. clear ();

5. string tanchu = s. Peek (). ToString (); // only obtain the last value, not removed

String tanchu = s. Pop (). ToString (); // Pop is the element that pops up and removes the last element.

6. Stack fuzhi = (Stack) s. Clone (); // value assignment set

7. Console. WriteLine (s. Count); // gets the number of elements in the set.

Queue first-in-first-out, and then-out

1. Build: Queue q = new Queue ();

2. int chu = int. Parse (q. Dequeue (). ToString (); // obtain the first element and remove it from the set.

3. int zhi = int. Parse (q. Peek (). ToString (); // read the first element without removing

4. bool d = q. Contains (5); // check whether the set Contains any element in the brackets and return the bool value.

A hash table (Hashtable) comes first and then comes first. A position that contains two values (,) is preceded by an index followed by an element.

1. Build Hashtable ht = new Hashtable ();

2. ht. Add (0, "aa"); // Add the bonding value to the hash table

3. ht. Remove (4); // Remove according to the Keys value in parentheses

4. Console. WriteLine (ht. Contains (4); // determines whether a key is included.

5. Output

Foreach (int I in ht. Keys) // Keys indicates the index

{

Console. WriteLine (I); // first-in-first-out, then-in-first-out

}

Foreach (int I in ht .. Values) //. Values indicates the element

{

Console. WriteLine (I); // first-in-first-out, then-in-first-out

}

What if I want to output indexes and elements at the same time?

Then:

// Output index numbers and elements using Enumeration

IDictionaryEnumerator ide = ht. GetEnumerator ();

While (ide. MoveNext ())

{

Console. WriteLine (ide. Key + "" + ide. Value );

}

6. convert a hash table to an Arraylist

ArrayList al = new ArrayList ();

Foreach (string j in ht. Values) // Values indicates the elements in the hash table.

{

Al. Add (j );

}

A simple program of C language Bubble Sorting

Main ()

{

Int I, j, temp;

Int a [10];

For (I = 0; I <10; I ++)

Scanf ("% d,", & a [I]);

For (j = 0; j <= 9; j ++)

{For (I = 0; I <10-j; I ++)

If (a [I]> a [I + 1])

{Temp = a [I];

A [I] = a [I + 1];

A [I + 1] = temp ;}

}

For (I = 1; I <11; I ++)

Printf ("% 5d,", a [I]);

Printf ("\ n ");

}

--------------

Bubble Algorithm

Algorithm Analysis and Improvement of Bubble Sorting

The basic idea of exchanging sorting is to compare the keywords of the records to be sorted in pairs. If the order of the two records is the opposite, the two records are exchanged until there is no reverse order record.

The basic concepts of application exchange sorting include Bubble sorting and quick sorting.

Bubble Sorting

1. Sorting Method

Vertically arrange the sorted record array R [1. n]. Each record R is considered as a bubble with the weight of R. key. According to the principle that a Light Bubble cannot be under a heavy bubble, scan the array R from the bottom up: Any Light Bubble scanned to a violation of this principle will make it "float" up ". This is repeated until the last two bubbles are light and heavy.

(1) initial

R [1. n] is an unordered area.

(2) First scan

The weights of two adjacent bubbles are compared from the bottom of the unordered area to the top. If the light bubbles are found to be in the lower and severe bubbles, the positions of the two bubbles are exchanged. That is, compare (R [n], R [n-1]), (R [n-1], R [N-2]),…, (R [2], R [1]); for each pair of bubbles (R [j + 1], R [j]), if R [j + 1]. key <R [j]. key, then the contents of R [j + 1] and R [j] are exchanged.

When the first scan is complete, the "lightest" bubble floated to the top of the interval, that is, the record with the smallest keyword is placed on the highest position R [1.

(3) second scan

Scan R [2. n]. When scanning is completed, the "light" bubble floated to the R [2] position ......

Finally, the sequential area R [1. n] can be obtained through n-1 scanning.

Note:

During the I-trip scan, R [1 .. I-1] and R [I.. n] are the current sequential and disordered areas, respectively. The scan continues from the bottom of the unordered area to the top of the area. When scanning is completed, the shortest bubbles in the area float to the top position R. The result is that R [1. I] is changed to a new ordered area.

2. Bubble sorting process example

Bubble Sorting of files whose keyword sequence is 49 38 65 97 76 13 27 49

3. Sorting Algorithm

(1) Analysis

Because each sort adds a bubble to the ordered area, there are n-1 bubbles in the ordered area after N-1 sort, in the disordered area, the bubble weight is always greater than or equal to the bubble weight in the ordered area. Therefore, the entire Bubble sorting process requires at most n-1 sorting.

If no bubble position exchange is found in a sorting, it means that all bubbles in the unordered area to be sorted meet the principle of being light and heavy. Therefore, the Bubble sorting process can be terminated after this sorting. Therefore, in the following algorithm, a Boolean exchange is introduced, which is set to FALSE before each sort starts. If an exchange occurs during the sorting process, set it to TRUE. Check exchange at the end of sorting. If exchange has not occurred, terminate the algorithm and no longer perform the next sorting.

(2) specific algorithms

Void BubbleSort (SeqList R)

{// R (l. n) is the file to be sorted. It uses bottom-up scanning to perform Bubble Sorting on R.

Int I, j;

Boolean exchange; // exchange flag

For (I = 1; I <G id = "1">

A simple program of C language Bubble Sorting

Main ()

{

Int I, j, temp;

Int a [10];

For (I = 0; I <10; I ++)

Scanf ("% d,", & a [I]);

For (j = 0; j <= 9; j ++)

{For (I = 0; I <10-j; I ++)

If (a [I]> a [I + 1])

{Temp = a [I];

A [I] = a [I + 1];

A [I + 1] = temp ;}

}

For (I = 1; I <11; I ++)

Printf ("% 5d,", a [I]);

Printf ("\ n ");

}

--------------

Bubble Algorithm

Algorithm Analysis and Improvement of Bubble Sorting

The basic idea of exchanging sorting is to compare the keywords of the records to be sorted in pairs. If the order of the two records is the opposite, the two records are exchanged until there is no reverse order record.

The basic concepts of application exchange sorting include Bubble sorting and quick sorting.

Bubble Sorting

1. Sorting Method

Vertically arrange the sorted record array R [1. n]. Each record R is considered as a bubble with the weight of R. key. According to the principle that a Light Bubble cannot be under a heavy bubble, scan the array R from the bottom up: Any Light Bubble scanned to a violation of this principle will make it "float" up ". This is repeated until the last two bubbles are light and heavy.

(1) initial

R [1. n] is an unordered area.

(2) First scan

The weights of two adjacent bubbles are compared from the bottom of the unordered area to the top. If the light bubbles are found to be in the lower and severe bubbles, the positions of the two bubbles are exchanged. That is, compare (R [n], R [n-1]), (R [n-1], R [N-2]),…, (R [2], R [1]); for each pair of bubbles (R [j + 1], R [j]), if R [j + 1]. key <R [j]. key, then the contents of R [j + 1] and R [j] are exchanged.

When the first scan is complete, the "lightest" bubble floated to the top of the interval, that is, the record with the smallest keyword is placed on the highest position R [1.

(3) second scan

Scan R [2. n]. When scanning is completed, the "light" bubble floated to the R [2] position ......

Finally, the sequential area R [1. n] can be obtained through n-1 scanning.

Note:

During the I-trip scan, R [1 .. I-1] and R [I.. n] are the current sequential and disordered areas, respectively. The scan continues from the bottom of the unordered area to the top of the area. When scanning is completed, the shortest bubbles in the area float to the top position R. The result is that R [1. I] is changed to a new ordered area.

2. Bubble sorting process example

Bubble Sorting of files whose keyword sequence is 49 38 65 97 76 13 27 49

3. Sorting Algorithm

(1) Analysis

Because each sort adds a bubble to the ordered area, there are n-1 bubbles in the ordered area after N-1 sort, in the disordered area, the bubble weight is always greater than or equal to the bubble weight in the ordered area. Therefore, the entire Bubble sorting process requires at most n-1 sorting.

If no bubble position exchange is found in a sorting, it means that all bubbles in the unordered area to be sorted meet the principle of being light and heavy. Therefore, the Bubble sorting process can be terminated after this sorting. Therefore, in the following algorithm, a Boolean exchange is introduced, which is set to FALSE before each sort starts. If an exchange occurs during the sorting process, set it to TRUE. Check exchange at the end of sorting. If exchange has not occurred, terminate the algorithm and no longer perform the next sorting.

(2) specific algorithms

Void BubbleSort (SeqList R)

{// R (l. n) is the file to be sorted. It uses bottom-up scanning to perform Bubble Sorting on R.

Int I, j;

Boolean exchange; // exchange flag

For (I = 1; I <G id = "1">