当前位置:网站首页>快速排序、聚簇索引、尋找數據中第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;
}
}
边栏推荐
- CMake教程系列-04-编译相关函数
- Detailed explanation of minimum stack
- Three solutions to forced hibernation of corporate computers
- The rigorous judgment of ID number is accurate to the last place in the team
- [untitled]
- Precautions for purchasing wildcard SSL certificate
- GTK interface programming (I): Environment Construction
- 原生JS怎么生成九宫格
- LeetCode 3. 无重复字符的最长子串
- Unity TimeLine 数据绑定
猜你喜欢

Interrupt operation: abortcontroller learning notes

最小栈详解

How to prevent duplicate submission under concurrent requests

2. successfully solved bug:exception when publishing [Failed to connect and initialize SSH connection...

自定义JvxeTable的按钮及备注下$set的用法

What is a self signed certificate? Advantages and disadvantages of self signed SSL certificates?

如何在 JupyterLab 中把 ipykernel 切换到不同的 conda 虚拟环境?

Sitelock nine FAQs

HTA introductory basic tutorial | GUI interface of vbs script HTA concise tutorial, with complete course and interface beautification

JMeter obtains cookies across thread groups or JMeter thread groups share cookies
随机推荐
如何在 JupyterLab 中把 ipykernel 切换到不同的 conda 虚拟环境?
oracle怎么设置密码复杂度及超时退出的功能
uniapp 地址转换经纬度
Network neuroscience -- a review of network Neuroscience
[dry goods sharing] the latest WHQL logo certification application process
002 color classification
What is certificate transparency CT? How to query CT logs certificate logs?
Threejs mirror case reflector create mirror + house construction + small ball movement
NLP text summary: data set introduction and preprocessing [New York Times annotated corpus]
IDEA 远程调试 Remote JVM Debug
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.
Heavy attack -- ue5's open source digital twin solution
c#控制台格式化代码
Pytoch learning (II)
O & M (21) make winpe startup USB flash disk
模板参数包和函数参数包
Seven common errors of SSL certificate and their solutions
Five cheapest wildcard SSL certificate brands
三层交换机和二层交换机区别是什么
The Oracle main program is deleted, but the data is on another hard disk. Can I import the data again?