C # algorithm Overview

Source: Internet
Author: User

Hill sorting

Hill sorting is to segment the group for insertion sorting.
If you want to improve your C # programming skills, we can discuss with each other.
For example, the following program does not implement polymorphism.

Using System;
Public class ShellSorter
{
Public void Sort (int [] list)
{
Int inc;
For (inc = 1; inc <= list. Length/9; inc = 3 * inc + 1 );
For (; inc> 0; inc/= 3)
{
For (int I = inc + 1; I <= list. Length; I + = inc)
{
Int t = list [I-1];
Int j = I;
While (j> inc) & (list [j-inc-1]> t ))
{
List [J-1] = list [j-inc-1];
J-= inc;
}
List [J-1] = t;
}
}
}
}
Public class MainClass
{
Public static void Main ()
{
Int [] iArrary = new int };
ShellSorter sh = new ShellSorter ();
Sh. Sort (iArrary );
For (int m = 0; m <= 13; m ++)
Console. WriteLine ("{0}", iArrary [m]);
}
}
It has been compiled.

Insert sort

Using System;
Public class InsertionSorter
{
Public void Sort (int [] list)
{
For (int I = 1; I <list. Length; ++ I)
{
Int t = list [I];
Int j = I;
While (j> 0) & (list [J-1]> t ))
{
List [j] = list [J-1];
-- J;
}
List [j] = t;
}

}
}
Public class MainClass
{
Public static void Main ()
{
Int [] iArrary = new int };
InsertionSorter ii = new InsertionSorter ();
Ii. Sort (iArrary );
For (int m = 0; m <= 13; m ++)
Console. WriteLine ("{0}", iArrary [m]);
}
}
It has been compiled and run. This is too simple and I will not detail it.

Select sort

Using System;
Public class SelectionSorter
{
// Public enum comp {COMP_LESS, COMP_EQUAL, COMP_GRTR };
Private int min;
// Private int m = 0;
Public void Sort (int [] list)
{
For (int I = 0; I <list. Length-1; ++ I)
{
Min = I;
For (int j = I + 1; j <list. Length; ++ j)
{
If (list [j] <list [min])
Min = j;
}
Int t = list [min];
List [min] = list [I];
List [I] = t;
// Console. WriteLine ("{0}", list [I]);
}

}
}
Public class MainClass
{
Public static void Main ()
{
Int [] iArrary = new int };
SelectionSorter ss = new SelectionSorter ();
Ss. Sort (iArrary );
For (int m = 0; m <= 13; m ++)
Console. WriteLine ("{0}", iArrary [m]);

}
}


======================================
Introduction to common sorting algorithms
□Non-brother posted on 23:00:00
1. Stable and non-stable sorting
Simply put, After all equal numbers are sorted by some sort method, they can still maintain their relative order before sorting.
This sorting method is stable. On the contrary, it is unstable.
For example, a group of numbers is sorted by a1, a2, a3, a4, and a5, where a2 is a4, Which is a1, a2, a4, a3, a5,
We can say that this sort is stable, because a2 is before a4, and it is still before a4. For example, a1, a4,
A2, a3, and a5 are not stable. 2. Inner and Outer sorting
In the sorting process, all the numbers to be sorted are in the memory and their storage order is adjusted in the memory;
In the sorting process, only part of the number is transferred to the memory, and the memory adjustment is used to store the number in the external storage. The sorting method is called external sorting.

3. Time and space complexity of the algorithm

The time complexity of an algorithm refers to the computing workload required to execute an algorithm.
The space complexity of an algorithm generally refers to the memory space required to execute this algorithm.
========================================================== ==========================================================
*/


