ShellSort主要是插入排序的一种变体。在插入排序中,我们仅将元素向前移动一个位置。当一个元素必须向前移动时,涉及许多移动。 shellSort的想法是允许交换远项。在shellSort中,我们对数组进行h排序以获得较大的h值。我们一直将h的值减小到1。如果每个第h个元素的所有子列表都被排序,则称数组为h排序。
以下是ShellSort的实现。
C++
// C++ implementation of Shell Sort
#include
using namespace std;
/* function to sort arr using shellSort */
int shellSort(int arr[], int n)
{
// Start with a big gap, then reduce the gap
for (int gap = n/2; gap > 0; gap /= 2)
{
// Do a gapped insertion sort for this gap size.
// The first gap elements a[0..gap-1] are already in gapped order
// keep adding one more element until the entire array is
// gap sorted
for (int i = gap; i < n; i += 1)
{
// add a[i] to the elements that have been gap sorted
// save a[i] in temp and make a hole at position i
int temp = arr[i];
// shift earlier gap-sorted elements up until the correct
// location for a[i] is found
int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
arr[j] = arr[j - gap];
// put temp (the original a[i]) in its correct location
arr[j] = temp;
}
}
return 0;
}
void printArray(int arr[], int n)
{
for (int i=0; i
Java
// Java implementation of ShellSort
class ShellSort
{
/* An utility function to print array of size n*/
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i 0; gap /= 2)
{
// Do a gapped insertion sort for this gap size.
// The first gap elements a[0..gap-1] are already
// in gapped order keep adding one more element
// until the entire array is gap sorted
for (int i = gap; i < n; i += 1)
{
// add a[i] to the elements that have been gap
// sorted save a[i] in temp and make a hole at
// position i
int temp = arr[i];
// shift earlier gap-sorted elements up until
// the correct location for a[i] is found
int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
arr[j] = arr[j - gap];
// put temp (the original a[i]) in its correct
// location
arr[j] = temp;
}
}
return 0;
}
// Driver method
public static void main(String args[])
{
int arr[] = {12, 34, 54, 2, 3};
System.out.println("Array before sorting");
printArray(arr);
ShellSort ob = new ShellSort();
ob.sort(arr);
System.out.println("Array after sorting");
printArray(arr);
}
}
/*This code is contributed by Rajat Mishra */
Python3
# Python3 program for implementation of Shell Sort
def shellSort(arr):
# Start with a big gap, then reduce the gap
n = len(arr)
gap = n//2
# Do a gapped insertion sort for this gap size.
# The first gap elements a[0..gap-1] are already in gapped
# order keep adding one more element until the entire array
# is gap sorted
while gap > 0:
for i in range(gap,n):
# add a[i] to the elements that have been gap sorted
# save a[i] in temp and make a hole at position i
temp = arr[i]
# shift earlier gap-sorted elements up until the correct
# location for a[i] is found
j = i
while j >= gap and arr[j-gap] >temp:
arr[j] = arr[j-gap]
j -= gap
# put temp (the original a[i]) in its correct location
arr[j] = temp
gap //= 2
# Driver code to test above
arr = [ 12, 34, 54, 2, 3]
n = len(arr)
print ("Array before sorting:")
for i in range(n):
print(arr[i]),
shellSort(arr)
print ("\nArray after sorting:")
for i in range(n):
print(arr[i]),
# This code is contributed by Mohit Kumra
C#
// C# implementation of ShellSort
using System;
class ShellSort
{
/* An utility function to
print array of size n*/
static void printArray(int []arr)
{
int n = arr.Length;
for (int i=0; i 0; gap /= 2)
{
// Do a gapped insertion sort for this gap size.
// The first gap elements a[0..gap-1] are already
// in gapped order keep adding one more element
// until the entire array is gap sorted
for (int i = gap; i < n; i += 1)
{
// add a[i] to the elements that have
// been gap sorted save a[i] in temp and
// make a hole at position i
int temp = arr[i];
// shift earlier gap-sorted elements up until
// the correct location for a[i] is found
int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
arr[j] = arr[j - gap];
// put temp (the original a[i])
// in its correct location
arr[j] = temp;
}
}
return 0;
}
// Driver method
public static void Main()
{
int []arr = {12, 34, 54, 2, 3};
Console.Write("Array before sorting :\n");
printArray(arr);
ShellSort ob = new ShellSort();
ob.sort(arr);
Console.Write("Array after sorting :\n");
printArray(arr);
}
}
// This code is contributed by nitin mittal.
输出:
Array before sorting:
12 34 54 2 3
Array after sorting:
2 3 12 34 54
时间复杂度:上述shellsort实现的时间复杂度为O(n 2 )。在上面的实现中,每次迭代的差距减少了一半。还有许多其他方法可以减少时间间隔,从而导致更好的时间复杂度。有关更多详细信息,请参见此。
参考:
https://www.youtube.com/watch?v=pGhazjsFW28
http://en.wikipedia.org/wiki/Shellsort
快照:
壳排序测验
GeeksforGeeks / GeeksQuiz上的其他排序算法:
- 选择排序
- 气泡排序
- 插入排序
- 合并排序
- 堆排序
- 快速排序
- 基数排序
- 计数排序
- 桶分类
排序的编码实践。