Sorting algorithms (insertion sort, bubble sort, selection sort, Hill)

Today describes some sorting algorithms, as the most basic part of the algorithm, master a variety of sorting algorithms and complexity of the various sorting algorithms guidance is necessary.

Insertion Sort

As the name suggests, insertion sort is first selected from a number in the array to be sorted, and then forward, to find it in the proper position, both the number of backward position behind once, and then inserted into the selected number of the herd. The principle we just play cards inserted card is the same.
Five random numbers in the following example:
23154
is first inserted: 23154 (3 greater than 2, do not move)
the second insertion: 12354 (1 inserted into the the first one, the 2,3 shift)
third inserted: 12,354 (larger than 5 5 not preceded, immobile)
fourth inserted: 12345 (4 into the fourth, after 5 shift)

void charupaixu(int *a,int n)
{
	for(int i=1;i<n;i++)
	{
		int x=a[i];
		int j=i-1;
		while(j>=0&&a[j]>x)
		{
			a[j+1]=a[j];
			j--;
		}
		a[j+1]=x;
	}
}

Complexity of the algorithm is O (N²)

Bubble Sort

The name of the bubble sort is very vivid, I believe you read the following explanation'll understand why it's called that name.
Insertion sorting different bubble sort, insertion sort is to select an object to be compared with a plurality of objects, and the bubble sort is the two elements compared to each other, for example, given a string of random array: 23,145. And we want the array in ascending order, as follows:

  • The first pass bubble: We want to find the biggest and the last place, then it should be:
    23154 (2 and 3 compared to 3 is greater than 2, do not move)
    21,354 (3 compared with the 1, 3 large, the exchange position)
    21354 (3 compared to 5, 5 is larger than 3, not moving)
    21345 (5 and 4 comparison, 5-4 large, the exchange position)
    through the first again the process we can clearly see the biggest 5 has been moved to our last one, so we are going on the right before the bubble can be number four in the sort
  • The second pass bubbling
    12345
    process it is omitted, the first four numbers is performed again on the above operation, the first three are then down on the number, the first two numbers. . .
void maopaopaixu(int *a,int n)
{
	int t;
	for(int i=0;i<n-1;i++)
	{
		for(int j=0;j<n-i-1;j++)
		{
			if(a[j+1]<a[j])
			{
				t=a[j+1];
				a[j+1]=a[j];
				a[j]=t;
			}
		}
	}
}

Complexity of the algorithm is O (N²)

Selection Sort

I still love the way this law is called Da Lei, because the selection process is like sort of the sort was accused in the same play again one by one, until only the largest (small) number we want.
To ascending order, for example, the principle is to start where to find the smallest number, and the smallest number first, find the second smallest number, put down second place, and so on.
E.g. unordered set of the given number: 23154
First Sort: 13254 (find the smallest one, and the first bit swap 2)
Second Sort: 12354 (2 find a second small, the second bit swap 3)
Sort third: 12354 (3 third small, just the third, fixed)
fourth Sort: 123 45 (4 fourth small, and the fourth interchange)

void xuanzepaixu(int *a,int n)
{
	int s;
	for(int i=0;i<n;i++)
	{
		int t=i;
		for(int j=i+1;j<n;j++)
		{
			if(a[t]>a[j])
			{
				t=j;
			}
		}
		s=a[t];
		a[t]=a[i];
		a[i]=s;
	}
}

Complexity of the algorithm is O (N²)

Shell sort

Hill sorting the equivalent of an upgraded version of the insertion sort, insertion sort we know have a good performance in dealing with more orderly arrangement of columns, Hill sorting actually put an array divided into several groups, each group insertion sort, then gradually reduced packet until each group has only one element.
Specific principles venue Hill sorting , graphical look at this article on the line, he uses Java.
Each time the step size of each group is, generally default int (array.lenth / 2), and the other two each time.

void xierpaixu(int *a,int n)
{
	for(int s=n/2;s>=1;s=s/2)
	{
		for(int i=s;i<n;i++)
		{
			int j=i;
			int x=a[j];
			while(j-s>=0&&x<a[j-s])
			{
				a[j]=a[j-s];
				j=j-s;
			}
			a[j]=x;
		}
	}
}
Published 17 original articles · won praise 18 · views 4005

Guess you like

Origin blog.csdn.net/weixin_45939019/article/details/104107756