当前位置:网站首页>快速排序、聚簇索引、尋找數據中第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;
}
}
边栏推荐
- Mysql表数据比较大情况下怎么修改添加字段
- IDEA 远程调试 Remote JVM Debug
- High paid programmers & interview questions series 63: talk about the differences between sleep (), yield (), join (), and wait ()
- Seven common errors of SSL certificate and their solutions
- FAQs for code signature and driver signature
- 福利抽奖 | 开源企业级监控Zabbix6.0都有哪些亮点
- Unity3d ugui force refresh of layout components
- IBM WebSphere channel connectivity setup and testing
- shell统计某个字符串最后一次出现的位置之前的所有字符串
- 最小栈详解
猜你喜欢

Sitelock nine FAQs

How to prevent phishing emails? S/mime mail certificate

prompt learning 一个空格引发的血案

uniapp 地址转换经纬度

Mysql提取表字段中的字符串

High paid programmers & interview questions series 63: talk about the differences between sleep (), yield (), join (), and wait ()

在php中字符串的概念是什么

How to use redis to realize the like function

中断操作:AbortController学习笔记

微信小程序页面跳转以及参数传递
随机推荐
Distributed file system fastdfs
Global and Chinese market of relay lens 2022-2028: Research Report on technology, participants, trends, market size and share
Heavy attack -- ue5's open source digital twin solution
How does native JS generate Jiugong lattice
How to set password complexity and timeout exit function in Oracle
Mysql表数据比较大情况下怎么修改添加字段
What is an X.509 certificate? 10. 509 certificate working principle and application?
Software testing skills, JMeter stress testing tutorial, transaction controller of logic controller (25)
Raki's notes on reading paper: neighborhood matching network for entity alignment
Unity timeline data binding
Global and Chinese markets for wireless security in LTE networks 2022-2028: Research Report on technology, participants, trends, market size and share
The Oracle main program is deleted, but the data is on another hard disk. Can I import the data again?
C console format code
Welfare lottery | what are the highlights of open source enterprise monitoring zabbix6.0
Raki's notes on reading paper: discontinuous named entity recognition as maximum clique discovery
CMake教程系列-01-最小配置示例
2. successfully solved bug:exception when publishing [Failed to connect and initialize SSH connection...
Cross domain, CORS, jsonp
RAII内存管理
Cmake tutorial series -05- options and variables