Given an array arr[] of N integers, the task is to sort the array using Shell-Metzner sort.
Input: arr[] = {0, -2, 8, 5, 1}
Output: -2 0 1 5 8
Input: arr[] = {4, 5, 6, 1, 100000, 1000}
Output: 1 4 5 6 1000 100000
Prerequisite: Shell Sort
The Shell-Metzner sort is an adaptation of the Shell sort by Marlene Metzner. The Shell-Metzner Sort uses five indices to check which cells to swap. The Metzner version starts with a step size equal to half the length of the array, with each pass increasing the number of comparisons quadratically.
Below is the implementation of Shell-Metzner sort:
C++
#include <bits/stdc++.h>
using namespace std;
void swap( int & a, int & b)
{
int temp = a;
a = b;
b = temp;
}
void sort_shell_metzner( int arr[], int n)
{
int i, j, k, l, m, temp;
m = n;
while (m > 0) {
m /= 2;
k = n - m;
j = 0;
do {
i = j;
do {
l = i + m;
if (arr[i] > arr[l]) {
swap(arr[i], arr[l]);
i -= m;
}
else
break ;
} while (i >= 0);
j++;
} while (j <= k);
}
}
void printArray( int arr[], int n)
{
for ( int i = 0; i < n; i++)
cout << arr[i] << " " ;
}
int main()
{
int arr[] = { 0, -2, 8, 5, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
sort_shell_metzner(arr, n);
printArray(arr, n);
return 0;
}
|
Java
class GFG
{
static int [] swap( int [] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
static void sort_shell_metzner( int arr[], int n)
{
int i, j, k, l, m, temp;
m = n;
while (m > 0 )
{
m /= 2 ;
k = n - m;
j = 0 ;
do
{
i = j;
do
{
l = i + m;
if (l < n && arr[i] > arr[l])
{
swap(arr, i, l);
i -= m;
}
else
{
break ;
}
} while (i >= 0 );
j++;
} while (j <= k);
}
}
static void printArray( int arr[], int n)
{
for ( int i = 0 ; i < n; i++)
{
System.out.print(arr[i] + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 0 , - 2 , 8 , 5 , 1 };
int n = arr.length;
sort_shell_metzner(arr, n);
printArray(arr, n);
}
}
|
Python3
def sort_shell_metzner( arr, n):
m = n;
while (m > 0 ):
m / / = 2
k = n - m
j = 0
while (j < k):
i = j
while (i > = 0 ):
l = i + m
if (arr[i] > arr[l]):
arr[i], arr[l] = arr[l],arr[i]
i - = m
else :
break
j + = 1
def printArray(arr, n):
for i in range ( n):
print ( arr[i], end = " " )
if __name__ = = "__main__" :
arr = [ 0 , - 2 , 8 , 5 , 1 ]
n = len (arr)
sort_shell_metzner(arr, n)
printArray(arr, n)
|
C#
using System;
class GFG
{
static int [] swap( int [] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
static void sort_shell_metzner( int []arr, int n)
{
int i, j, k, l, m, temp;
m = n;
while (m > 0)
{
m /= 2;
k = n - m;
j = 0;
do
{
i = j;
do
{
l = i + m;
if (l < n && arr[i] > arr[l])
{
swap(arr, i, l);
i -= m;
}
else
{
break ;
}
} while (i >= 0);
j++;
} while (j <= k);
}
}
static void printArray( int []arr, int n)
{
for ( int i = 0; i < n; i++)
{
Console.Write(arr[i] + " " );
}
}
public static void Main(String[] args)
{
int []arr = {0, -2, 8, 5, 1};
int n = arr.Length;
sort_shell_metzner(arr, n);
printArray(arr, n);
}
}
|
Javascript
<script>
function swap(arr,i,j)
{
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
function sort_shell_metzner(arr,n)
{
let i, j, k, l, m, temp;
m = n;
while (m > 0)
{
m = Math.floor(m/2);
k = n - m;
j = 0;
do
{
i = j;
do
{
l = i + m;
if (l < n && arr[i] > arr[l])
{
swap(arr, i, l);
i -= m;
}
else
{
break ;
}
} while (i >= 0);
j++;
} while (j <= k);
}
}
function printArray(arr,n)
{
for (let i = 0; i < n; i++)
{
document.write(arr[i] + " " );
}
}
let arr=[0, -2, 8, 5, 1];
let n = arr.length;
sort_shell_metzner(arr, n);
printArray(arr, n);
</script>
|
PHP
<?php
function sort_shell_metzner( $arr , $n )
{
$m = $n ;
while ( $m > 0)
{
$m = $m / 2;
$k = $n - $m ;
$j = 0;
do {
$i = $j ;
do {
$l = $i + $m ;
if ( $arr [ $i ] > $arr [ $l ])
{
$temp = $arr [ $i ];
$arr [ $i ] = $arr [ $l ];
$arr [ $l ] = $temp ;
$i -= $m ;
}
else
break ;
} while ( $i >= 0);
$j ++;
} while ( $j <= $k );
}
return $arr ;
}
function printArray( $arr , $n )
{
for ( $i = 0; $i < $n ; $i ++)
echo $arr [ $i ], " " ;
}
$arr = array ( 0, -2, 8, 5, 1 );
$n = count ( $arr );
$result_array = sort_shell_metzner( $arr , $n );
printArray( $result_array , $n );
?>
|
Time Complexity: O(n2)
Space Complexity: O(1) as no extra space has been used.
Another Approach: Using a loop to repeatedly reduce the gap value until it reaches 1.
- For each gap value, we perform an insertion sort on subarrays of the original array, using the gap value as the increment for the inner loop.
- In this implementation, we use five indices (‘i’,’j’, ‘k’, ‘l’, and ‘m’) to compare and swap elements within the subarray.
- ‘i’ is the current index being inserted, and ‘j’,’ k’, ‘l’, and ‘m’ are the indices that are ‘gap’ positions behind ‘i’.
- The while loop compares arr[i] to arr[j] and swaps them if necessary.
- Then, we update the indices to move ‘i and the other indices back by gap, and repeat the process until ‘j’ is out of bounds or arr[i] is in the correct position.
Below is the implementation:
C++
#include <iostream>
#include <vector>
using namespace std;
vector< int > shell_sort(vector< int >& arr) {
int n = arr.size();
int gap = n / 2;
while (gap > 0) {
for ( int i = gap; i < n; i++) {
int j = i - gap;
int k = j - gap;
int l = k - gap;
int m = l - gap;
while (j >= 0 && arr[i] < arr[j]) {
swap(arr[i], arr[j]);
i = j;
j = k;
k = l;
l = m;
m = (m - gap >= 0) ? m - gap : -1;
}
}
gap /= 2;
}
return arr;
}
int main() {
int arr[] = { 0, -2, 8, 5, 1 };
vector< int > vec(arr, arr + sizeof (arr) / sizeof ( int ));
vector< int > sorted_arr = shell_sort(vec);
for ( int x : sorted_arr) {
cout << x << " " ;
}
cout << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class ShellSort {
public static int [] shellSort( int [] arr)
{
int n = arr.length;
int gap = n / 2 ;
while (gap > 0 ) {
for ( int i = gap; i < n; i++) {
int j = i - gap;
int k = j - gap;
int l = k - gap;
int m = l - gap;
while (j >= 0 && arr[i] < arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i = j;
j = k;
k = l;
l = m;
m = (m - gap >= 0 ) ? m - gap : - 1 ;
}
}
gap /= 2 ;
}
return arr;
}
public static void main(String[] args)
{
int [] arr = { 0 , - 2 , 8 , 5 , 1 };
int [] sortedArr = shellSort(arr);
System.out.println( "Sorted Array:" );
for ( int x : sortedArr) {
System.out.print(x + " " );
}
System.out.println();
}
}
|
Python
def shell_sort(arr):
n = len (arr)
gap = n / / 2
while gap > 0 :
for i in range (gap, n):
j = i
while j > = gap and arr[j - gap] > arr[j]:
arr[j], arr[j - gap] = arr[j - gap], arr[j]
j - = gap
gap / / = 2
return arr
if __name__ = = "__main__" :
arr = [ 0 , - 2 , 8 , 5 , 1 ]
sorted_arr = shell_sort(arr)
for x in sorted_arr:
print (x),
print ()
|
C#
using System;
using System.Collections.Generic;
public class Program
{
public static List< int > ShellSort(List< int > arr)
{
int n = arr.Count;
int gap = n / 2;
while (gap > 0)
{
for ( int i = gap; i < n; i++)
{
int j = i - gap;
int k = j - gap;
int l = k - gap;
int m = l - gap;
while (j >= 0 && arr[i] < arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i = j;
j = k;
k = l;
l = m;
m = (m - gap >= 0) ? m - gap : -1;
}
}
gap /= 2;
}
return arr;
}
public static void Main( string [] args)
{
int [] arr = { 0, -2, 8, 5, 1 };
List< int > vec = new List< int >(arr);
List< int > sorted_arr = ShellSort(vec);
foreach ( int x in sorted_arr)
{
Console.Write(x + " " );
}
Console.WriteLine();
}
}
|
Javascript
function shellSort(arr) {
const n = arr.length;
let gap = Math.floor(n / 2);
while (gap > 0) {
for (let i = gap; i < n; i++) {
let j = i - gap;
let k = j - gap;
let l = k - gap;
let m = l - gap;
while (j >= 0 && arr[i] < arr[j]) {
[arr[i], arr[j]] = [arr[j], arr[i]];
i = j;
j = k;
k = l;
l = m;
m = (m - gap >= 0) ? m - gap : -1;
}
}
gap = Math.floor(gap / 2);
}
return arr;
}
function main() {
const arr = [0, -2, 8, 5, 1];
const sortedArr = shellSort(arr);
console.log(sortedArr.join( ' ' ));
}
main();
|
Time Complexity: O(n2)
Auxiliary Space: O(1)
Last Updated :
31 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...