给定n个元素的数组,其中每个元素距离其目标位置最多k个,设计一种算法,以O(n log k)时间排序。例如,让我们考虑k为2,在排序数组中索引7处的元素可以在给定数组中索引5、6、7、8、9处。可以假设k <n。
例子:
Input: arr[] = {6, 5, 3, 2, 8, 10, 9},
k = 3
Output: arr[] = {2, 3, 5, 6, 8, 9, 10}
Input: arr[] = {10, 9, 8, 7, 4, 70, 60, 50},
k = 4
Output: arr[] = {4, 7, 8, 9, 10, 50, 60, 70}
简单方法:基本解决方案是使用任何标准排序算法对数组进行排序。
CPP14
// A STL based C++ program to
// sort a nearly sorted array.
#include
using namespace std;
// Given an array of size n,
// where every element
// is k away from its target
// position, sorts the
// array in O(n Log n) time.
int sortK(int arr[], int n, int k)
{
// Sort the array using
// inbuilt function
sort(arr, arr + n);
}
// An utility function to print
// array elements
void printArray(
int arr[], int size)
{
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
// Driver program to test
// above functions
int main()
{
int k = 3;
int arr[] = { 2, 6, 3, 12, 56, 8 };
int n = sizeof(arr) / sizeof(arr[0]);
sortK(arr, n, k);
cout << "Following is sorted array\n";
printArray(arr, n);
return 0;
}
Java
// A STL based Java program to
// sort a nearly sorted array.
import java.util.*;
public class GFG
{
// Given an array of size n,
// where every element
// is k away from its target
// position, sorts the
// array in O(n Log n) time.
static void sortK(int[] arr, int n, int k)
{
// Sort the array using
// inbuilt function
Arrays.sort(arr);
}
// An utility function to print
// array elements
static void printArray(
int[] arr, int size)
{
for (int i = 0; i < size; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
// Driver code
public static void main(String[] args)
{
int k = 3;
int[] arr = { 2, 6, 3, 12, 56, 8 };
int n = arr.length;
sortK(arr, n, k);
System.out.println("Following is sorted array");
printArray(arr, n);
}
}
// This code is contributed by divyesh072019.
Python3
# A STL based Java program to
# sort a nearly sorted array.
# Given an array of size n,
# where every element
# is k away from its target
# position, sorts the
# array in O(n Log n) time.
def sortK(arr, n, k):
# Sort the array using
# inbuilt function
arr.sort()
# An utility function to print
# array elements
def printArray(arr, size):
for i in range(size):
print(arr[i], end = " ")
print()
# Driver code
k = 3
arr = [ 2, 6, 3, 12, 56, 8]
n = len(arr)
sortK(arr, n, k)
print("Following is sorted array")
printArray(arr, n)
# This code is contributed by avanitrachhadiya2155
C#
// A STL based C# program to
// sort a nearly sorted array.
using System;
class GFG
{
// Given an array of size n,
// where every element
// is k away from its target
// position, sorts the
// array in O(n Log n) time.
static void sortK(int[] arr, int n, int k)
{
// Sort the array using
// inbuilt function
Array.Sort(arr);
}
// An utility function to print
// array elements
static void printArray(
int[] arr, int size)
{
for (int i = 0; i < size; i++)
Console.Write(arr[i] + " ");
Console.WriteLine();
}
// Driver code
static void Main()
{
int k = 3;
int[] arr = { 2, 6, 3, 12, 56, 8 };
int n = arr.Length;
sortK(arr, n, k);
Console.WriteLine("Following is sorted array");
printArray(arr, n);
}
}
// This code is contributed by divyeshrabadiya07.
CPP
// A STL based C++ program to sort
// a nearly sorted array.
#include
using namespace std;
// Given an array of size n,
// where every element
// is k away from its target
// position, sorts the
// array in O(nLogk) time.
int sortK(int arr[], int n, int k)
{
// Insert first k+1 items in a
// priority queue (or min heap)
// (A O(k) operation)
priority_queue,
greater >
pq(arr, arr + k + 1);
// i is index for remaining
// elements in arr[] and index
// is target index of for
// current minimum element in
// Min Heapm 'hp'.
int index = 0;
for (int i = k + 1; i < n; i++) {
arr[index++] = pq.top();
pq.pop();
pq.push(arr[i]);
}
while (pq.empty() == false) {
arr[index++] = pq.top();
pq.pop();
}
}
// A utility function to print
// array elements
void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
// Driver program to test above functions
int main()
{
int k = 3;
int arr[] = { 2, 6, 3, 12, 56, 8 };
int n = sizeof(arr) / sizeof(arr[0]);
sortK(arr, n, k);
cout << "Following is sorted arrayn";
printArray(arr, n);
return 0;
}
输出:
Following is sorted arrayn2 3 6 8 12 56
复杂度分析:
- 时间复杂度: O(n log n),其中n是数组的大小。
排序算法花费log n次。由于数组的大小为n,因此整个程序需要O(n log n)的时间。 - 空间复杂度: O(1)。
由于不需要额外的空间。
高效解决方案:滑动窗口技术。
方法:更好的解决方案是使用优先级队列(或堆数据结构)。使用滑动窗口技术将窗口的连续k个元素保留在堆中。然后删除顶部元素(最小元素),并用它替换窗口的第一个元素。
由于每个元素最多相距k个距离,因此将k个连续元素保留在一个窗口中,同时用从i到(i + k)的最小元素替换第i个元素就足够了(首先对i-1个元素进行排序)。
算法 :
- 建立第一个(k + 1)个元素的优先级队列pq。
- 初始化索引= 0(对于结果数组)。
- 对从k + 1到n-1的元素执行以下操作。
- 从pq弹出一个项目,并将其放在索引处,即递增索引处。
- 将arr [i]推至pq。
- 虽然pq不为空,
从pq弹出一个项目,并将其放在索引处,即递增索引处。
我们已经在对几乎排序(或K排序)的数组进行排序中讨论了一个简单的实现。在本文中,完成了基于STL的实现。
CPP
// A STL based C++ program to sort
// a nearly sorted array.
#include
using namespace std;
// Given an array of size n,
// where every element
// is k away from its target
// position, sorts the
// array in O(nLogk) time.
int sortK(int arr[], int n, int k)
{
// Insert first k+1 items in a
// priority queue (or min heap)
// (A O(k) operation)
priority_queue,
greater >
pq(arr, arr + k + 1);
// i is index for remaining
// elements in arr[] and index
// is target index of for
// current minimum element in
// Min Heapm 'hp'.
int index = 0;
for (int i = k + 1; i < n; i++) {
arr[index++] = pq.top();
pq.pop();
pq.push(arr[i]);
}
while (pq.empty() == false) {
arr[index++] = pq.top();
pq.pop();
}
}
// A utility function to print
// array elements
void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
// Driver program to test above functions
int main()
{
int k = 3;
int arr[] = { 2, 6, 3, 12, 56, 8 };
int n = sizeof(arr) / sizeof(arr[0]);
sortK(arr, n, k);
cout << "Following is sorted arrayn";
printArray(arr, n);
return 0;
}
输出:
Following is sorted arrayn2 3 6 8 12 56
复杂度分析:
- 时间复杂度: O(n Log k)。
对于每个元素,将其推入优先级队列,并且由于优先级队列中有k个元素,因此插入和删除操作需要O(log k)时间。 - 辅助空间: O(k)。
要将k个元素存储在优先级队列中,需要O(k)空间。