Achieve common sorting algorithm to sort of exchange ------

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/DX_Jone/article/details/99870523

Exchange Sort:

Sorting of exchange:

Bubble Sort
Quick Sort
1. bubble sort:

Thought: Start comparing two elements from the end of the element in the array, and the position change according to the size until the maximum or minimum of the array elements of the start position is discharged;
Here Insert Picture Description
code to achieve the following:

void Swap(int *left,int* right){
	in tmp=*left;
	*left=*right;
	*right=tmp;
}

void Bubblesort(int arr[],int size){
	for(int bound=0;bound<size;++bound){
		for(int cur=0;cur<size-1-bound;cur++){
			if(arr[cur]>arr[cur+1]){
				Swap(&arr[cur],&arr[cur+1});
			}
		}
	}
}

int main(){
	int array[]={9,5,2,4}
	int size=sizeof(arr)/sizeof(arr[0]);
    Bubblesort(array,size);
	return 0;
}

2. Quick Sort:

Ideas: three step:

Step 1: Determine a reference value;

Find ways to reference values:
1. Select the edge;
2. randomly;
3. Take France Number three: the leftmost section, the rightmost section, is an intermediate value of a selected number;

(The second and third methods require switching to the edge of the value)

Step two: do segmentation: traverse the entire array range, compare the size relationship of each number and the reference value, the magnifying left it smaller than the reference value after traversing the end, put it on the right big (to the left than the reference value than the reference value small, on the right is larger than the reference value)

About three methods to do specific segmentation:
1.Hover method (method pointer around / about the standard method)

//对数组中的[low,hight]的区间进行分组,基准值是array[low]
int Partition(int array[],int low,int hight){
	int piove=array[low];
	int begin=low;
	int end=hight;
	//[begin,end]的区间是没被比较过的数;
	while(begin<end){
		while(begin<end && array[begin]<=piove){
			begin++;
		}
		while(begin<end && array[end]>=piove){
			end--;
		}
		//array[begin]比基准值大了,交换begin和end下标处的数据;
		Swap(array,begin,end);
	}
	  //low基准值;
      //[low+1,begin] 比基准值小;
      //[begin+1,high] 比基准值大;
      //把基准值和比他小的最后一个数进行交换;
      Swap(array,low,begin);
      return begin;
}
			

2. trenching method :( exchanged through the backfilled pit)

int Partition(int array[], int low, int high){
      int pivot = array[low]; //begin 是坑的下标;
      int begin = low;      //从基准值的位置开始,而不是low+1;
      int end = high;
      //(begin,end)的区间是没有被比较过的数据;
      while (begin < end){
           //如果基准值在左边,需要从右边开始走;
           while (begin<end && array[begin] <= pivot){
                 begin++;
           }
            //array[begin]比基准值大了,坑的下标是end;
            //array[end] = array[begin];
            //begin变为坑的下标;
           
           while (begin<end && array[end] >= pivot){
                 end--;
           }
           //array[end]比基准值小了,坑的下标就是begin;
           array[begin] = array[end];
           //end是坑的下标;
      }
      //low基准值;
      //[low+1,begin] 比基准值小;
      //[begin+1,high] 比基准值大;
      //最后把基准值填到最后一个坑;
      array[begin] = pivot;
      return begin;
}

3. Finger front:

int Partition(int array[], int low, int high){
      int d = low + 1;
      int i = low + 1;
      int pivot = array[low];
      while (i <= high){
           if (array[i] < pivot){
                 Swap(array,d,i);
                 d++;
           }
           i++;
      }
      Swap(array, d - 1, low);
      return d - 1;
}


The third step: using a divide and conquer algorithm; In the same manner, respectively, between two similarly treated cells, the number of sub-length between out until cell <= 1, can be stopped;

Quick sort algorithm implementation:

1. Find by recursive method:
int Partition(int array[],int low,int hight){
	int piove=array[low];
	int begin=low;
	int end=hight;
	while(begin<end){
		while(begin<end && array[begin]<=piove){
			begin++;
		}
		while(begin<end && array[end]>=poive){
			end--;
		}
		array[begin]=array[end];
	}
	array[begin]=piove;
	return begin;
}

void _QuickSort(int array[], int low, int high){
      if (low == high){     //区间内只剩一个数,有序,所以不需要排序;
           return;
      }
      if (low > high){
           return;             //区间内没有数,所以不需要排序;
      }
      
      //1.找基准值,选最左边,基准值得下标是low;
      //2.遍历整个区间,把小的放左边,大的放右边,返回基准值所在下标;
      int pivotIdx = Partition(array, low, high);
      
      //3.区间被分为三部分:
      //[low,pivotIdx-1]  小
      //[pivotIdx,pivotIdx]  有序
      //[pivotIdx+1,high]  大
      
      //4.分治算法,分别调用两个小区间;
      _QuickSort(array,low, pivotIdx - 1);
      _QuickSort(array,pivotIdx + 1, high);
      
      //直到区间长度为0,或区间长度为1,表示区间的数已经有序;
}

void QuickSort(int array[], int size){  
      _QuickSort(array, 0, size - 1);
}
2. Non-recursive quicksort:
#include <stack>
void QuickSortNoR(int array[], int size){
      std::stack <int> stack;
      stack.push(0);            //low
      stack.push(size - 1);     //high
      while (stack.empty()){
           int high = stack.top();
           stack.pop();
           int low = stack.pop();
           stack.pop();
           
           if (low >= high){
                 continue;
           }
           
           int pivotIdx = Partition(array, low, high);
           [low,pivotIdx-1]
           stack.push(low);
           stack.push(pivotIdx - 1);
           [pivotIdx+1,high]
           stack.push(pivotIdx + 1);
           stack.push(high);
      }
}

Guess you like

Origin blog.csdn.net/DX_Jone/article/details/99870523
Recommended