**ArticleDirectory**
- 3.2.1 sorting Overview
- 3.2.2 Bubble Sorting
- 3.2.3 insert sorting

Chapter 3 Array Structure

Storage-related data item groups are mostly software applicationsProgramThis can be achieved by using arrays. Arrays are similar in various development languages. The declaration and initialization of arrays are very convenient. In some cases, they can replace complex structures and classes. They are small in size, fast in access, and high in resource utilization, thanks to the popularity of developers, the rational use of arrays can help us get twice the result with half the effort. The following describes the concepts and usage of arrays in detail.

The focus of this chapter:

◆ Sorting Concept

◆ Bubble Sorting

◆ Insert sorting

3.2 sorting

C # sortingAlgorithmGenerally, loops and assignments are involved. Sorting can be used for simple statistics and classification, which is of great value. Here we will introduce two different C # sorting algorithms.CodeTo help you.

3.2.1 sorting Overview

Sorting is a type of operation that is often performed in a computer. Its purpose is to adjust a set of "unordered" record sequences to "ordered" record sequences. A good sorting method can effectively increase the sorting speed and improve the sorting effect. There are two main types of sorting: Internal sorting and external sorting.

If the sorting process can be completed without access to external storage, the Sorting Problem is called internal sorting;

Otherwise, if the number of records participating in sorting is large and the sorting process of the entire sequence cannot be completed in the memory, this sort problem is called external sorting.

The internal sorting process is a process of gradually expanding the sequence length of records.

There are many inner sorting methods. Different policies can be classified into five types: insertion sorting, selection sorting, exchange sorting, Merge Sorting, and allocation sorting.

Insert sorting mainly includes direct insertion sorting and Hill sorting. Selection sorting mainly includes direct selection sorting and heap sorting. Exchange sorting mainly includes Gas Bubble sorting and fast sorting.

3.2.2 Bubble Sorting

In many program designs, we need to sort a sequence to facilitate statistics. Common sorting methods include Bubble sorting, binary tree sorting, and selective sorting. Bubble Sorting has always been favored by its concise ideas and methods and high efficiency.

Basic Idea: Compare key codes of adjacent records. If the key codes recorded earlier are greater than the key codes recorded later, they are exchanged. Otherwise, they are not exchanged. There are N records in the list of sequence tables to be sorted. At the beginning, there is only one record list [0] In the subsequence. During the first sorting, compare list [1] With list [0]. If list [0] <= list [1], no sorting is required, otherwise, the values of list [0] and list [1] are exchanged. During the second sorting, list [2] and list [1] are compared in size, if list [2] <= list [1], the values of list [1] and list [2] are exchanged.

After the first row is complete, we will determine that the maximum number has been placed in the last row. Then, if the second row is arranged, the loop will be less than once. Therefore, Double Loops are required for bubbling, each time is compared once.

Original array: 21 54 76 12 34 44 12 15 28 3 10 68

First exchange: 21 54 76 12 34 12 15 3 28 10 68

Second exchange: 21 54 76 12 34 12 3 15 28 10 68

Third exchange: 21 54 76 12 34 3 12 15 28 10 68

Fourth exchange: 21 54 76 12 34 3 44 12 15 28 10 68

Fifth exchange: 21 54 76 12 3 34 44 12 15 28 10 68

Sixth exchange: 21 54 76 3 12 34 12 15 28 10 68

Seventh exchange: 21 54 3 76 12 34 12 15 28 10 68

Eighth exchange: 21 3 54 76 12 34 12 15 28 10 68

Ninth exchange: 3 21 54 76 12 34 12 15 28 10 68

First sort: 3 21 54 76 12 34 12 15 28 10 68

Tenth exchange: 3 21 54 76 12 34 12 15 10 28 68

11th exchanges: 3 21 54 76 12 34 12 10 15 28 68

12th exchanges: 3 21 54 76 12 34 44 10 12 15 28 68

13th exchanges: 3 21 54 76 12 34 10 44 12 15 28 68

14th exchanges: 3 21 54 76 12 10 34 12 15 28 68

......

Second sort: 3 10 21 54 76 12 34 12 15 28 68

Third sort: 3 10 12 21 54 76 12 34 44 15 28 68

......

Last sort: 3 10 12 12 15 21 28 34 44 54 68 76

Here is an example:

01 using system;

02 public class testbubblesort

03 {

04 public static void main (string [] ARGs)

05 {

06 int [] list = new int [] {2, 6, 3, 8, 2, 7, 3}; // defines an array of the int type

07 bubblesort (list); // sort

08 for (INT I = 0; I <list. length; I ++) // cyclically outputs the sorted Array

09 {

10 console. writeline (list [I]); // output result

11}

12}

13 public static void bubblesort (INT [] list) // Bubble Sorting Algorithm

14 {

15 For (INT I = 0; I <list. length; I ++) // control the number of switches

16 {

17 For (Int J = I; j <list. length; j ++) // control the nth round of exchange

18 {

19 if (list [I] <list [J]) // compare the size of items before and after the array. If it is small, it is exchanged.

20 {

21 int temp = list [I]; // create a temporary variable to save data

22 list [I] = list [J]; // put the following data in front

23 list [J] = temp; // put the preceding data behind

24}

25}

26}

27}

28}