/*
========================================================== ==========
Function: Select sorting
Input: array name (that is, the first address of the array), number of elements in the array
========================================================== ==========
*/
/*
========================================================== ================
A brief description of the algorithm concept:

In the number of groups to be sorted, the minimum number is selected to exchange with the number at the first position;
Then find the smallest number in the remaining number and exchange it with the number in the second position.
Until the second to last number is compared with the last number.

The sorting is unstable. Algorithm complexity O (n2) -- [the square of n]
========================================================== ==================
*/
Void select_sort (int * x, int n)
{
Int I, j, min, t;

For (I = 0; I <n-1; I ++)/* Number of times to be selected: 0 ~ N-2 n-1 times */
{
Min = I;/* assume that the current subscript is the smallest number of I, and then adjust it after comparison */
For (j = I + 1; j <n; j ++)/* Find the subscript of the smallest number in a loop */
{
If (* (x + j) <* (x + min ))
{
Min = j;/* If the number is smaller than the previous one, write down its subscript */
}
}

If (min! = I)/* If min changes in the loop, data needs to be exchanged */
{
T = * (x + I );
* (X + I) = * (x + min );
* (X + min) = t;
}
}
}


/*
========================================================== ==========
Function: insert and sort data directly.
Input: array name (that is, the first address of the array), number of elements in the array
========================================================== ==========
*/
/*
========================================================== ================
A brief description of the algorithm concept:

In the number of groups to be sorted, assume that the number of the preceding (n-1) [n> = 2] is already
Now we need to insert the nth number into the previous ordered number so that the n number
It is also sorted. This repeats until all the rows are sorted.

Direct insertion and sorting are stable. Algorithm time complexity O (n2) -- [n square]
========================================================== ==================
*/
Void insert_sort (int * x, int n)
{
Int I, j, t;

For (I = 1; I <n; I ++)/* Number of times to be selected: 1 ~ N-1 total n-1 times */
{
/*
Number of saved subscript I. Note: The subscript starts from 1 because
The first number is the number of subscripts with 0. There is no number before.
It is ordered.
*/
T = * (x + I );
For (j = I-1; j> = 0 & t <* (x + j); j --)/* Note: j = I-1, j --, here is the number of Subscripts for I. There is a sequence before it to find the insert position. */
{
* (X + j + 1) = * (x + j);/* Move back if the conditions are met. The worst case is that t is smaller than the number of 0 subscripts. It should be placed at the beginning, j =-1, exit the loop */
}

* (X + j + 1) = t;/* locate the placement location of the number with the subscript I */
}
}


/*
========================================================== ==========
Function: Bubble Sorting
Input: array name (that is, the first address of the array), number of elements in the array
========================================================== ==========
*/
/*
========================================================== ================
A brief description of the algorithm concept:

In the number of groups to be sorted, the number of all numbers in the range not sorted yet, from the top
The next step is to compare and adjust the adjacent two numbers, so that a large number will sink
Small to go up. That is, when the number of two adjacent parties is compared, they are sorted and sorted.
On the contrary, they are exchanged.

The following is an improved bubble algorithm, which records the number of sinks after each scan.
Position k, which can reduce the number of times the outer loop scan is performed.

Bubble Sorting is stable. Algorithm time complexity O (n2) -- [n square]
========================================================== ==================
*/

Void bubble_sort (int * x, int n)
{
Int j, k, h, t;

For (h = n-1; h> 0; h = k)/* loop to no comparison range */
{
For (j = 0, k = 0; j {
If (* (x + j)> * (x + j + 1)/* put large values behind the scenes, and small values to the front */
{
T = * (x + j );
* (X + j) = * (x + j + 1 );
* (X + j + 1) = t;/* Complete switching */
K = j;/* Save the position of the last sink. In this way, k is sorted. */
}
}
}
}


/*
========================================================== ==========
Function: Hill sorting
Input: array name (that is, the first address of the array), number of elements in the array
========================================================== ==========
*/
/*
========================================================== ================
A brief description of the algorithm concept:

In the direct insertion sorting algorithm, insert a number at a time to add only one node to the sequence,
This does not help insert the next number. If the comparison is relatively long distance (called
Incremental), so that when the number moves across multiple elements, a comparison may be eliminated.
Multiple elements are exchanged. D. L. shell was implemented in the sorting algorithm named by him in 1959.
This idea. The first to be sorted by the algorithm.

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.