选择排序的优化
package contcurrentandalgorithm;
/**
*
* @author Administrator
* zyyjiao@mail.ustc.edu.cn
*/
public class ShellSort {
public static void main(String[] args) {
int a[] = {2, 1, 3, 6, 5, 4, 8, 7};
shellSort(a, 8);
for (int i = 0; i < 8; i++) {
System.out.println(a[i]);
}
}
public static void shellSort(int a[], int n) {
int k = n / 2;//第一个间距 分成两个一组
while (k > 0) {
for (int i = k; i < n; i++)//从与第一个值间距 n/2开始比较
{
int t = a[i];//与第一个值间距 n/2的值
int j = i - k;//第一趟 i=k 取的第一个数
while (j >= 0 && t < a[j])//后面的数是不是小 ?
{
a[j + k] = a[j];
j = j - k; //如果互换了 这里将变成负的
}
a[j + k] = t;
}
k = k / 2;
}
}
}

本文介绍了一种改进的选择排序算法,通过调整间距实现更高效的排序过程。优化后的算法减少了不必要的比较和交换操作,提高了排序效率。
597

被折叠的 条评论
为什么被折叠?



