当前位置:网站首页>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);
}
}
}
边栏推荐
- Team management | how to improve the thinking skills of technical leaders?
- ion_ mmap
- Unity points that are vulnerable to pit
- 增额终身寿险产品都有哪些优势?门槛高吗?
- Basic introduction of graph and depth first traversal and breadth first traversal
- [introduction to ROS] - 03 basic concepts and instructions of ROS
- compiler explorer
- qt程序插件报错plugin xcb
- 明朝的那些皇帝
- SqlServer中的锁
猜你喜欢

CentOS actual deployment redis

Summary of pytorch classification problems

About log traffic monitoring and early warning small project | flask

最好的创意鼓工具:Groove Agent 5
![[paper reading] fixmatch: simplifying semi supervised learning with consistency and confidence](/img/86/72726f933deef6944b62149759b7d5.png)
[paper reading] fixmatch: simplifying semi supervised learning with consistency and confidence

Why can't Google search page infinite?

Alicloud configures SLB (load balancing) instances

最好的創意鼓工具:Groove Agent 5

Teach you the front and back separation architecture (V) system authentication implementation

招聘 | 南京 | TRIOSTUDIO 三厘社 – 室内设计师 / 施工图深化设计师 / 装置/产品设计师 / 实习生等
随机推荐
[paper reading] tganet: text guided attention for improved polyp segmentation
【VBA脚本】提取word文档中所有批注的信息和待解决状态
最好的创意鼓工具:Groove Agent 5
CentOS actual deployment redis
Promise
MySQL trigger
Animation
day01
Blend for visual studio overview
Time dependent - format, operation, comparison, conversion
百万级访问量—高并发问题的解决历程
Non presented paper (no show) policy
WPF - timeline class
循环结构语句
Teach you the front and back separation architecture (V) system authentication implementation
87.(leaflet之家)leaflet军事标绘-直线箭头修改
北京中国专利奖政策支持介绍,补贴100万
LeetCode 8. String to integer (ATOI) (medium, string)
Cosine similarity calculation summary
Clean up the broken artifacts data (.lastUpdated files) and reload the project.问题解决办法