常被提到的排序四(希尔排序)

希尔排序

下面是《算法》书中对希尔排序的解释:

希尔排序的思想是使数组中任意介个为h的元素都是有序的。这样的数组被称为h有序数组。换句话说,一个h有序数组就是h个互相独立的有序数组编织在一组成的一个数组。在及进行排序时,如果h很大,我们就能将元素移动到很远的地方,为实现更小的h有序创造方便。用这种方式,对于任意以1结尾的h序列,我们都能够将数组排序。

希尔排序:
这里写图片描述

希尔排序我个人 的理解:

 * 按我个人的理解是将整个数组先分成等长度的小数组
 * 然后先将每个数据的第一数按顺序排序
 * 排序完后再将数组的区间继续缩小
 * 知道排序完成


代码实现:
public class ShellSort {
	public static void sort(int[] source){
		int N = source.length;
		int h = N /2;
		
		while(h >= 1){
			//将数据变为h有序
			for (int i = h; i < source.length; i++) {
				//将source[i]插入到source[i-h],source[i-2h],source[i-3h],source[i-4h]...之中
				for(int j = i ; j >= 0 && (source[i] > source[j]); j =j-h){
					swap(source, j , j-h);
				}
			}
			//第一轮排序完将区间减小
			h = h/2;
		}
	}
	/*
	 * 创建方法,由于交换数的位置
	 */
	private static void swap(int[] source, int x, int y) {
			
		int temp = source[x];
		
		source[x] = source[y];
		
		source[y] = temp;
	}
	
	public static void main(String[] args) {
		int[] a = {1,2,3,7,3,4,12,-23,54,76};
		sort(a);
		for (int i = 0; i < 10; i++) {
			System.out.print(a[i] +" ");
			
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您解答这个问题。 以下是您所提到的七种排序算法的C/C++代码实现,每个算法均有对应的注释说明。其中,基排序的实现使用了队列(Queue)和链表(List)两种据结构。 ```C++ #include <iostream> #include <vector> #include <queue> using namespace std; // 直接插入排序 void InsertionSort(vector<int>& nums) { int n = nums.size(); for (int i = 1; i < n; ++i) { int key = nums[i]; int j = i - 1; while (j >= 0 && nums[j] > key) { nums[j + 1] = nums[j]; --j; } nums[j + 1] = key; } } // 希尔排序 void ShellSort(vector<int>& nums) { int n = nums.size(); for (int gap = n / 2; gap > 0; gap /= 2) { for (int i = gap; i < n; ++i) { int key = nums[i]; int j = i; while (j >= gap && nums[j - gap] > key) { nums[j] = nums[j - gap]; j -= gap; } nums[j] = key; } } } // 快速排序 void QuickSort(vector<int>& nums, int left, int right) { if (left >= right) return; int i = left, j = right; int pivot = nums[left]; while (i < j) { while (i < j && nums[j] >= pivot) --j; nums[i] = nums[j]; while (i < j && nums[i] <= pivot) ++i; nums[j] = nums[i]; } nums[i] = pivot; QuickSort(nums, left, i - 1); QuickSort(nums, i + 1, right); } // 冒泡排序 void BubbleSort(vector<int>& nums) { int n = nums.size(); for (int i = 0; i < n - 1; ++i) { bool flag = false; for (int j = 0; j < n - i - 1; ++j) { if (nums[j] > nums[j + 1]) { swap(nums[j], nums[j + 1]); flag = true; } } if (!flag) break; } } // 堆排序 void HeapSort(vector<int>& nums) { int n = nums.size(); for (int i = n / 2 - 1; i >= 0; --i) { int j = i; while (2 * j + 1 < n) { int k = 2 * j + 1; if (k + 1 < n && nums[k + 1] > nums[k]) ++k; if (nums[k] > nums[j]) { swap(nums[k], nums[j]); j = k; } else { break; } } } for (int i = n - 1; i > 0; --i) { swap(nums[0], nums[i]); int j = 0; while (2 * j + 1 < i) { int k = 2 * j + 1; if (k + 1 < i && nums[k + 1] > nums[k]) ++k; if (nums[k] > nums[j]) { swap(nums[k], nums[j]); j = k; } else { break; } } } } // 选择排序 void SelectionSort(vector<int>& nums) { int n = nums.size(); for (int i = 0; i < n - 1; ++i) { int min_idx = i; for (int j = i + 1; j < n; ++j) { if (nums[j] < nums[min_idx]) { min_idx = j; } } swap(nums[i], nums[min_idx]); } } // 基排序 void RadixSort(vector<int>& nums) { int n = nums.size(); int max_val = *max_element(nums.begin(), nums.end()); int d = 1; // 位 while (max_val /= 10) ++d; queue<int> q[10]; for (int i = 0; i < d; ++i) { for (int j = 0; j < n; ++j) { int k = nums[j] / pow(10, i) % 10; q[k].push(nums[j]); } int idx = 0; for (int j = 0; j < 10; ++j) { while (!q[j].empty()) { nums[idx++] = q[j].front(); q[j].pop(); } } } } int main() { vector<int> nums = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}; cout << "Original: "; for (int x : nums) cout << x << " "; cout << endl; InsertionSort(nums); cout << "InsertionSort: "; for (int x : nums) cout << x << " "; cout << endl; nums = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}; ShellSort(nums); cout << "ShellSort: "; for (int x : nums) cout << x << " "; cout << endl; nums = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}; QuickSort(nums, 0, nums.size() - 1); cout << "QuickSort: "; for (int x : nums) cout << x << " "; cout << endl; nums = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}; BubbleSort(nums); cout << "BubbleSort: "; for (int x : nums) cout << x << " "; cout << endl; nums = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}; HeapSort(nums); cout << "HeapSort: "; for (int x : nums) cout << x << " "; cout << endl; nums = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}; SelectionSort(nums); cout << "SelectionSort: "; for (int x : nums) cout << x << " "; cout << endl; nums = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}; RadixSort(nums); cout << "RadixSort: "; for (int x : nums) cout << x << " "; cout << endl; return 0; } ``` 您可以将这段代码复制到Visual Studio等开发环境中进行编译运行,输入待排序记录后即可看到各个排序算法的输出结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值