当前位置:网站首页>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
边栏推荐
- Data types in tensorflow
- Test APK exception control nettraffic attacker development
- 30 sets of report templates necessary for the workplace, meeting 95% of the report needs, and no code is required for one click application
- RTMP streaming exception fast recovery scheme
- Yan's DP analysis
- Solutions to abnormal network connection of Xiaoai speakers
- Intelligence Education - how to merge codes when code conflicts occur in multi person collaborative development?
- 浅析 Open API 设计规范
- [深度学习][原创]如何不用yolov5权重或者模型进行目标检测和绘制map等参数图
- What is distributed?
猜你喜欢

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

利用for循环输出一个字母三角形

Qt工程报错:-1: error: Cannot run compiler ‘clang++‘. Output:mingw32-make.exe

JS to determine the added and decreased elements of two arrays

Using the for loop to output an alphabetic triangle

Deploy kubersphere in kubernetes

干货来了|《PaaS》合辑抢先看~

The sandbox has reached a cooperation with football player to bring popular football cartoons and animation into the metauniverse

在线JSON转CSharp(C#)Class工具
![[deep learning] [original] how to detect targets and draw map and other parameter maps without yolov5 weights or models](/img/f3/ff14cb5439a24e26f977e5f0d15785.png)
[deep learning] [original] how to detect targets and draw map and other parameter maps without yolov5 weights or models
随机推荐
1278_FreeRTOS_借助prvAddCurrentTaskToDelayedList接口理解delayed task
The eighth experiment of hcip Road
2022山东大学软件学院软件项目管理期末考试(回忆版)
1.概率论-组合分析
Matlab随机波动率SV、GARCH用MCMC马尔可夫链蒙特卡罗方法分析汇率时间序列
[AI practice] data normalization and standardization of machine learning data processing
The sandbox has reached a cooperation with football player to bring popular football cartoons and animation into the metauniverse
EXCEL VBA 入门与实用例子
传智教育 | 多人协作开发出现代码冲突,如何合并代码?
RTMP streaming exception fast recovery scheme
Using the for loop to output an alphabetic triangle
Can you think of a better way to solve the problem of string inversion?
Playwirght深度入门
Heuristic search strategy
Qt 使用QDomDocument读取xml文件
Feelm joined the Carbon Disclosure Project as an initiative of Smallville to deal with climate change emergencies
User mode and kernel mode
What is the experience of being a data product manager in the financial industry
Decoding and practice of cmaf Technology
JS to determine the added and decreased elements of two arrays