当前位置:网站首页>Bubble sort and quick sort
Bubble sort and quick sort
2022-06-11 01:13:00 【Ping_ qing】
1、 Bubble sort
1.1、 Basic introduction
Bubble sort (Bubble Sorting) The basic idea of : By treating the sort sequence from front to back ( Start with elements with smaller subscripts ,, Compare the values of adjacent elements in turn , If reverse order is found, exchange , Gradually move the higher value elements from the front to the back , It's like a bubble under the water coming up .
Because in the process of sorting , Each element keeps getting closer to its place , If there is no exchange after comparison , It means that the sequence is orderly , Therefore, set a flag in the sorting process flag Determine whether elements have been exchanged . So as to reduce unnecessary comparison .
1.2、 Code implementation
// Ascending sort
// Time complexity of bubble sort O(n²)
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {
9,4,2,6,3,7,4,1};
bubbleSort(arr);
System.out.println(" After ordering ");
System.out.println(Arrays.toString(arr));
}
public static void bubbleSort(int[] arr){
int temp = 0; // Temporary variable
boolean flag = false; // Identifying variables , Indicates whether an exchange has occurred
for (int j = 0; j < arr.length - 1; j++) {
for (int i = 0; i < arr.length - 1 - j; i++) {
// If the front number is larger than the back number , Just exchange
if(arr[i] > arr[i+1]){
flag = true;
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
if(!flag){
// In a sequence , No exchange has ever happened
break;
}else{
flag = false; // Reset flag, Make the next judgment
}
}
}
}
2、 Quick sort
2.1、 Introduce
Quick sort (Quicksort) It's an improvement on bubble sort .
The basic idea yes : Divide the data to be sorted into two independent parts by one sorting , All the data in one part is smaller than all the data in the other part , Then according to this method, the two parts of data are sorted quickly , The whole sorting process can recursive ( Open stack , Need space ) Conduct , So that the whole data becomes an ordered sequence
2.2、 Code implementation
// Quick sort
public class QuickSort {
public static void main(String[] args) {
int[] arr = {
15, 0, 10, 5, 25};
quickSort(arr,0, arr.length-1);
System.out.println("arr="+ Arrays.toString(arr));
}
public static void quickSort(int[] arr, int left, int right) {
int l = left; // Left subscript
int r = right; // Right subscript
//pivot Central axis
int pivot = arr[(left + right) / 2];
int temp = 0; // Temporary variable
//while The purpose of the cycle is to make a comparison pivot Small values are placed on the left ; Than pivot Big value to the right
while (l < r) {
// stay pivot I 've been looking for it on the left side of , Find greater than or equal to pivot Value , Just quit
while (arr[l] < pivot) {
l++;
}
// stay pivot I 've been looking for , Find less than or equal to pivot Value , Just quit
while (arr[r] > pivot) {
r--;
}
// If l>=r establish , explain pivot The left and right values of , It's all less than or equal to... On the left pivot Value , And on the right are all greater than or equal to pivot Value
if (l >= r) {
break;
}
// In exchange for
temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
// If after the exchange , Found this arr[l] == pivot Value ,r-- , Move forward
if (arr[l] == pivot) {
r--;
}
// If after the exchange , Found this arr[r] == pivot Value ,l++ , Move backward
if (arr[r] == pivot) {
l++;
}
}
// If l==r , must l++,r--, Otherwise, there will be a stack overflow
if(l==r){
l++;
r--;
}
// Left recursion
if(left <r){
quickSort(arr,left,r);
}
// Right recursion
if(right >l){
quickSort(arr,l,right);
}
}
}
边栏推荐
- Optimization of startup under SYSTEMd, deleting useless SYSTEMd services
- About log traffic monitoring and early warning small project | Kafka vs redis
- dma_buf_export
- 明朝的那些皇帝
- OTA upgrade
- pytorch分类问题总结
- Redis data has been overwritten
- 适配器模式
- ion_ dma_ buf_ begin_ cpu_ access
- NVIDIA Jetson's PWM Fan custom control
猜你喜欢

The best creative drum tool: groove agent 5

Redis data has been overwritten

SQL audit | "cloud" users can use the SQL audit service with one click

招聘 | 南京 | TRIOSTUDIO 三厘社 – 室内设计师 / 施工图深化设计师 / 装置/产品设计师 / 实习生等
![[paper reading] tganet: text guided attention for improved polyp segmentation](/img/e4/a80615e78b819a50886410cc69146d.png)
[paper reading] tganet: text guided attention for improved polyp segmentation

明朝的那些皇帝

NVIDIA Jetson之PWM风扇自定义控制

Small project on log traffic monitoring and early warning | environment and foundation 1

适配器模式

循环结构语句
随机推荐
Viewpager and dot of bottom wireless loop
dma_buf_export
浅谈IEEE会议论文的不出席政策Non-Presented Paper(No-Show)Policy
day01
【NVIDIA驱动的顽固问题】---- /dev/sdax:clean,xxx/xxx files,xxx/xxx blocks ---- 最全解决方法
Traversal (pre order, middle order, post order) and search of binary tree
systemd 下开机启动的优化,删除无用的systemd服务
Load balancing strategy graphic explanation
"Past and present" of permission management
SSH远程登陆配置sshd_config文件详解
库存管理与策略模式
Array simulation [queue] and [ring queue]_ code implementation
网络基础(1)-----认识网络
ZABBIX offline installation
[paper translation] recent advances in open set recognition: a survey
Basic introduction of graph and depth first traversal and breadth first traversal
Promise
87. (leaflet house) leaflet military plotting - straight arrow modification
[论文阅读] BoostMIS: Boosting Medical Image Semi-supervised Learning with Adaptive Pseudo Labeling
Promise