Sort Topics

Sorting, typing code is the only way to go. The same string of numbers and different codes have the same result, and the process is often the key to solving the problem. Time complexity and space complexity are all issues to be considered.
Consolidate, make a summary.





## 一、直接插入排序

2 ,1 3 5 4 
1 2 ,3 5 4
1 2 3 ,5 4
1 2 3 4 ,5

```cpp
/*
*从小到大之直接插入排序 
*每次移动一个元素  
*依次进行比对 插入到应在的位置 
*/

#include<iostream>
using namespace std;

int main()
{
	int a[]={2,1,3,5,4};
	int i,j,temp;
	//从第二个数开始比较 
	for(i=1;i<sizeof(a)/sizeof(a[0]);i++)
	 {
	 	temp=a[i];   //记录当前需要插入元素的值 
	 	for(j=i-1;j>=0;j--)  //从需要插入位的前一个位置 进行比较   
	 	{
	 		if(temp<a[j])    //说明需要将元素移动到前面   
	 		{
	 			a[j+1]=a[j];  // 移动元素  
			}
	 		else  //这个元素比前面的元素值要大 ,不需要移动 
			{
				break; 	
			} 
		 }
		a[j+1]=temp;   //由于之前  j-- 了  所以在j+1位置插入元素 
	}
	for(i=0;i<sizeof(a)/sizeof(a[0]);i++) 
	{
		cout<<a[i]<<" ";
	}
	cout<<endl;
	return 0;
}

Pointer function form:

/*
*从小到大之直接插入排序 
*每次移动一个元素  
*依次进行比对 插入到应在的位置 
*/

#include<iostream>
void sort(int *ptr,int n);
using namespace std;

int main()
{
    
    
	int a[]={
    
    2,1,3,5,4};
	int n=sizeof(a)/sizeof(a[0]);
	sort(a,n);
	for(int i=0;i<n;i++) 
	{
    
    
		cout<<a[i]<<" ";
	}
	cout<<endl;
	return 0;
}

void sort(int *ptr,int n)
{
    
    
	int i,j,temp;
	//从第二个数开始比较 
	for(i=1;i<n;i++)
	 {
    
    
	 	temp=*(ptr+i);   //记录当前需要插入元素的值 
	 	for(j=i-1;j>=0;j--)  //从需要插入位的前一个位置 进行比较   
	 	{
    
    
	 		if(temp<*(ptr+j))    //说明需要将元素移动到前面   
	 		{
    
    
	 		 *(ptr+j+1)=*(ptr+j); // 移动元素  
			}
	 		else  //这个元素比前面的元素值要大 ,不需要移动 
			{
    
    
				break; 	
			} 
		 }
		*(ptr+j+1)=temp;   //插入元素 
	}

2. Bubble sort

A gurgle goes from the bottom to the surface of the water from small to large (the essence of bubbling)

/*
*从小到大之冒泡排序
*咕噜咕噜 
*/

#include<iostream>
using namespace std;
int main()
{
    
    
	int a[]={
    
    2,1,3,5,4};
	int n=sizeof(a)/sizeof(a[0]);
	//冒泡 
	for(int i=1;i<=n;i++)
	{
    
    
		for(int j=0;j<=n-i-1;j++)
		{
    
    
			if(a[j]>a[j+1])
			{
    
    
				int temp=a[j];
				a[j]=a[j+1];
				a[j+1]=temp;
			}
		}
	}
	for(int i=0;i<n;i++) 
	{
    
    
		cout<<a[i]<<" ";
	}
	cout<<endl;
	return 0;
}

3. Selection sort

/*
*从小到大之选择排序 
*/
#include<iostream>
using namespace std;

int main()
{
    
    
	int a[]={
    
    2,1,3,5,4};
	int n=sizeof(a)/sizeof(a[0]);
	int i,j,k,temp;

	for(i=0;i<n;i++)
{
    
    
	k=i;
	for(j=i+1;j<n;j++)
	{
    
    
		if(a[j]<a[k])
		{
    
    
			k=j;
		}
	}
	if(k!=i)
	{
    
    
		temp=a[i];
		a[i]=a[k];
		a[k]=temp;
	}
}
	for(int i=0;i<n;i++) 
	{
    
    
		cout<<a[i]<<" ";
	}
	cout<<endl;
	return 0;
}

4. Hill sort

规定步长 ,把步子上的数进行排序,从而达到整体上的排序
3 5  2  1  4   

The first pass: 3 2 4 Sort 2 3 4
5 1 Sort 1 5
The first pass is executed 2 1 3 5 4

/*
*从小到大排序之希尔排序 
*/
#include<iostream>
using namespace std;

int main()
{
    
    
	int a[]={
    
    3,5,2,1,4};
	int n=sizeof(a)/sizeof(a[0]);
	int h,i,j,temp;
	for(h=n/2;h>0;h/=2)
	{
    
    
		for(i=h;i<n;i++)
	{
    
    
		temp=a[i];
		{
    
    
			for(j=i-h;j>=0;j-=h)
			{
    
    
				if(temp<a[j])
				{
    
    
					a[j+h]=a[j];
				}
				else
				{
    
    
					break;
				}
			}
			a[j+h]=temp;
		}
	}
}
	for(i=0;i<n;i++)
	{
    
    
		cout<<a[i]<<" ";
	}
	cout<<endl;
	return 0;
 } 

Five, merge sort






在这里插入代码片

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324135020&siteId=291194637