剑指offer 之 partition函数运用

本文介绍了一种高效的方法来查找数组中的最小k个数,通过使用快速排序中的partition函数,实现时间复杂度为O(nlogn)。

1 找出数组最小的k个数## 标题 ##

@1;最简单也最直观的解法。对数组排序,顺序输出前k个数字。最快的是利用快排,时间复杂度为o(nlogn);

@2:利用快排中的partition ,该函数的思想为划分数组为两部分 左半部小于枢纽值,右半边大于枢纽值。利用该算法,可以得出本题的算法我们可以把最终的low值定位在 第k-1的位置。代码如下。

void mypartition(data[],int low,int high,int k)
{       
        int pivot=0;
       while(pivot!=k-1)
       {
           pivot=partition(data,low,high);
           if(pivot<k-1)
           pivot=partition(data,pivot+1,high);
           else
           pivot=partition(data,low,pivot-1);
       }

      for(int i=0;i<k;i++)
      printf("%d",data[i]);
      putchar("\n");  
}
### Partition Function Usage in Programming The `partition` function or method is commonly found within various programming contexts, especially when dealing with algorithms that require dividing elements into groups based on certain criteria. In C++, the Standard Template Library (STL) provides an implementation of this functionality through its algorithm component[^2]. #### Using std::partition in C++ In C++'s STL, `std::partition` rearranges elements so that all elements for which a predicate returns true precede those for which it returns false. However, unlike `stable_partition`, `partition` does not guarantee relative order preservation among elements. Here’s how one can use `std::partition`: ```cpp #include <algorithm> #include <vector> #include <iostream> int main() { std::vector<int> v = {10, 7, 8, 9, 1, 5}; auto is_even = [](int i){ return i % 2 == 0; }; // Apply partition using lambda expression as predicate auto new_end = std::partition(v.begin(), v.end(), is_even); // Print results after applying partition for(auto iter=v.begin(); iter!=new_end; ++iter){ std::cout << *iter << " "; } } ``` This code snippet demonstrates moving even numbers to the front while odd ones follow them without maintaining their original sequence. For GPU-based computations involving CUDA, similar concepts apply but involve different APIs like hipLaunchKernelGGL(). These are used specifically for launching kernels rather than manipulating collections directly[^1]. When optimizing performance-critical applications either offline or online, understanding such functions helps improve efficiency by enabling better management of data structures during processing stages[^3]. --related questions-- 1. How does stable_partition differ from partition? 2. Can you provide examples where partition might be more efficient compared to other methods? 3. What role do predicates play in determining element placement within containers post-partition operation? 4. Are there equivalent operations available across multiple languages besides C++? 5. How would one implement custom partition logic outside standard libraries?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值