当前位置:网站首页>Time complexity analysis of bubble sorting
Time complexity analysis of bubble sorting
2022-07-26 13:24:00 【weixin_ forty-three million seven hundred and sixty-six thousan】
Time complexity analysis of bubble sort

1. Unoptimized bubble sort
- Inner loop : Because the array subscript involves i+1, So the maximum is length-2, In addition, every time the external circulation is completed , The elements to be processed in the array will be reduced by one , Therefore, the maximum value of internal circulation is :length -1- k
/** * Analysis of comparison times : * int[] There are elements n individual * i = 0 --> All in all n Pending elements A total comparison n-1 Time * i = 1 --> All in all n-1 Pending elements A total comparison n-2 Time * i = 2 --> All in all n-2 Pending elements A total comparison n-3 Time * ...... * i = n-3 --> All in all 3 Pending elements A total comparison 2 Time * i = n-2 --> All in all 2 Pending elements A total comparison 1 Time * * Add the above comparison times to get Total number of comparisons : * (n-1)+(n-2)+(n-3)+ ... +3+2+1 = (1+(n-1))*(n-1)/2 = n*(n-1)/2 = (n^2 -n)/2 */
public static void mppx(int[] oldArr){
if (oldArr.length > 1){
for (int k = 0; k < oldArr.length; k++) {
// Outer loop
for (int i = 0; i < oldArr.length -1-k; i++) {
// Inner loop
if (oldArr[i]>oldArr[i+1]){
int max = oldArr[i];
oldArr[i] = oldArr[i+1];
oldArr[i+1] = max;
}
}
}
}
}
2. Bubble sorting after optimization
- External circulation optimization :n Elements Actually, it only needs to be completed n-1 Great wheel
- If the first big round ( Outer loop ) Come down , If there is no exchange , Then the elements in the array are ordered , So we can optimize the algorithm
public static void mppx(int[] oldArr){
if (oldArr.length > 1){
int k,i,max,
flag=1,// be used for It is the judgment of ordered array flag: If there is no element exchange after the first round of outer loop , That proves that the array is ordered
//moveCount = 0,// Record the number of exchanges
len = oldArr.length ;
for ( k = 0; k < len-1; k++) {
// Outer loop : Loop through every element in the array ,n Elements Actually, it only needs to be completed n-1 round
for ( i = 0; i < len-1-k; i++) {
// Inner loop : Used to compare and exchange with other elements
if (oldArr[i] > oldArr[i+1]){
max = oldArr[i];
oldArr[i] = oldArr[i+1];
oldArr[i+1] = max;
flag = 0;
moveCount++;
}
}
// The first round comes down , If there is no exchange , The elements in the array are ordered
if (flag==1){
//System.out.println("k = " + k + ": The elements in an array are ordered ");
break;
}
}
//System.out.println("moveCount = " + moveCount);
}
}
3. Best case analysis ( The elements in the array are ordered )
- Bubble sorting algorithm is optimized ( pivotal flag)
| Array | Compare and move times |
|---|---|
| 123 | Compare 2 Time In exchange for 0 Time |
| 1234 | Compare 3 Time In exchange for 0 Time |
| 12345 | Compare 4 Time In exchange for 0 Time |
| 123456 | Compare 5 Time In exchange for 0 Time |
| … | … |
| n An ordered element | Compare n-1 Time In exchange for 0 Time |
- therefore Bubble sorted The optimal time complexity is : O(n)
4. Worst case analysis ( The elements in the array are completely in reverse order )
- Hypothetical array 2 The process of exchanging element values is an operation
| Original array + Moving process | Comparison and exchange times |
|---|---|
| 21: 12 | 2 Elements == Compare 1 Time In exchange for 1 Time |
| 321: 231->213->123 | 3 Elements == Compare (1+2) Time In exchange for (1+2) Time |
| 4321: 3421->3241->3214->2314->2134->1234 | 4 Elements == Compare (1+2+3) Time In exchange for (1+2+3) Time |
| 54321: 45321->43521->43251->43215->34215->32415->32145->23145->21345->12345 | 5 Elements == Compare (1+2+3+4) Time In exchange for (1+2+3+4) Time |
| 654321: | 6 Elements == Compare (1+2+3+4+5) Time In exchange for (1+2+3+4+5) Time |
| … | … |
| n Elements in reverse order | n Elements == Compare (1+2+3+4+5+…+ n-1) Time In exchange for (1+2+3+4+5+…+ n-1) Time |
| n Elements in reverse order | n Elements == Compare (n-1)*n/2 Time In exchange for (n-1)*n/2 Time |
- therefore Bubble sorted The worst time complexity is :(n-1)*n/2 + (n-1)*n/2 = n^2 -n Ignore the lower power and get O(n^2)
5. Stability of bubble sorting algorithm
- If two adjacent elements are equal , No swap operation will occur , So bubbling doesn't change the subscript of the same element , So bubble sort is a stable sort algorithm .
边栏推荐
- The parent component accesses the methods or parameters of the child component (the child component exposes the method defineexpose)
- Kubernetes apiserver current limiting strategy
- 牛客刷SQL---2
- A college archives management system based on asp.net
- 为什么要做“密评”?
- Algorithm -- continuous sequence (kotlin)
- JS object assignment problem
- MySQL data directory (1) -- database structure (24)
- Niuke brush sql---2
- Kubelet CRI 容器运行时
猜你喜欢

Brief introduction of reflection mechanism

A college archives management system based on asp.net

基于BERT的情感分析模型

Learn about Pinia state getters actions plugins

Kubelet CRI container runtime

学习pinia 介绍-State-Getters-Actions-Plugins

1312_适用7z命令进行压缩与解压

Photoshop (cc2020) unfinished

详解关系抽取模型 CasRel

Solve the problem that the remote host cannot connect to the MySQL database
随机推荐
LeetCode 263.丑数
This article explains the FS file module and path module in nodejs in detail
Can MySQL customize variable parameter storage functions?
LeetCode 1523. 在区间范围内统计奇数数目
vector的一些实用操作
B+树索引使用(9)分组、回表、覆盖索引(二十一)
JSON format execution plan (6) - MySQL execution plan (52)
[typescript] typescript common types (Part 1)
银行业客户体验管理现状与优化策略分析
B+ tree index use (8) sorting use and precautions (20)
如何构建以客户为中心的产品蓝图:来自首席技术官的建议
Sword finger offer (21): push in and pop-up sequence of stack
Basic sentence structure of English ----- origin
JS object assignment problem
Mysql数据目录(1)---数据库结构(二十四)
Exploration on cache design optimization of community like business
异步线程池在开发中的使用
《Kotlin系列》之MVVM架构封装(kotlin+mvvm)
同站攻击(相关域攻击)论文阅读 Can I Take Your Subdomain?Exploring Same-Site Attacks in the Modern Web
Tupu 3D visual national style design | collision between technology and culture "cool" spark“