两种插入类排序:

直接插入排序:

 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);
}
}

  

最新文章

  1. 【踩坑速记】开源日历控件,顺便全面解析开源库打包发布到Bintray/Jcenter全过程(新),让开源更简单~
  2. python-socket模块
  3. vyatta常用操作
  4. Asp.net MVC 之异常处理
  5. NOIP201402比例化简
  6. 16、SQL基础整理(触发器.方便备份)
  7. SQLite数据库管理的相关命令
  8. xcode 上 crash 调试的三种方法
  9. POJ 1721 CARDS(置换群)
  10. PHP Html 弹窗,本页面弹窗子页面
  11. 蓝桥网试题 java 基础练习 数列特征
  12. Python日期时间Date/Time
  13. MySQL的Explain关键字查看是否使用索引
  14. Python--subprocess
  15. spring 生命周期最详解
  16. 【!Important】Java线程死锁查看分析方法
  17. 【Dalston】【第六章】API服务网关(Zuul) 下
  18. C# fckeditor浏览服务器和上传目录不一致,看不到上传过的文件
  19. robot framework + python实现http接口自动化测试框架
  20. POJ Georgia and Bob-----阶梯博弈变形。

热门文章

  1. 通过xshell在本地win主机和远程linux主机传输文件
  2. Linux命令:hexdump
  3. 【转】netty4.1.32 pipeline的添加顺序和执行顺序
  4. 依靠MySQL(frm、MYD、MYI)数据文件恢复
  5. Java 实现 bash命令
  6. Mysql使用Java UUID作为唯一值时使用前缀索引测试
  7. ubuntu 18.04安装ftp服务器
  8. DB2的web可视化客户端工具
  9. linux 清除/var/spool/mail/root日志存储
  10. Vuforia笔记1(Vuforia8.0.10与Unity2018.3.6f1)