I recently used some sorting algorithms to sort a few simple sorting algorithms, such as Bubble sorting, selection sorting, insertion sorting, parity sorting, and quick sorting, the code is implemented using C # code and passed the test. We hope to provide you with reference.
1. Bubble Sorting
Bubble Sorting is a Sort Algorithm of computers. Its time complexity is O (n ^ 2). Although it is less complex than heap sorting and fast sorting, It is O (nlogn, the base number is 2), but there are two advantages: 1: Low programming complexity, easy to implement; 2 is stable, the stability here means that the relative sequence of the same elements in the source sequence is still in the order after sorting, and the heap sorting and quick sorting are not stable.
Basic Concepts
The basic concept of BubbleSort: Compares two adjacent numbers in sequence, where the values are smaller and larger. In the first step, we first compare the numbers of 1st and 2nd, put the smaller ones at the front, put the larger ones at the back, and then compare the numbers of 2nd and 3rd, the smaller ones at the front, and the larger ones at the back, this continues until the last two numbers are compared, smaller ones are in the front, larger ones are in the back, and at the end of the first trip, the largest number is placed at the end. In the second round, the comparison is still started 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 second-to-last number (the first-to-last number is already the largest), and the second row ends, so that a new maximum number is obtained at the second-to-last position, so that the loop goes on, repeat the preceding process until the sorting is completed.
For example, there is a series of 10, 33, 2, 4, 55, 6, 12, 34,456, 66, 43, 23, 65, 1,345, 61, 76, 31, 43, 76.
After the first sorting: 1,345, 76,456
After the second sorting: 345,456
After the third sorting: 345,456
After the fourth sorting: 345,456
After the fifth sorting: 345,456
After the sixth sorting: 345,456
After the seventh sorting: 345,456
After the eighth sorting: 345,456
After the ninth sorting: 345,456
After the tenth sorting: 345,456
After sorting 11th times: 345,456
After sorting 12th times: 345,456
After sorting 13th times: 345,456
In this way, the sequence becomes an ordered sequence after 13-byte sorting.
The specific implementation code is:
Private static void BubbleSort (int [] R)
{
Int len = R. Length;
Bool flag = false;
For (int I = 0; I <len-1; I ++)
{
Flag = false;
For (int j = 0; j <len-i-1; j ++)
{
If (R [j]> R [j + 1])
{
Swap (ref R [j], ref R [j + 1]);
Flag = true;
}
}
If (! Flag)
{
Break;
}
}
}
Private static void Swap (ref int left, ref int right)
{
Int temp = left;
Left = right;
Right = temp;
}
2. Select sort
Select the smallest (largest) element from the elements to be sorted, and place the elements in sequence at the end of the sorted series until the elements to be sorted are distributed, the selected sorting is unstable.
Basic Concepts
The series with nelements can be directly sorted by n-1 troughs to obtain an ordered result. The initial State ordered area is empty, and the disordered area is R [1... n].
1st sorting in unordered zone R [1... n] select the R [k] element with the smallest keyword and exchange it with the R [1] of the unordered zone, so that R [1... 1] and R [2... n] is changed to a new ordered area, and the number of elements in the unordered area minus 1.
At the beginning of the I-th sorting, the current ordered and disordered areas are R [1 .. I-1] and R (1 ≤ I ≤ N-1), respectively ). This sort selects the record R [k] with the smallest keyword from the current unordered area, and exchanges it with the 1st records R in the unordered area,
Change R [1. I] and R to a new ordered area with one more record count and a new disordered area with one fewer record count. In this way, the direct sorting of files with n records can be directly selected through n-1 to obtain the ordered results.
For numbers of 10, 33, 345, 76, 76
After the first sorting :,
After the second sorting :,
After the third sorting :,
After the fourth sorting :,
After the fifth sorting :,
After the sixth sorting :,
After the seventh sorting :,
After the eighth sorting :,
After the ninth sorting: 43,456
After the tenth sorting: 43,456
After sorting 11th times :,
After sorting 12th times :,
After sorting 13th times: 76,345,456, 76
After sorting 14th times: 456,345
After sorting 15th times: 345,456
Finally, after 15 sorting tasks, it becomes an ordered series.
The specific implementation code is:
Private static void SelectSort (int [] R)
{
Int len = R. Length;
Int min = 0;
For (int I = 0; I <len-1; I ++)
{
Min = I;
For (int j = I + 1; j <len-1; j ++)
{
If (R [min]> R [j])
{
Min = j;
}
}
Swap (ref R [I], ref R [min]);
}
}
3. Insert sorting
Insert sorting is a stable algorithm with the time complexity O (n ^ 2). It is suitable for sorting a small amount of data. The basic operation of the insert algorithm is to insert a data into the ordered data that has been sorted, so as to obtain a new ordered data with a number plus 1. The insert algorithm divides the array to be sorted into two parts: the first part contains all elements of the array, except the last element, and the second part only contains this element, after sorting the first part, insert the last element into the ordered part.
Basic Ideas
1: Each processing operation compares the first element of an unordered sequence with the element of an ordered sequence one by one, finds the proper insertion position, and inserts the element into the proper position of the ordered sequence.
2: Sorting starts from the Order R [1] and the unordered R [2... n.
3: when processing the I-th element (I = 2, 3 ,..., N), number of columns {R1, R2 ,... the Ri-1} is ordered, and the series {Ri, Ri + 1 ,... rn} is unordered. Use Ri and {R1, R2 ,... ri-1} compare one by one, locate the appropriate location, insert Ri,
4: Repeat the third part to perform n-I insertion, And the arrays are all sorted.
For numbers of 10, 33, 345, 76, 76
After the first sorting: 34,456, 33, 1,345, 76
After the second sorting: 34,456, 1,345, 76
After the third sorting: 34,456, 1,345, 76
After the fourth sorting: 34,456, 1,345, 76
After the fifth sorting: 34,456, 1,345, 76
After the sixth sorting: 55,456, 1,345, 76
After the seventh sorting: 55,456, 1,345, 76
After the eighth sorting: 1,345, 76
After the ninth sorting: 66,456, 1,345, 76
After the tenth sorting: 1,345, 76, 76
After sorting 11th times: 66,456, 1,345, 76
After sorting 12th times: 456,345, 76
After sorting 13th times: 345,456, 76
After sorting 14th: 66,345,456
After sorting 15th times: 345,456, 76
After sorting 16th times: 76,345,456
After sorting 17th times: 345,456, 76
After sorting 18th times: 76,345,456
After a total of 18 sorting times, the array is all ordered.
Implementation Code:
Private static void InsertSort (int [] R)
{
Int len = R. Length;
Int j = 0;
Int temp = 0;
For (int I = 1; I <len; I ++)
{
Temp = R [I];
J = I-1;
While (j> = 0 & temp <R [j])
{
R [j + 1] = R [j];
J --;
}
R [j + 1] = temp;
}
}
4. Quick sorting
Quick sorting is an improvement of Bubble sorting. Its basic idea is to divide the series to be sorted into two independent parts by one sort, all the data in one part is smaller than all the data in the latter part. Then, the two parts are sorted in a fast manner using this method. The whole sorting process can be performed recursively, the entire sequence reaches the order level. Fast sorting is not a stable sorting.
Algorithm process
Set the array to be sorted to R [1... n], first select an element (usually the first element) as the key element, and then put all the elements smaller than it in front, and all those larger than it in the back, this process becomes a quick sort.
1. Set two variables low and high. When sorting starts, low = 0 and high = n-1;
2. Take the first array element as the key, that is, key = R [low];
3. search forward from high, that is, search forward from the back, high = high-1, find the first value less than the key R [high], and exchange with R [low,
4. start from low and start Backward Search, that is, start Backward Search from the beginning, low = low + 1, find the first value greater than the key R [low], and switch in R [high.
5. Repeat 3, 4 until low = high.
For numbers of 10, 33, 345, 76, 76
Initial key data key = 10;
After the first exchange: 1, 33, 2, 4, 55, 6, 12, 34,456, 66, 43, 23, 65, 1,345, 61, 76, 31, 43, 76
After the second exchange: 1, 33, 2, 4, 55, 6, 12, 34,456, 66, 43, 23, 65, 33,345, 61, 76, 31, 43, 76
After the third exchange: 1, 6, 2, 4, 55, 6, 12, 34,456, 66, 43, 23, 65, 33,345, 61, 76, 31, 43, 76
After the fourth exchange: 1, 6, 2, 4, 55, 55, 12, 34,456, 66, 43, 23, 65, 33,345, 61, 76, 31, 43, 76
So low = high = 4
Then, set R [low] = key.
In this way, after the first fast sorting, the sequence becomes {34,456, 33,345} 10 {, 76}
In this way, {34,456, 2, 4} and {33,345,} are sorted quickly and repeat this step, in the end, the entire array is ordered.
The specific implementation code is:
Private static void QuickSort (int [] R, int low, int high)
{
Int optional tloc = 0;
If (low
{
Required tloc = Partition (R, low, high );
QuickSort (R, low, repeated tloc-1 );
QuickSort (R, repeated tloc + 1, high );
}
}
Private static int Partition (int [] R, int low, int high)
{
Int temp = R [low];
While (low {
While (low {
High --;
}
R [low] = R [high];
While (low {
Low ++;
}
R [high] = R [low];
}
R [low] = temp;
Return low;
}
// Fast non-recursive sorting
Public static void QuickSort (int [] R, int Low, int High, Stack Stack)
{
Int low = Low;
Int high = High;
Int temp = R [low];
While (high> low)
{
While (low {
High --;
}
If (high> low)
{
R [low] = R [high];
R [high] = temp;
}
While (low {
Low ++;
}
If (high> low)
{
R [high] = R [low];
R [low] = temp;
}
If (low = high)
{
If (Low <low-1)
{
Stack. Push (Low );
Stack. Push (low-1 );
}
If (High> low + 1)
{
Stack. Push (low + 1 );
Stack. Push (High );
}
}
}
}
Test code:
Static void Main (string [] args)
{
Int [] arry = new int [] {10, 33, 2, 4, 55, 6, 12, 34,456, 66, 43, 23, 65, 1,345, 61, 76, 31, 43, 76 };
Stack S = new Stack ();
S. Push (0 );
S. Push (arryk. Length-1 );
While (s. Count> 0)
{
Int low = s. Pop ();
Int high = s. Pop ();
If (low> high)
{
Temp = low;
Low = high;
High = temp;
}
QuickSort (arryk, low, high, s );
}
Console. ReadLine ();
}
After recursive and non-recursive sorting of 0.1 million random data records, it is found that the speed of non-recursive methods is about 40 times faster than that of recursive methods.
5. How to retrieve the maximum and minimum values of an unordered Array Using a function requires the lowest time complexity and the least space
The specific algorithm is as follows:
Private static int [] GetMaxMin (int [] R)
{
Int len = R. Length;
Int min = R [0];
Int max = R [0];
For (int I = 1; I <len; I ++)
{
If (min> R [I])
{
Min = R [I];
}
If (max <R [I])
{
Max = R [I];
}
}
Int [] res = new int [2];
Res [0] = min;
Res [1] = max;
Return res;
}
6. For known arrays, store one hundred numbers randomly and place the odd numbers on the left and the even numbers on the right. The specific algorithm is as follows:
Private static void SortNumber (int [] R)
{
Int high = R. Length-1;
Int low = 0;
Int temp = R [low];
While (low {
While (low {
High --;
}
R [low] = R [high];
While (low {
Low ++;
}
R [high] = R [low];
}
R [low] = temp;
}
7. Binary Search Algorithm
The binary search algorithm is also called a half-fold query. The advantage is that the query speed is fast and the average performance is good. The disadvantage is that the given array must be ordered and difficult to insert or delete, therefore, the binary search is used to find frequently ordered tables that do not change frequently. First, assume that the array is an ordered table in ascending order. Compare the elements in the middle of the table with the given elements to be searched. If they are equal, the query is successful, otherwise, the table is divided into two Word tables by using the intermediate position. If the element recorded in the intermediate position is greater than the given data, the table is searched in the previous Word Table, otherwise, search in the Word Table. Repeat the preceding process until the child table is found or does not exist.
The specific implementation code is as follows:
Private static int BinarySearch (int [] R, int arg)
{
Int low = 0;
Int high = R. Length-1;
While (low {
Int middle = (low + high)/2;
If (arg = R [middle])
{
Return middle;
}
Else if (arg <R [middle])
{
High = middle-1;
}
Else
{
Low = middle + 1;
}
}
Return-1;
}