【数据结构】希尔排序
2015-12-22 11:49
375 查看
对于一个int数组,请编写一个希尔排序算法,对数组元素排序。
给定一个int数组A及数组的大小n,请返回排序后的数组。保证元素小于等于2000。
测试样例:
[1,2,3,5,2,3],6
[1,2,2,3,3,5]
给定一个int数组A及数组的大小n,请返回排序后的数组。保证元素小于等于2000。
测试样例:
[1,2,3,5,2,3],6
[1,2,2,3,3,5]
[code]#include<iostream> #include<string> using namespace std; class ShellSort { public: void display(int * A, int n){ for (int i = 0; i < n; i++){ printf("%d ", A[i]); } printf("\n"); } void swap(int * A, int * B){ int temp = *A; *A = *B; *B = temp; } int* shellSort(int* A, int n) { // write code here int step = 3; while (step > 0){ for (int i = step; i < n; i++){ int j = i; while (j >= step){ if (A[j] < A[j - step]){ swap(A + j, A + j - step); j -= step; } else{ break; } }//end while j }//end for step--; } return A; } }; int main() { ShellSort s; int A[] = { 54, 35, 48, 36, 27, 12, 44, 44, 8, 14, 26, 17, 28 }; int n = 13; s.shellSort(A, n); s.display(A, n); }
相关文章推荐
- 【Linux 驱动】netfilter/iptables (二) Netfilter hook 数据结构
- 数据结构随笔
- 数据结构总结—林莉
- 数据结构总结-焦梦真
- 数据结构之线性表顺序结构
- 数据结构与算法 LeetCode编程练习--Delete Node in a Linked List
- 数据结构基础之栈
- 数据结构中各种内部排序算法的比较
- poj 食物链 数据结构 并查集
- 数据结构学期学习总结
- 数据结构基础(二)栈和队列
- 09 DirectoryStructure
- 数据结构实验之排序六:希尔排序
- UI day 14 XML JSON数据结构解析
- 数据结构2 二叉树的高度和宽度
- 对数据库索引的数据结构学习
- 探索 ConcurrentHashMap 高并发性的实现机制
- 数据结构:二叉搜索树(BST)的基本操作
- 数据结构:二叉搜索树(BST)的基本操作
- 数据结构与算法书籍推荐