java实现4种内部排序
2024-09-03 20:53:00
两种插入类排序:
直接插入排序:
public static void insertSort(int[] arr, int len){
for(int i=1; i<len; i++){
int temp = arr[i];
int j=i-1;
while(j>=0 && temp<arr[j]){
arr[j+1] = arr[j];
--j;
}
arr[j+1] = temp;
}
}
折半插入排序:
public static void binarySort(int[] arr,int len){
for(int i=1; i<len; i++){
int temp = arr[i];
int low = 0, high = i-1;
while(low<=high){
int mid=(low+high)/2;
if(arr[i] > arr[mid]) low=mid+1;
else high = mid-1;
}
for(int j=i; j>low; --j){
arr[j] = arr[j-1];
}
arr[low] = temp;
}
}
两种交换类排序:
冒泡排序:
public static void bubbleSort(int[] arr,int len){
for(int i=len-1; i>=1; i--){
for(int j=1; j<=i; j++){
if(arr[j]<arr[j-1]){
int temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
}
}
}
}
快速排序:
public static void quickSort(int[] arr,int low, int high){
if(low<high){
int temp = arr[low];
int i = low,j = high;
while(i!=j){
while(i<j && arr[j]>=temp) --j;
if(i<j){
arr[i] = arr[j];
i++;
}
while(i<j && arr[i] < temp) ++i;
if(i<j) {
arr[j] = arr[i];
j--;
}
}
arr[i] = temp;
quickSort(arr,0,i-1);
quickSort(arr, i+1,high);
}
}
时间复杂度和空间复杂度:
方法调用:
public static void main(String[] args) {
int arr[] = {23,3,7,22,1,9,2};
//bubbleSort(arr,arr.length);
//quickSort(arr,0, arr.length-1);
//binarySort(arr,arr.length);
insertSort(arr,arr.length);
System.out.print("排序后为:");
for(int x : arr){
System.out.printf("%d,",x);
}
}
最新文章
- 【踩坑速记】开源日历控件,顺便全面解析开源库打包发布到Bintray/Jcenter全过程(新),让开源更简单~
- python-socket模块
- vyatta常用操作
- Asp.net MVC 之异常处理
- NOIP201402比例化简
- 16、SQL基础整理(触发器.方便备份)
- SQLite数据库管理的相关命令
- xcode 上 crash 调试的三种方法
- POJ 1721 CARDS(置换群)
- PHP Html 弹窗,本页面弹窗子页面
- 蓝桥网试题 java 基础练习 数列特征
- Python日期时间Date/Time
- MySQL的Explain关键字查看是否使用索引
- Python--subprocess
- spring 生命周期最详解
- 【!Important】Java线程死锁查看分析方法
- 【Dalston】【第六章】API服务网关(Zuul) 下
- C# fckeditor浏览服务器和上传目录不一致,看不到上传过的文件
- robot framework + python实现http接口自动化测试框架
- POJ Georgia and Bob-----阶梯博弈变形。
热门文章
- 通过xshell在本地win主机和远程linux主机传输文件
- Linux命令:hexdump
- 【转】netty4.1.32 pipeline的添加顺序和执行顺序
- 依靠MySQL(frm、MYD、MYI)数据文件恢复
- Java 实现 bash命令
- Mysql使用Java UUID作为唯一值时使用前缀索引测试
- ubuntu 18.04安装ftp服务器
- DB2的web可视化客户端工具
- linux 清除/var/spool/mail/root日志存储
- Vuforia笔记1(Vuforia8.0.10与Unity2018.3.6f1)