当前位置:网站首页>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
边栏推荐
- [veusz] import 2D data in CSV
- Spock constraint - call frequency / target / method parameters
- [AI practice] xgb Xgbregression multioutputregressor parameter 1
- 一篇文章学会er图绘制
- Which company would like to buy serious illness insurance in 2022?
- Online JSON to CSharp (c) class tool
- Detailed explanation of redis persistence, master-slave and sentry architecture
- 传智教育 | 多人协作开发出现代码冲突,如何合并代码?
- How to quickly and gracefully download large files from Google cloud disk (II)
- 3DMAX plug-in development environment configuration and fileexport and utilities template testing
猜你喜欢

Yan's DP analysis

U-Net: Convolutional Networks for Biomedical Image Segmentation

How MySQL converts a date to a number

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

传智教育 | 多人协作开发出现代码冲突,如何合并代码?

3DMAX plug-in development environment configuration and fileexport and utilities template testing

【星球精选】如何高效构建 Roam 与 theBrain 间细粒度双向链接?

EXCEL VBA 入门与实用例子

测试apk-异常管控NetTraffic攻击者开发

The eighth experiment of hcip Road
随机推荐
MySQL transaction isolation level
Several characteristics of MySQL database
HCIP之路
论文伪代码规范,伪代码在线编辑器,
跳跃表原理
Download the OSS file and modify the file name
Elaborate on the operation of idea
Spock sub piling
【Veusz】导入CSV中的二维数据
'Latin-1' codec can't encode characters in position 103-115: body ('string of Chinese ') is not valid Latin-1
20bn Jester complete dataset Download
How flannel works
C WPF realizes dynamic loading of controls through binding
[AI practice] xgbgressor model accelerates training and uses GPU to train xgbgressor in seconds
Online JSON to CSharp (c) class tool
Operation on a bit of binary
vs在连接SQL时出现的问题myconn.OPen();无法运行
[markdown] markdown tutorial summary
Wechat multiplayer chat and Roulette Games (websocket Implementation)
Minio single node deployment Minio distributed deployment fool deployment process (I)