In the above Code, because the sorting process always places a decimal number forward and a large number backward, it is equivalent to a bubble rising, so it is called a Bubble sorting. Imagine the sorted array R [1 .. n] vertical erect, each data element is considered as a bubble with weight, according to the principle that light bubbles cannot be under heavy bubbles, scanning array R from bottom up, when a light bubble that violates this principle is scanned, it is made to "float" up, so it is repeated until the last two bubbles are both light and heavy.

If the initial status of the record sequence is "positive", the Bubble sorting process only needs to sort the sequence. During the sorting process, only n-1 comparisons are required and records are not moved. Otherwise, if the initial status of the record sequence is "backward", You need to perform n (n-1)/2 comparisons and record movement. Therefore, the total time complexity of Bubble Sorting is (N * n ).

Advantage: stable, known as the number of comparisons;

Disadvantage: It is slow. Only two adjacent data can be moved at a time, and the number of data moves is large.

3.2.3 insert sorting

Basic Idea: insert the records to be sorted in sequence to the proper position of the sorted records subsequence based on their key codes. There are N records in the list of sequence tables to be sorted. At the beginning, there is only one record list [0] In the subsequence. During the first sorting, compare list [1] With list [0]. If list [0] <= list [1], no sorting is required. Otherwise, the position is changed, in the second sorting, list [2] and list [1] compare the size. If list [2] is smaller than list [1], then it is compared with list [0, insert it to the appropriate location.

Original array: 23 53 73 11 34 44 11 15 28 3 10 66

Bytes

First insert: 23 53 73 11 34 44 11 15 28 3 10 66

Bytes

Second insert: 23 53 73 11 34 44 11 15 28 3 10 66

Certificate -------------- |

Third insert: 11 23 53 73 34 44 11 15 28 3 10 66

Certificate -------- |

Fourth insert: 11 23 34 53 73 44 11 15 28 3 10 66

Certificate -------- |

Fifth insert: 11 23 34 44 53 73 11 15 28 3 10 66

Certificate ---------------------------- |

Sixth insert: 11 11 23 34 44 53 73 15 28 3 10 66

Certificate ---------------------- |

Seventh insert: 11 11 15 23 34 44 53 73 28 3 10 66

......

Final results: 3 10 11 11 15 23 28 34 44 53 66 73

Here is an example:

01 using system;

02 public class testinsertionsort

03 {

04 public static void main (string [] ARGs)

05 {

06 int [] list = new int [] {2, 6, 3, 8, 2, 7, 3}; // defines an array of the int type

07 insertionsort (list); // sort

08 for (INT I = 0; I <list. length; I ++) // cyclically outputs the sorted Array

09 {

10 console. writeline (list [I]); // output result

11}

12}

13 public static void insertionsort (INT [] list) // insert a Sorting Algorithm

14 {

15 For (INT I = 1; I <list. length; I ++) // starting from the first element, this element can be considered sorted

16 {

17 int T = list [I];

18 Int J = I;

19 while (j> 0) & (list [J-1]> T) // scan from the back to the front of the sorted element sequence

20 {

21 list [J] = list [J-1]; // Insert a new element to this position

22 -- J;

23}

24 list [J] = T;

25}

26}

27}

In the above Code, if the comparison operation costs more than the exchange operation, you can use the binary search method to reduce the number of comparison operations. This algorithm can be considered as a variant of insert sorting, called Binary Search sorting.

In general, insert sorting is implemented on the array using in-place. The specific algorithm is described as follows:

1. Starting from the first element, this element can be considered to have been sorted

2. Retrieve the next element and scan the sorted element sequence from the back to the front.

3. If the element (sorted) is greater than the new element, move the element to the next position.

4. Repeat Step 3 until you find the position where the sorted elements are smaller than or equal to the new elements.

5. Insert new elements to this position

6. Repeat Step 2.

If the target is to sort the sequences of n elements in ascending order, there are best cases and worst cases of adopting insert sorting. The best case is that the sequence is already in ascending order. In this case, the comparison operation needs to be performed (n-1) times. The worst case is that the sequence is sorted in descending order. At this time, we need to compare N (n-1)/two times. The value assignment operation for insert sorting adds (n-1) the number of comparison operations. On average, the complexity of the insert sorting algorithm is (N * 2 ). Therefore, insert sorting is not suitable for sorting applications with large data volumes. However, if the amount of data to be sorted is small, for example, if the order is smaller than, insertion sorting is still a good choice.

Advantages: stable and fast;

Disadvantage: the number of comparisons is not certain. The less the number of comparisons, the more data is moved after the inserted point. Especially when the total amount of data is large, the linked list can solve this problem.