排序- 希尔排序 ShellSort

本文详细介绍了一种希尔排序算法的具体实现过程,并通过实例演示了如何逐步完成数组从小到大的排序。该算法采用增量分组的方式进行排序,随着增量的减少,最终达到完全排序的目的。
package genius.sort;

import java.util.Arrays;

public class ShellSortFinal {
    public static void shellSortSmallToBig(int[] data) {
        int j = 0;
        int temp = 0;
        //外循环控制循环的次数,也就是分组希尔排序的次数....直到分组个数为1.。。
        System.out.println("the length of the array is "+data.length);
        for (int increment = data.length / 2; increment > 0; increment /= 2) {
            System.out.println("当分组数为increment:" + increment);
            for (int i = increment; i < data.length; i++) {
                temp = data[i];
                for (j = i - increment; j >= 0; j -= increment) {
                    /* System.out.println("j:" + j);
                     System.out.println("temp:" + temp);
                     System.out.println("data[" + j + "]:" + data[j]);*/
                    if (temp < data[j]) {
                        data[j + increment] = data[j];
                    } else {
                        break;
                    }
                }
                data[j + increment] = temp;
            }
            for (int i = 0; i < data.length; i++){
                System.out.print(data[i] + " ");
            }
            System.out.print("\n");
        }
    }

    public static void main(String[] args) {
        int[] data = new int[] { 26, 53, 67, 48, 57, 13, 48, 32, 60, 50 ,1};
        System.out.println(Arrays.toString(data));
        shellSortSmallToBig(data);
        System.out.println("ShellSortFinal.main() Excuted the final Result.......");
        System.out.println(Arrays.toString(data));
    }

}
[26, 53, 67, 48, 57, 13, 48, 32, 60, 50, 1]
the length of the array is 11
当分组数为increment:5
1 48 32 48 50 13 53 67 60 57 26 
当分组数为increment:2
1 13 26 48 32 48 50 57 53 67 60 
当分组数为increment:1
1 13 26 32 48 48 50 53 57 60 67 
ShellSortFinal.main() Excuted the final Result.......
[1, 13, 26, 32, 48, 48, 50, 53, 57, 60, 67]
### 希尔排序算法详解 #### 算法概述 希尔排序(Shell Sort),亦称为递减增量排序算法,是对插入排序的一种优化版本[^1]。该算法由Donald Shell于1959年提出,并在论文“A high-speed sorting procedure”中对其进行了详细的阐述[^3]。 #### 工作原理 希尔排序通过比较相隔一定间隔的元素来工作,这些间隔逐渐减少直到变为1。当间隔为1时,希尔排序即成为普通的插入排序。这种策略使得远距离的数据可以更快地移动到接近其最终位置的地方,从而提高了整体性能[^4]。 #### 时间复杂度分析 尽管具体的渐近时间复杂度取决于所使用的间隔序列,但在最坏情况下,希尔排序的时间复杂度通常优于简单的插入排序。对于某些特定的选择间隔序列,平均情况下的表现甚至能够达到接近\( O(n \log n) \)。 #### 实现细节 以下是使用Python编写的希尔排序的具体实现: ```python def shell_sort(arr): n = len(arr) gap = n // 2 while gap > 0: for i in range(gap, n): temp = arr[i] j = i while j >= gap and arr[j - gap] > temp: arr[j] = arr[j - gap] j -= gap arr[j] = temp gap //= 2 if __name__ == "__main__": test_array = [64, 34, 25, 12, 22, 11, 90] print("原始数组:", test_array) shell_sort(test_array) print("排序后的数组:", test_array) ``` 这段代码展示了如何利用逐步缩小的间隔来进行多次部分有序化的操作,最后完成整个列表的完全排序过程[^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值