当前位置:网站首页>C #: quick sort. If there is the same number, it will be ignored, and then continue the previous search direction to find the next number that meets the requirements for replacement
C #: quick sort. If there is the same number, it will be ignored, and then continue the previous search direction to find the next number that meets the requirements for replacement
2022-07-23 12:52:00 【Sixi Liyu】
summary
Number of pits to be filled + Divide and conquer method
Summarize the number of pit excavation and filling
- i =L; j = R; The first pit will be excavated a[i], For example, the first benchmark number is 0 Indexed
- j– Find a smaller number from back to front , After finding it, dig out this number and fill the previous pit a[i] in .
- i++ Find a larger number from front to back , After finding it, dig out this number and fill it in the previous pit a[j] in .
- And repeat 2,3 Two steps , until i==j, Fill the reference number in a[i] in , When meeting, update the left half index according to the benchmark , And right half index
- quick_sort(s, l, i - 1);quick_sort(s, i + 1, r); Recursively call , Until every recursive l And r equal , End recursion
void quick_sort(int s[], int l, int r)
{
if (l < r){
int i = l, j = r, x = s[l];
while (i < j){
while(i < j && s[j] >= x) // Find the first less than... From right to left x Number of numbers
--j;
if(i < j)
s[i++] = s[j];
while(i < j && s[i] < x) // Find the first greater than or equal to... From left to right x Number of numbers
++i;
if(i < j)
s[j--] = s[i];
}
s[i] = x;
quick_sort(s, l, i - 1); // Recursively call
quick_sort(s, i + 1, r);
}
}
What is the process of quick sorting if there are the same numbers
The same number will be ignored , Then continue the previous search direction to find the next number that meets the requirements for replacement
test
int[] array = new int[8] { 5 ,2, 2, 1, 7 ,3, 4, 4 };
Time complexity
O (nlogn)
O(log n) analysis
Another example O(log n), When the data increases n Times , Time consuming log n times ( there log In order to 2 At the bottom of the , such as , When the data increases 256 Times , Time consuming only increases 8 times , It's a lower time complexity than linearity ). A binary search is O(log n) The algorithm of , Every time you look for it, rule out half of the possibilities ,256 Just look for 8 You can find the target in three times .
Easy to understand examples
This is like having a hundred keys , You suddenly feel , Is it too slow for me to look from the beginning , I looked in the middle , For example, I want to find 23 Room key No , I cut it in the middle , find 50 The position of the number , then 23 stay 150 Inside , I'll cut it from the middle into 25, then 23 stay 125 Between , I'll cut it into 12.5, then 23 stay 12.5~25 Between , Look down in turn , Until you find the key . The complexity of this method of finding keys is O(log^n)
O(n log n) analysis
O(n log n) Empathy , Namely n multiply log n, When the data increases 256 Times , Time consuming 256*8=2048 times . This complexity is higher than linear, lower than square . Merge sort is O(n log n) Time complexity of .
Source code
https://github.com/luoyikun/UnityForTest
SortScene scene
边栏推荐
- [Reading Notes "Phoenix architecture" - a large-scale distributed system with reliable architecture. Zhou Zhiming] (I)
- 第五周作业
- WebSocket 协议讲解
- @RequiredArgsConstructor注解使用
- DNS域名解析服务
- 0动态规划 LeetCode918. 环形子数组的最大和
- 学习日记——(路由与交换技术)网络地址转换 NAT技术
- 即时通讯WebSocket
- Hcip ---- relevant knowledge points of GRE protocol, mGRE environment and OSPF Protocol
- 详解TCP分段与IP分片
猜你喜欢

Unity3d:UGUI,UI与特效粒子层级,2018.2以上版本BakeMesh,粒子在两个Image之间且在ScrollView

GameFramework:资源热更代码分析,检查版本信息,下载版本文件,校验版本文件,得到更新文件数量,下载文件,TaskPool

Super easy to use packet capturing tool tcpdump

C#:TopK:1万个数取前最大的100,堆排序

C #: TOPK: take the largest 100 before 10000 numbers, and sort the heap

C custom queue set

HCIP---BGP相关配置

C#:快速排序,有相同的数字会忽略,然后继续先前的寻找方向去找下一个满足要求的数字进行替换

Unity3D+moba+技能指示器(二)

浅做一下思科实验吧!
随机推荐
学习日记——(路由与交换技术)动态路由(rip协议)和静态路由
HCIP-HCIA知识回顾(二)
Take go language as an example to explain [performance analysis] by analogizing detective reasoning
C (CSharp) wechat official account development - basic configuration
Reading Phoenix Architecture - History and knowledge of RPC
Unity3d: ugui source, Rebuild Optimization
Article on the basic technology needed to build hybrid app
0双指针 LeetCode844. 比较含退格的字符串
0回溯/动态规划中等 LeetCode526. 优美的排列
C custom queue set
Overview of OpenSSL self visa process
Unity3D+moba+技能指示器(二)
htpasswd作用
简洁描述raft与paxos在设计上的共同点和不同点
Htpasswd action
Hcip--- BGP related configuration (Federal chapter)
整数乘以整数溢出了
Homework of the fifth week
《wireshark网络分析就是这么简单》知识点与技巧
关于搭建Hybrid App所需要的基础技术一文