当前位置:网站首页>快速排序、聚簇索引、尋找數據中第k大的值
快速排序、聚簇索引、尋找數據中第k大的值
2022-06-30 02:57:00 【PPDY小芽】
快速排序代碼
public class Quick {
public static void main(String[] args) {
int [] nums = {
4,2,4,4,5,1,10,8,6,5,7,100};
System.out.println(Arrays.toString(nums));
QuickSort(nums,0,nums.length-1);
System.out.println(Arrays.toString(nums));
}
private static void QuickSort(int[] nums, int beigin, int end) {
if(beigin > end){
return ;
}
int i = beigin;
int j = end;
int v = nums[beigin];
while(i < j ){
while(i < j && nums[j]> v){
j--;
}
while(i < j && nums[i] <= v){
i++;
}
int temp = nums[j];
nums[j] = nums[i];
nums[i] = temp;
}
nums[beigin] = nums[i];
nums[i] = v;
QuickSort(nums,beigin,i-1);
QuickSort(nums,i+1,end);
}
}
聚簇索引和非聚簇索引的區別
聚簇索引:索引項的排序方式和錶中數據記錄排序方式一致的索引(字典中的拼音查找漢字)innodb
非聚集索引:索引順序與物理存儲順序不同(字典中的偏旁部首查找漢字)myisam
聚簇索引的葉子節點就是數據節點,索引和數據在一起。
稠密索引和稀疏索引
稠密索引:每個索引鍵值對都對應一個索引項;
稀疏索引:只為某些搜索碼值建立索引記錄;
稀疏索引的優點是:所占用的空間更小,且插入和删除時維護開銷也小。
尋找數據中第k大的值(基於快排)
public class Quic4 {
public int findKthLargest(int[] nums, int k) {
return getKth(nums, 0, nums.length - 1, nums.length - k);
}
public int getKth(int[] nums, int start, int end, int k) {
int pivot = nums[start];
int left = start;
int right = end;
while (left < right) {
while (left < right && nums[right] > pivot) {
right--;
}
while (left < right && nums[left] <= pivot) {
left++;
}
swap(nums, left, right);
}
swap(nums, left, start);
if (k == left) {
return pivot;
} else if (k < left) {
return getKth(nums, start, left - 1, k);
} else {
return getKth(nums, left + 1, end, k);
}
}
public void swap(int[] nums, int n1, int n2) {
int tmp = nums[n1];
nums[n1] = nums[n2];
nums[n2] = tmp;
}
}
边栏推荐
- NLP text summary: data set introduction and preprocessing [New York Times annotated corpus]
- What is an X.509 certificate? 10. 509 certificate working principle and application?
- Which is a good foreign exchange trading platform? Is it safe to have regulated funds?
- shell统计某个字符串最后一次出现的位置之前的所有字符串
- Recursion frog jumping steps problem
- Network neuroscience -- a review of network Neuroscience
- IDEA 远程调试 Remote JVM Debug
- C # basic learning (XIII) | breakpoint debugging
- Some technology sharing
- 2022 tool fitter (Advanced) and tool fitter (Advanced) certificate examination
猜你喜欢
![[dry goods sharing] the latest WHQL logo certification application process](/img/c3/37277572c70b0af944e594f0965a6c.png)
[dry goods sharing] the latest WHQL logo certification application process

【直播笔记0629】 并发编程二:锁

What files does a CA digital certificate contain? How to view SSL certificate information?

How to use vant to realize data paging and drop-down loading

Implementation of Sanzi chess with C language

Summary of knowledge points about eigenvalues and eigenvectors of matrices in Chapter 5 of Linear Algebra (Jeff's self perception)

Call collections Sort() method, compare two person objects (by age ratio first, and by name ratio for the same age), and pass lambda expression as a parameter.

Customize the buttons of jvxetable and the usage of $set under notes

可视化HTA窗体设计器-HtaMaker 界面介绍及使用方法,下载 | HTA VBS可视化脚本编写

O & M (20) make and start USB flash disk and install win10
随机推荐
Software testing skills, JMeter stress testing tutorial, transaction controller of logic controller (25)
福利抽奖 | 开源企业级监控Zabbix6.0都有哪些亮点
Visual HTA form designer htamaker interface introduction and usage, Download | HTA VBS visual script writing
What files does a CA digital certificate contain? How to view SSL certificate information?
Distributed file system fastdfs
CMake教程系列-03-依赖管理
What is the concept of string in PHP
CMake教程系列-01-最小配置示例
prompt learning 一个空格引发的血案
Série de tutoriels cmake - 02 - génération de binaires à l'aide du Code cmake
What kind of foreign exchange trading platform is regulated and safe?
CMake教程系列-04-编译相关函数
Redis+AOP怎么自定义注解实现限流
How does native JS generate Jiugong lattice
CMake教程系列-05-选项及变量
【直播笔记0629】 并发编程二:锁
FAQs for code signature and driver signature
Note the use of export/import and class inheritance in ES6
CMake教程系列-02-使用cmake代码生成二进制
The rigorous judgment of ID number is accurate to the last place in the team