夜深人静写算法——线性时间选择(分治,数组第n小的数)

线性时间选择:求数组中第n小的值

一:

   解决的方法是基于快速排序解决的,当快速排序进行一次排序的时候,在参考点左侧的都是比参考值小的,右侧都是比参考点大的。

  (1)参考点的下标等于 n-1,说明参考点就是第n小的值。

  (2)参考点的下标大于n-1 , 说明所要求得第n小的值在参考值左侧的数组里,只需要对左侧数组进行快速排序。

  (3)参考点的下标小于n-1,说明所要求的第n小的值在参考值右侧的数组里,只需要对右侧数组进行快速排序。

二:

程序如下:

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <time.h>
#include <stdlib.h>
#define LENGTH 10
using namespace std;
template <class Type>
void swap(Type *a,Type *b){
	Type c;
	c = *a;
	*a = *b;
	*b = c;
}
template <class Type>
Type QuickSort(Type a[],int l,int r,int n){ //排序从l到r的数组
	if(l < r){  // 如果l >= r 不进行操作
		int t = l;     
		int stl = l;  //stl为左侧的指针
		int str = r+1; //str为右侧指针
		while(true){
			while(a[++stl] <= a[t] && stl <= r); //stl扫描到大于 参考值 的值后停止扫描
			while(a[--str]> a[t] && str >= 0);  //str扫描到小于 参考值 的值后停止扫描
			if(str < stl)break;  //如果当前str小于stl 说明扫描完一遍,最外层while循环停止
			swap(&a[stl],&a[str]);  //交换两个指针的值
		}
		
		swap(&a[l],&a[str]);  //交换参考值和str指针指向的值
		
		if(str - l == n - 1)   //如果参考点下标等于n-1,返回参考值
			return a[str]; 
		else if(str - l  < n - 1)  //参考点下标小于n-1,在右侧数组进行寻找
			QuickSort(a,str+1,r,n-1-(str - l) );
		else
			QuickSort(a,l,str-1,n);  //对左侧数组进行寻找
		 
	}
	
}
 
int main(){
	 int n;
	 srand(time(NULL));n= rand()%LENGTH + 1; 

	 int a[LENGTH] = { 2,1,3,4,7,5,6,9,8,0};
	 cout<<"第"<<n<<"小的数为:";
	 cout<<QuickSort(a,0,LENGTH-1,n);
}

猜你喜欢

转载自blog.csdn.net/curtern/article/details/83314352
今日推荐