The three entry-level sorting algorithm (compare, bubbling, fast row) speak in detail

Sorting 1. Comparative
  Comparative sort is most obvious white sorting method, starting from the first element, and sequentially comparing all subsequent elements, smaller than the first element, these two elements will be exchanged, and finally completed the first ratio a first element must be minimal, and then starting from the second element, the elements and all subsequent comparison, a ratio of finished, the second element is a second small, empathy, than n-1 End (n is the number of array elements) wheel, all become ordered array. Comparative sort is stable , that is, the value is encountered as the exchange does not occur is large, hold position, its time complexity is O (n ^ 2), the following code:

#include <iostream>
using namespace std;

void swap(int* a,int* b){//交换 
	int temp=*a;
	*a=*b;
	*b=temp;
}

void Sort(int a[],int len){
	for(int i=0;i<len-1;i++){
		for(int j=i+1;j<len;j++){
			if(a[i]>a[j]){
				swap(a[i],a[j]);
			}
		}
	}
}

int main(){
	int a[10]={8,6,9,7,3,0,2,5,1,4};
	cout << "排序前的数组:" << endl;
	for(int i=0;i<10;i++){
		cout << a[i] << " ";
		if(i==9) cout << endl;
	}
	Sort(a,10);
	cout << "排序后的数组:" << endl;
	for(int i=0;i<10;i++){
		cout << a[i] << " ";
		if(i==9) cout << endl;
	}
	return 0;
}

2. Bubble sort
  Bubble sort is a sorting algorithm is a classic, as the name implies, bubble sort, just take the water blisters, like most slowly rose from under the top. Bubble sort is adjacent elements are compared, so that the maximum value to the final surface to change slowly, so that the bubble sort have to be n-1 times, is stable , and its time complexity is O (N ^ 2) ,code show as below:

#include <iostream>
using namespace std;

void swap(int* a,int* b){//交换 
	int temp=*a;
	*a=*b;
	*b=temp;
}

void Sort(int a[],int len){
	for(int i=0;i<len-1;i++){
		for(int j=0;j<len-i-1;j++){
			if(a[j]>a[j+1]){
				swap(a[j],a[j+1]);
			}
		}
	}
}

int main(){
	int a[10]={8,6,9,7,3,0,2,5,1,4};
	cout << "排序前的数组:" << endl;
	for(int i=0;i<10;i++){
		cout << a[i] << " ";
		if(i==9) cout << endl;
	}
	Sort(a,10);
	cout << "排序后的数组:" << endl;
	for(int i=0;i<10;i++){
		cout << a[i] << " ";
		if(i==9) cout << endl;
	}
	return 0;
}

3. Quick Sort
  Quick Sort referred fast row, is the most common method of sorting, C ++ STL library which sort () method is fast row (optimized). Understand quick sort you must first understand the ideological divide and conquer, is about a large, complex problem into smaller problems easy to solve, when you put all the small problems to solve, the big problem will be solved. Coming back so fast discharge, fast discharge idea is: find a reference value to be sorted array, the array will be sorted into two sections, the first section of the whole is less than the value of the reference value, the whole is greater than the value of the second paragraph of this reference value . This operation is then repeated until the entire array of these have become orderly. That is "the whole orderly, partial disorder" . Wherein the reference speed related to the selected row worth efficiency, quick drain average complexity of O (n * logn) when the array is fully reverse, fast row degenerate into a complexity O (n-2 ^) . Select respect to the reference value (fast row optimization) not expand here, we generally choose the most left element interval as the reference value.
  So our problem into how the range segment? It is also quick drain core: cross-scan, the position of the switching element
For example: to be sorted array is: 8697302514
In this case, the reference value flag = 8, the leftmost subscript interval left = 0, the rightmost subscript right = 9; right-9 from the right end to start scanning (because we have chosen the reference value is the value of the leftmost element), 4 < 8, the left side should be representative of 4 to 8, we will exchange its position, the array becomes: 4 6 9 7 3 0 2 5 1 8, left = 0, right = 9 then starts from the left to sweep left = 0 ,, 4 <= 8, 4 should be representative of the left side 8, in the correct position, then the next scan, 6 <= 8, 6 should be representative of the left side 8, in the correct position, then the next scan, 9 => 8,9. 8 on the right should be , j exchange, the array becomes: 4 6 8 7 3 0 2 5 1 9, left = 2, right = 9, then repeat this operation (no exchange continues forward (after) scanned from another exchange on while start scanning) until the left> = right, it represents the array has been divided into many segments. The array is divided into many segments: 4617302589, divided into 46,173,025 to be sorted and the two sections 9, above operation is repeated until all the sections are sorted ( the position of each element are properly), that is the sort is complete. Note that: fast discharge is unstable . Based on the above ideas, we can write the following code:
  the code contains detailed notes as follows :()

#include <iostream>
using namespace std;

void swap(int *a,int *b){//交换
	int temp=*a;
	*a=*b;
	*b=temp;
}

int partition(int left,int right,int a[]){//使基准值左边全小于基准值,基准值右边全大于基准值 
	int flag=a[left];//基准值,一般选取区间最左边的元素 
	while(left<right){
		//先从后往前扫,如果比基准值小,将两个值交换 
		while(left<right && a[right]>=flag) right--;
		swap(&a[left],&a[right]);
		//然后从前往后扫,如果比基准值大,将两个值交换 
		while(left<right && a[left]<=flag) left++;
		swap(&a[left],&a[right]);
	}
	return left;//返回基准值的下标,以此为分界线,将区间划分成两个小区间 
}

void quickSort(int start,int end,int a[]){//递归调用(分治思想),直至所有的元素 
	if(start<end){
		int flag=partition(start,end,a);
		quickSort(start,flag-1,a);
		quickSort(flag+1,end,a);
	}
}

int main(){
	int a[10]={8,6,9,7,3,0,2,5,1,4};
	cout << "排序前的数组:" << endl;
	for(int i=0;i<10;i++){
		cout << a[i] << " ";
		if(i==9) cout << endl;
	}
	quickSort(0,9,a);
	cout << "排序后的数组:" << endl;
	for(int i=0;i<10;i++){
		cout << a[i] << " ";
		if(i==9) cout << endl;
	}
	return 0;
}
Published 10 original articles · won praise 7 · views 160

Guess you like

Origin blog.csdn.net/qq_44204959/article/details/104681475