📜  对近乎排序(或 K 排序)的数组进行排序 | Set 2 (Gap 方法 – Shell 排序)

📅  最后修改于: 2022-05-13 01:56:08.953000             🧑  作者: Mango

对近乎排序(或 K 排序)的数组进行排序 | Set 2 (Gap 方法 – Shell 排序)

给定一个包含N个元素的数组arr[] ,其中每个元素距其目标位置最多K ,任务是设计一种在O(N*log(K))时间内排序的算法。

例子:

方法:给定的问题对几乎排序(或 K 排序)的数组进行排序已经解决。这里的想法是使用 shell 排序对数组进行排序。这里使用的想法类似于 In-Place Merge Sort 的合并步骤。请按照以下步骤解决问题:

  • 初始化一个变量,例如Gap ,其值为K以对每个Gap th进行排序 每个子列表的元素。
  • 迭代直到Gap大于0并执行以下步骤:
    • 使用变量i在范围[0, N-Gap]上进行迭代,并且在每次迭代中,如果arr[i]大于arr[i+Gap],则交换数组元素。
    • Gap更新为Gap = ceil(Gap/2)。
  • 最后,完成上述步骤后,打印数组arr[]的元素。
C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the nextGap
int nextGap(double k)
{
    if (k < 2)
        return 0;
    return ceil(k / 2);
}
 
// A utility function to print the array
void printArray(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
// Function to sort a K sorted array
void kSort(int arr[], int K, int n)
{
   
    // Iterate until gap is atleast
    // greater than 0
    for (int gap = K; gap > 0; gap = nextGap(gap)) {
 
        // Iterate over the range [0, N]
        for (int i = 0; i + gap < n; i++) {
 
            // If arr[i] is greater
            // than arr[i+gap]
            if (arr[i] > arr[i + gap]) {
 
                // Swap arr[i] and
                // arr[i+gap]
                swap(arr[i], arr[i + gap]);
            }
        }
    }
    printArray(arr, n);
}
 
// Driver Code
int main()
{
 
    // Input
    int arr[] = { 10, 9, 8, 7, 4, 70, 60, 50 };
    int K = 3;
    int n = sizeof(arr) / sizeof(arr[0]);
   
    // Function call
    kSort(arr, K, n);
    return 0;
}
 
// This code is contributed by lokesh potta.


Java
// Java program for the above approach
import java.util.Iterator;
import java.util.PriorityQueue;
 
class GFG {
 
    // Function to sort a K sorted array
    static void kSort(int[] arr, int K)
    {
        // Iterate until gap is atleast
        // greater than 0
        for (int gap = K; gap > 0; gap = nextGap(gap)) {
 
            // Iterate over the range [0, N]
            for (int i = 0; i + gap < arr.length; i++) {
 
                // If arr[i] is greater
                // than arr[i+gap]
                if (arr[i] > arr[i + gap]) {
 
                    // Swap arr[i] and
                    // arr[i+gap]
                    swap(arr, i, i + gap);
                }
            }
        }
        printArray(arr);
    }
 
    // Function to find the nextGap
    static int nextGap(double k)
    {
        if (k < 2)
            return 0;
        return (int)Math.ceil(k / 2);
    }
 
    // Function to swap two elements
    // of the array arr[]
    static void swap(int[] arr, int i, int j)
    {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
 
    // A utility function to print the array
    private static void printArray(int[] arr)
    {
        for (int i = 0; i < arr.length; i++)
            System.out.print(arr[i] + " ");
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Input
        int arr[] = { 10, 9, 8, 7, 4, 70, 60, 50 };
        int K = 3;
 
        // Function call
        kSort(arr, K);
    }
}


Python3
# Python3 program for the above approach
import math
 
# Function to find the nextGap
def nextGap(k):
     
    if (k < 2):
        return 0
         
    return math.ceil(k / 2)
 
# A utility function to print array
def printArray(arr, n):
     
    for i in range(n):
        print(arr[i], end = " ")
 
# Function to sort a K sorted array
def kSort(arr, K, n):
   
    # Iterate until gap is atleast
    # greater than 0
    gap = K
     
    while (gap > 0):
         
        # Iterate over the range [0, N]
        i = 0
        while (i + gap < n):
             
            # If arr[i] is greater
            # than arr[i+gap]
            if (arr[i] > arr[i + gap]):
                 
                # Swap arr[i] and
                # arr[i+gap]
                arr[i], arr[i + gap] = arr[i + gap], arr[i]
                 
            i += 1
         
        gap = nextGap(gap)
         
    printArray(arr, n)
 
# Driver Code
 
# Input
arr = [ 10, 9, 8, 7, 4, 70, 60, 50 ]
K = 3
n = len(arr)
   
# Function call
kSort(arr, K, n)
 
# This code is contributed by target_2


C#
// C# program for the above approach
using System;
 
class GFG {
 
    // Function to sort a K sorted array
    static void kSort(int[] arr, int K)
    {
        // Iterate until gap is atleast
        // greater than 0
        for (int gap = K; gap > 0; gap = nextGap(gap)) {
 
            // Iterate over the range [0, N]
            for (int i = 0; i + gap < arr.Length; i++) {
 
                // If arr[i] is greater
                // than arr[i+gap]
                if (arr[i] > arr[i + gap]) {
 
                    // Swap arr[i] and
                    // arr[i+gap]
                    swap(arr, i, i + gap);
                }
            }
        }
        printArray(arr);
    }
 
    // Function to find the nextGap
    static int nextGap(double k)
    {
        if (k < 2)
            return 0;
        return (int)Math.Ceiling(k / 2);
    }
 
    // Function to swap two elements
    // of the array arr[]
    static void swap(int[] arr, int i, int j)
    {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
 
    // A utility function to print the array
    private static void printArray(int[] arr)
    {
        for (int i = 0; i < arr.Length; i++)
            Console.Write(arr[i] + " ");
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        // Input
        int []arr = { 10, 9, 8, 7, 4, 70, 60, 50 };
        int K = 3;
 
        // Function call
        kSort(arr, K);
    }
}
 
// This code is contributed by ukasp.


Javascript


输出
4 7 8 9 10 50 60 70 

时间复杂度: O(N*log K)
辅助空间: O(1)