提交 bf9f8240 编写于 作者: ultra_zeta's avatar ultra_zeta

Update ShellSort.java

上级 e612667e
流水线 #2546 已通过 ,包含阶段
in 3 分54 秒
package com.yzh;
/**
* 要求通过希尔排序算法对一个数组进行升序排序,使用Java语言实现
* 输入:给定的数组 arr
* 输出:排序好的数组 arr
*/
public class ShellSort {
public int[] solution(int arr[]) {
//请在下方编辑代码
int len = arr.length;
int temp, gap = len / 2;
while (gap > 0) {
for (int i = gap; i < len; i++) {
temp = arr[i];
int preIndex = i - gap;
while (preIndex >= 0 && arr[preIndex] > temp) {
arr[preIndex + gap] = arr[preIndex];
preIndex -= gap;
}
arr[preIndex + gap] = temp;
}
gap /= 2;
}
return arr;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册