Sort a nearly sorted (or K sorted) array | Set 2 (Gap method – Shell sort)
Last Updated :
03 Sep, 2021
Given an array, arr[] of N elements, where each element is at most K away from its target position, the task is to devise an algorithm that sorts in O(N*log(K)) time.
Examples:
Input: arr[] = {10, 9, 8, 7, 4, 70, 60, 50}, K = 4
Output: 4 7 8 9 10 50 60 70
Explanation:
Follow the steps below to sort the array:
- Start with Gap = K(i.e. 4)
- 10 9 8 7 4 70 60 50, swap the elements at indices 0 and 4. Then the array modifies to {4, 9, 8, 7, 10, 70, 60, 50}.
4 9 8 7 10 70 60 50, Do not swap the elements at indices 1 and 5.
4 9 8 7 10 70 60 50, Do not swap the elements at indices 2 and 6.
4 9 8 7 10 70 60 50, Do not swap the elements at indices 3 and 7.
- Gap = ceiling of 4/2 = 2
- 4 9 8 7 10 70 60 50, Do not swap the elements at indices 0 and 2.
4 9 8 7 10 70 60 50, swap the elements at indices 1 and 3. Then the array modifies to {4, 7, 8, 9, 10, 70, 60, 50}.
4 7 8 9 10 70 60 50, Do not swap the elements at indices 2 and 4.
4 7 8 9 10 70 60 50, Do not swap the elements at indices 3 and 5.
4 7 8 9 10 70 60 50, Do not swap the elements at indices 4 and 6.
4 7 8 9 10 70 60 50, swap the elements at indices 5 and 7. Then the array modifies to {4, 7, 8, 9, 10, 70, 60, 50}.
4 7 8 9 10 50 60 70
- Gap = ceiling of 2/2 = 1
- 4 7 8 9 10 50 60 70, Do not swap the elements at indices 0 and 1.
4 7 8 9 10 50 60 70, Do not swap the elements at indices 1 and 2.
4 7 8 9 10 50 60 70, Do not swap the elements at indices 2 and 3.
4 7 8 9 10 50 60 70, Do not swap the elements at indices 3 and 4.
4 7 8 9 10 50 60 70, Do not swap the elements at indices 4 and 5.
4 7 8 9 10 50 60 70, Do not swap the elements at indices 5 and 6.
4 7 8 9 10 50 60 70, Do not swap the elements at indices 6 and 7.
Input: arr[] = {6, 5, 3, 2, 8, 10, 9}, K = 3
Output: 2 3 5 6 8 9 10
Approach: The given problem Sort a nearly sorted (or K sorted) array is already solved. Here the idea is to use shell sorting to sort the array. The idea used here is similar to the merging step of the In-Place Merge Sort. Follow the steps below to solve the problem:
- Initialize a variable, say Gap with a value K to sort every Gapth element of every sublist.
- Iterate until Gap is greater than 0 and perform the following steps:
- Iterate over the range [0, N-Gap] using the variable i, and in each iteration, if arr[i] is greater than the arr[i+Gap], then swap the array elements.
- Update the Gap as Gap = ceil(Gap/2).
- Finally, after completing the above step print the elements of the array arr[].
C++
#include <bits/stdc++.h>
using namespace std;
int nextGap( double k)
{
if (k < 2)
return 0;
return ceil (k / 2);
}
void printArray( int arr[], int n)
{
for ( int i = 0; i < n; i++)
cout << arr[i] << " " ;
}
void kSort( int arr[], int K, int n)
{
for ( int gap = K; gap > 0; gap = nextGap(gap)) {
for ( int i = 0; i + gap < n; i++) {
if (arr[i] > arr[i + gap]) {
swap(arr[i], arr[i + gap]);
}
}
}
printArray(arr, n);
}
int main()
{
int arr[] = { 10, 9, 8, 7, 4, 70, 60, 50 };
int K = 3;
int n = sizeof (arr) / sizeof (arr[0]);
kSort(arr, K, n);
return 0;
}
|
Java
import java.util.Iterator;
import java.util.PriorityQueue;
class GFG {
static void kSort( int [] arr, int K)
{
for ( int gap = K; gap > 0 ; gap = nextGap(gap)) {
for ( int i = 0 ; i + gap < arr.length; i++) {
if (arr[i] > arr[i + gap]) {
swap(arr, i, i + gap);
}
}
}
printArray(arr);
}
static int nextGap( double k)
{
if (k < 2 )
return 0 ;
return ( int )Math.ceil(k / 2 );
}
static void swap( int [] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
private static void printArray( int [] arr)
{
for ( int i = 0 ; i < arr.length; i++)
System.out.print(arr[i] + " " );
}
public static void main(String[] args)
{
int arr[] = { 10 , 9 , 8 , 7 , 4 , 70 , 60 , 50 };
int K = 3 ;
kSort(arr, K);
}
}
|
Python3
import math
def nextGap(k):
if (k < 2 ):
return 0
return math.ceil(k / 2 )
def printArray(arr, n):
for i in range (n):
print (arr[i], end = " " )
def kSort(arr, K, n):
gap = K
while (gap > 0 ):
i = 0
while (i + gap < n):
if (arr[i] > arr[i + gap]):
arr[i], arr[i + gap] = arr[i + gap], arr[i]
i + = 1
gap = nextGap(gap)
printArray(arr, n)
arr = [ 10 , 9 , 8 , 7 , 4 , 70 , 60 , 50 ]
K = 3
n = len (arr)
kSort(arr, K, n)
|
C#
using System;
class GFG {
static void kSort( int [] arr, int K)
{
for ( int gap = K; gap > 0; gap = nextGap(gap)) {
for ( int i = 0; i + gap < arr.Length; i++) {
if (arr[i] > arr[i + gap]) {
swap(arr, i, i + gap);
}
}
}
printArray(arr);
}
static int nextGap( double k)
{
if (k < 2)
return 0;
return ( int )Math.Ceiling(k / 2);
}
static void swap( int [] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
private static void printArray( int [] arr)
{
for ( int i = 0; i < arr.Length; i++)
Console.Write(arr[i] + " " );
}
public static void Main( string [] args)
{
int []arr = { 10, 9, 8, 7, 4, 70, 60, 50 };
int K = 3;
kSort(arr, K);
}
}
|
Javascript
<script>
function nextGap(k)
{
if (k < 2)
return 0;
return Math.ceil(k / 2);
}
function printArray(arr, n)
{
for (let i = 0; i < n; i++)
document.write(arr[i] + " " );
}
function kSort(arr, K, n)
{
for (let gap = K; gap > 0; gap = nextGap(gap))
{
for (let i = 0; i + gap < n; i++)
{
if (arr[i] > arr[i + gap])
{
let temp = arr[i];
arr[i] = arr[i + gap];
arr[i + gap] = temp;
}
}
}
printArray(arr, n);
}
let arr = [ 10, 9, 8, 7, 4, 70, 60, 50 ];
let K = 3;
let n = arr.length;
kSort(arr, K, n);
</script>
|
Output
4 7 8 9 10 50 60 70
Time Complexity: O(N*log K)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...