排序算法之希尔排序的思想以及Java实现
2024-10-15 14:23:36
1 基本思想
shell排序又称之为缩小增量排序,基本思想是,先将待排序序列分割成若干个特殊的子表,分别进行插入排序,当整个表中元素”基本有序”时,再对全体记录进行一次直接插入排序。该方法实质上是一个分组插入方法。
2,算法的实现(Java)
package Algorithm;
public class ShellSort {
/**
* @param args
*/
public static void main(String[] args) {
int[] data = new int[] {11,10,55,78,100,111,45,56,79,90,345,1000};
System.out.println("排序之前:");
ShellSort.output(data);
System.out.println();
System.out.println("排序之后:");
ShellSort.Shell_Sort(data);
ShellSort.output(data);
}
//带增量的插入排序
public static void Shell_Sort(int[] arr){
int s = 1;
while (s < arr.length)
s = s * 3 + 1;
while (s >= 1) {
for (int i = 1; i < arr.length; i++) {
for (int j = i; j >= s; j = j - s) {
if (arr[j] < arr[j - s]) {
int temp = arr[j];
arr[j]= arr[j-s];
arr[j-s] = temp;
} else{
break;
}
}
}
s = s / 3;
}
}
//输出打印
public static void output(int[] arr){
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+"\t");
}
}
}
最终结果显示如下:
3,性能分析
shell排序需要一个记录的辅助空间,它是一种不稳定的排序。
最新文章
- 几种linux脚本的简单执行方法
- ( 转)基于.NET平台常用的框架整理
- ACM学习之路————一个大整数与一个小整数不得不说得的秘密
- 【转】Mac OS X开机启动Path had bad permissions错误解决方案
- ORACLE 如何定位消耗资源的SQL
- mini2440移植uboot-2008.10 (一)
- Sed&;awk笔记之sed篇
- 最近国外很拉风的,,基于.net 的一个手表
- [Mugeda HTML5技术教程之9]使用元件
- Ansible之 Inventory 资源清单介绍
- DAY5-小别-2018-1-15
- 【Java学习笔记之二十】final关键字在Java继承中的用法小结
- 《玩转spring全家桶》学习笔记-------------丁雪丰
- 基于vue的图片查看插件vue-photo-preview
- Python全栈开发之路 【第十八篇】:Ajax技术
- orm操作
- Python3基础 dict clear 清空一个字典
- 【命令】MongoDB常用命令记录
- nigos core 安装配置
- vue实现点击区域外部的区域,关闭该区域