📅  最后修改于: 2020-10-15 03:14:41             🧑  作者: Mango
壳排序是插入排序的一般化,它通过比较由多个位置的间隙分隔的元素来克服插入排序的缺点。通常,Shell sort执行以下步骤。
Complexity | Best Case | Average Case | Worst Case |
---|---|---|---|
Time Complexity | Ω(n log(n)) | θ(n log(n)2) | O(n log(n)2) |
Space Complexity | O(1) |
Shell_Sort(Arr,n)
#include
void shellsort(int arr[], int num)
{
int i, j, k, tmp;
for (i = num / 2; i > 0; i = i / 2)
{
for (j = i; j < num; j++)
{
for(k = j - i; k >= 0; k = k - i)
{
if (arr[k+i] >= arr[k])
break;
else
{
tmp = arr[k];
arr[k] = arr[k+i];
arr[k+i] = tmp;
}
}
}
}
}
int main()
{
int arr[30];
int k, num;
printf("Enter total no. of elements : ");
scanf("%d", &num);
printf("\nEnter %d numbers: ", num);
for (k = 0 ; k < num; k++)
{
scanf("%d", &arr[k]);
}
shellsort(arr, num);
printf("\n Sorted array is: ");
for (k = 0; k < num; k++)
printf("%d ", arr[k]);
return 0;
}
输出:
Enter total no. of elements : 6
Enter 6 numbers: 3
2
4
10
2
1
Sorted array is: 1 2 2 3 4 10
import java.util.*;
public class Shell_Sort
{
static void shellsort(int[] arr, intnum)
{
int i, j, k, tmp;
for (i = num / 2; i> 0; i = i / 2)
{
for (j = i; j= 0; k = k - i)
{
if (arr[k+i] >= arr[k])
break;
else
{
tmp = arr[k];
arr[k] = arr[k+i];
arr[k+i] = tmp;
}
}
}
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int arr[]= newint[30];
intk, num;
System.out.println("Enter total no. of elements : ");
num = sc.nextInt();
for (k = 0 ; k
输出:
Enter total no. of elements : 6
3
2
4
10
2
1
Sorted array is: 1 2 2 3 4 10
using System;
public class Shell_Sort
{
static void shellsort(int[] arr, int num)
{
int i, j, k, tmp;
for (i = num / 2; i > 0; i = i / 2)
{
for (j = i; j < num; j++)
{
for(k = j - i; k >= 0; k = k - i)
{
if (arr[k+i] >= arr[k])
break;
else
{
tmp = arr[k];
arr[k] = arr[k+i];
arr[k+i] = tmp;
}
}
}
}
}
public void Main()
{
int[] arr=new int[10];
int k, num;
Console.WriteLine("Enter total no. of elements : ");
num = Convert.ToInt32(Console.ReadLine());
for (k = 0 ; k < num; k++)
{
arr[k]=Convert.ToInt32(Console.ReadLine());
}
shellsort(arr, num);
Console.WriteLine("\n Sorted array is: ");
for (k = 0; k < num; k++)
Console.WriteLine(arr[k]);
}
}
输出:
Enter total no. of elements : 6
3
2
4
10
2
1
Sorted array is: 1 2 2 3 4 10