分治算法四(查找第K小元素)
来源:互联网 发布:无损分区软件 编辑:程序博客网 时间:2024/06/11 18:30
1.问题描述
对于一个非有序的数组A[start..end],求数组中第k小的元素
2.解决思路
最直观的的方式,先对数组排序,复杂度为o(nlgn),但是这样复杂度太大,可以利用快速排序的思路,解决这个问题,并且复杂度为o(n)。
对于一个非有序的数组A[start..end],求数组中第k小的元素
2.解决思路
最直观的的方式,先对数组排序,复杂度为o(nlgn),但是这样复杂度太大,可以利用快速排序的思路,解决这个问题,并且复杂度为o(n)。
关键在于划分只一个部分,我们知道快速排序选择一个pivot对数组进行划分,左边小于pivot,右边大于等于pivot,所以我们计算左边小于pivot(加上pivot)的个数count总共有多少,如果等于k,正是我们所要的,如果大于k,说明第k小的数在左边,那就在左边进行我们的递归;否则,在右边,那么说明右边的第k-count小的数就是我们所要的,在右边进行我们的递归。
/* file: rand_select*//* 1、the same as the quicksort*//* 2、use a random algorithm to choose a pivot, then split the Array into two subarrays. the left elements are smaller than pivot, the right elements are larger than pivot*//* 3、compare i with pivot'index to choose the correct side of the Array.*//* 4、conquer to find the ith smallest element*/#include<stdio.h>#include <stdlib.h>/*===================================== swap:swap two numbers a and b=======================================*/inline void swap(int* a,int* b){int tmp;tmp = *a;*a = *b;*b = tmp;}/*===================================== Partition:Partition the Array from startto end into two subarrays.Return the pivot of the elementArray[start]=======================================*/int Partition(int* Array, int start, int end){//choose Array[start] as the pivot element //divide the array into two subarrays.//left of the Array's elements <= Array[start]//right of the array's elements > Array[start]int pivot = start,j;for(j = start + 1; j <= end; j++)if(Array[j] <= Array[start]){pivot++;swap(&Array[pivot], &Array[j]);}swap(&Array[start], &Array[pivot]);return pivot;}/*===================================== RandomPartition:using random algorithm.partition the Array into two subarrays.Return the pivot of the element Array[start] which is already swaped=======================================*/int RandomPartition(int* Array, int start, int end){//generate the random index of the pivotint random_index = start + rand() % (end - start + 1);//swap the pivot with the random_index elementswap(&Array[start], &Array[random_index]);//now random_index element act as the start elementreturn Partition(Array,start,end);}/*===================================== RandSelect:find the ith smallest element=======================================*/int RandSelect(int* Array, int start, int end, int i){int pivot, k;//this is the end of the conquerif(start == end) return Array[start];//use RandomPartition function to split the Arraypivot = RandomPartition(Array, start, end);k = pivot - start + 1;//conquerif(i == k) return Array[pivot];if(i < k)return RandSelect(Array, start, pivot - 1, i);elsereturn RandSelect(Array, pivot + 1, end, i - k);}void main(){int n, i, k, selected;printf("Please input the length of Array<0:exit>:\n");while(scanf("%d",&n),n){//create an arrays of length nint* Array = new int[n];//get the input integersprintf("Please input %d integers:\n",n);for(i = 0; i < n; i++)scanf("%d",&Array[i]);//get the kprintf("Please input <k>:");scanf("%d",&k);//use RandSelect algorithomselected = RandSelect(Array, 0, n-1, k);//print the kth smallest elementprintf("The %dth smallest element is %d:\n",k, selected);system("pause");system("cls");//delete the arraydelete[] Array;printf("Please input the length of Array<0:exit>:\n");}}
- 分治算法四(查找第K小元素)
- 查找第k小元素的函数. 分治算法
- 分治 第k小元素
- 【转】【DBSDFZOJ 1163】分治 第K小元素(分治)
- 选择问题(第k小元素)(分治法)
- 分治法 寻找第K小元素
- 算法之查找第k小的数和查找最小的k个元素
- 分治算法的利用例子(查找最大最小数,快速排序,查看第k小的数字)
- 分治算法 求第k小元素 O(n) < O(nlog2^n)
- 选择问题(查找第k小元素)
- 用快排来查找第k小元素
- 查找第K小的元素
- 查找第K小元素(C语言版)
- 算法讨论(四)--查找最小的k个元素
- 面试算法:lg(k)时间查找两个排序数组合并后第k小的元素
- BFPRT算法查找第k大元素
- 分治法实验-寻找第k小元素
- 分治法求第k小元素
- MFC中自定义消息
- C/C++文件输入输出操作——FILE*、fstream、windowsAPI
- Android中的URI
- linux动态连接
- 湘潭邀请赛总结
- 分治算法四(查找第K小元素)
- ?2 基本套接字
- jQuery 自定义插件
- 关于路由器设置的各种问题
- Win XP Virtual PC + WINDDK + DriverStudio + VC6.0 环境配置
- javaweb全站乱码解决
- 学会使用BeanUtils 来操作bean属性
- messages忽略包含特定字符串的信息
- 超想-3000TC综合实验/仿真系统Keil C实验指导书