c ++ c language sorting methods / three methods. Bubble sort, insertion sort, sort order.

Sort: Sort the so-called need is to have become the order of the data together.

1. bubble sort:

The basic idea of ​​the bubble sort: it compares two adjacent elements, if they are in the wrong order, and let them swap over. For example, a number on a disorderly column: 5,8,6,3,9,2,1,7, from small to large row of a sequence, in accordance with the thought bubble sort, we put adjacent elements pairwise comparison, according to position size exchange element. Process is as follows:
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

#include<bits/stdc++.h>
using namespace std;
const int maxn=100;
int a[maxn],x;
void bubble_sort(int n){//n为数组的长度
     for(int i=0;i<n;i++){
     	for(int j=0;j<n-i-1;j++){//这个n-i-1是因为在第一轮冒泡循环后,最大的已经排到了最后。 
     		if(a[j]>a[j+1]){//从大到小排序。 
     			int temp=a[j];//定义一个中间变量来使两个元素互换 
     			a[j]=a[j+1];
     			a[j+1]=temp;
			 }
		 }
	 }	
} 
int main(){
	cin>>x;//输入要几个数排序。 
	for(int i=0;i<x;i++){
		cin>>a[i];
	}
	bubble_sort(x);
	for(int i=0;i<x;i++){
		cout<<a[i]<<" "; 
	}
	return 0;
}

The results are as follows:
Here Insert Picture Description

Difficulty understanding:

J where <ni-1; Why?
Because there is a bubble sort features, this program is sorted in descending order, so after the first round of sorting, the maximum number will float to the far right; after the second round of sorting, the second largest number will float to the penultimate two positions; after the third round of sorting, the third largest number will float up to the last third position ......
in other words, how much sorting round, there are that many numbers have been ordered by the requirements lined up, they do not need to Compare.
Write j <n-1 can be, but the program do a lot of wasted effort in the implementation.

2. Insert Sort:

Insert into the array of ideas and methods to arrange the hand of playing cards similar to, for example, we are now holding a single card, and then the cards from left to right, descending sort, this time, we need to draw a card out, are inserted into previously arranged place good hands, and repeat this operation until the last card is inserted, it lined up.
For example: 2,5,1,3,6, from small to large sort
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

#include<bits/stdc++.h>
using namespace std;
const int maxn=100;
int a[maxn],x;
void insert_sort(int n){
	int j,temp;
	for(int i=1;i<n;i++){
		temp=a[i];
		j=i-1;
		while(j>=0&&a[j]>temp){//向前一个一个判断有没有比它大的数 
			a[j+1]=a[j];
			j--;
		}
		a[j+1]=temp;//找到它应该在的位置交换 
	}
}
int main(){
	cin>>x;//x个数进行排序
	for(int i=0;i<x;i++){
		cin>>a[i];
	}
	insert_sort(x);
	for(int i=0;i<x;i++){
		cout<<a[i]<<" ";
	}
	return 0;
} 

Here Insert Picture Description

3.sort Sort:

an array can sort in ascending order of functions.

#include<bits/stdc++.h>
using namespace std;
int main(){
	int x;
	cin>>x;
	int a[x];
	for(int i=0;i<x;i++) cin>>a[i];
	sort(a,a+x);
	for(int i=0;i<x;i++) cout<<a[i]<<" ";
	return 0;
} 

sort ordering simple and straightforward.
Here Insert Picture Description
Take in bloom.

Released eight original articles · won praise 8 · views 201

Guess you like

Origin blog.csdn.net/xzy15703841578/article/details/105371733