当前位置:网站首页>Quick sort + bubble sort + insert sort + select sort
Quick sort + bubble sort + insert sort + select sort
2022-06-23 07:43:00 【Donkey of the production team】
Code
Selection sort
public class Solution {
/** * @param a: an integer array * @return: nothing */
public void sortIntegers(int[] a) {
// write your code here
int n = a.length;
for (int i = 0; i < n - 1; i++){
int min = i;
for (int j = i + 1; j < n; j++){
if (a[j] < a[min]){
min = j;
}
}
swap(a, i, min);
}
}
public void swap(int[] a, int index1, int index2){
int temp = a[index1];
a[index1] = a[index2];
a[index2] = temp;
}
}
Bubble sort
// bubble sort
public class Solution{
public void sortIntegers(int[] a){
int n = a.length;
for (int i = 0; i < n- 1;i++){
for (int j = 0; j < n - 1 - i; j++){
if (a[j] > a[j+1]) swap(a, j, j+1);
}
}
}
public void swap(int[] a, int index1, int index2){
int temp = a[index1];
a[index1] = a[index2];
a[index2] = temp;
}
}
Quick sort
public class Solution{
public void sortIntegers(int[] a) {
int low = 0;
int hight = a.length - 1;
quickSort(a, low, hight);
}
public void quickSort(int[] a, int low, int hight){
if (low > hight) return;
int left, right, pivot;
left = low;
right = hight;
pivot = a[left];
while(left < right){
while(left < right && a[right] >= pivot) right--;
if (left < right) a[left] = a[right];
while(left < right && a[left] <= pivot) left++;
if (left < right) a[right] = a[left];
if (left == right) a[left] = pivot;
}
quickSort(a, low, right - 1);
quickSort(a, right + 1, hight);
}
}
Quick sort In the video Do watch the video , Don't look at the code , Look directly at the code Not easy to understand
Horse soldiers or something , It is not easy to understand , Look at this
Insertion sort
public class Solution{
public void sortIntegers(int[] a){
int n = a.length;
for (int i = 1; i < n; i++){
int temp = a[i];
int j = i;
while (j > 0 && a[j - 1] > temp){
a[j] = a[j - 1];
j--;
}
a[j] = temp;
}
}
}
https://www.bilibili.com/video/BV1at411T75o?spm_id_from=333.337.search-card.all.click&vd_source=8d8fef6cad2875d6b6b4c08c3a9ac66d
边栏推荐
- Both are hard disk partitions. What is the difference between C disk and D disk?
- C WPF realizes dynamic loading of controls through binding
- Eureka服务注册与发现
- unity 音频可视化方案
- MySQL (IV) - MySQL storage engine
- [veusz] import 2D data in CSV
- 小爱音箱连接网络异常解决办法
- 跳跃表原理
- Product axure9 (English version), prototype design and production pull-down secondary menu
- Live broadcast review | how can the container transformation of traditional applications be fast and stable?
猜你喜欢

MySQL on duplicate key and PgSQL on conflict (primary key) handle primary key conflicts

Eureka服务注册与发现
![[* * * array * * *]](/img/fe/2520d85faab7d1fbf5036973ff5965.png)
[* * * array * * *]

Ntu-rgbd data set download and data format analysis
![[cloud computing event] vocational skill competition -- container development example pig rapid development framework](/img/4b/393b5980090330602a501867c60089.png)
[cloud computing event] vocational skill competition -- container development example pig rapid development framework

Focusing on the industry, enabling customers | release of solutions for the five industries of the cloud container cloud product family

《一周的朋友》

跳跃表原理

启动appium

Eureka service registration and discovery
随机推荐
2022山东大学软件学院软件项目管理期末考试(回忆版)
WPS for thesis writing installs MathType plug-in to write mathematical formulas
3DMAX plug-in development environment configuration and fileexport and utilities template testing
[pyqt5 series] modify the counter to realize control
快速排序 + 冒泡排序 + 插入排序 + 选择排序
Test APK exception control nettraffic attacker development
Download the OSS file and modify the file name
Pseudocode specification, pseudocode online editor,
[AI practice] xgbgressor model accelerates training and uses GPU to train xgbgressor in seconds
1.概率论-组合分析
Basic experiment of data statistics and analysis - basic grammar and operation
Can you think of a better way to solve the problem of string inversion?
Qt工程报错:-1: error: Cannot run compiler ‘clang++‘. Output:mingw32-make.exe
How to tag and label naming before the project release
MySQL (II) - MySQL data type
Yolov5 detecting small targets (with source code)
论文写作之WPS安装Mathtype插件编写数学公式
Live broadcast review | how can the container transformation of traditional applications be fast and stable?
The road to hcip MPLS
Ntu-rgbd data set download and data format analysis