当前位置:网站首页>快速排序 + 冒泡排序 + 插入排序 + 选择排序
快速排序 + 冒泡排序 + 插入排序 + 选择排序
2022-06-23 07:05:00 【生产队的驴儿】
代码
选择排序
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
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;
}
}
快速排序
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);
}
}
快速排序 的视频 一定看视频,不要看代码,直接看代码 不容易理解
马士兵什么的,没有这个讲的简单易懂,看这个
插入排序
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
边栏推荐
- 传智教育 | 项目发布前如何打tag标签及标签命名规范
- [AI practice] data normalization and standardization of machine learning data processing
- vs在连接SQL时出现的问题myconn.OPen();无法运行
- C WPF additional attribute implementation interface defines decorator
- Elaborate on the operation of idea
- Design of temperature detection and alarm system based on 51 single chip microcomputer
- What is distributed?
- User mode and kernel mode
- Using the for loop to output an alphabetic triangle
- The Sandbox 与《足球小将》达成合作,将流行的足球漫画及动画带入元宇宙
猜你喜欢

【云计算赛项】职业技能竞赛--容器开发部分例题Pig快速开发框架
![[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

Cirium has gradually become the standard for airlines' carbon dioxide emission reporting

How to tag and label naming before the project release

MySQL (VIII) - explain

Design of temperature detection and alarm system based on 51 single chip microcomputer

Intelligence Education - how to merge codes when code conflicts occur in multi person collaborative development?

Detailed explanation of redis persistence, master-slave and sentry architecture

The road to hcip MPLS

基于51单片机的温度检测监测报警系统设计
随机推荐
利用for循环输出一个字母三角形
3dmax插件开发环境配置及FileExport和Utilities模板测试
How bootstrap clears floating styles
Yolov5 detecting small targets (with source code)
TCP fastopen is used inside the origin server to quickly return to the source
To conquer salt fields and vegetable fields with AI, scientific and technological innovation should also step on the "field"
Judge black production based on CDN and client slow log characteristics
Pagoda forgot password
Akamai-1.75 version-_ Abck parameter generation JS reverse analysis
codeforce 158B Taxi
Friends of the week
【云计算赛项】职业技能竞赛--容器开发部分例题Pig快速开发框架
RFID data security experiment: C # visual realization of parity check, CRC redundancy check and Hamming code check
测试apk-异常管控NetTraffic攻击者开发
Heterogeneous transaction scenario interaction process and consistency assurance
Nacos adapts Oracle11g create table DDL statement
char和varchar区别
Arthas thread command locates thread deadlock
《一周的朋友》
Deploy kubersphere in kubernetes