Shell-Metzner Sort

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of Shell-Metzner Sort
#include <bits/stdc++.h>
using namespace std;
  
// Function to swap two elements
void swap(int& a, int& b)
{
    int temp = a;
    a = b;
    b = temp;
}
  
// Function to sort arr[] using Shell Metzner sort
void sort_shell_metzner(int arr[], int n)
{
  
    // Declare variables
    int i, j, k, l, m, temp;
  
    // Set initial step size to
    // the size of the array
    m = n;
  
    while (m > 0) {
  
        // Step size decreases by half each time
        m /= 2;
  
        // k is the upper limit for j
        k = n - m;
  
        // j is the starting point
        j = 0;
  
        do {
  
            // i equals to smaller value
            i = j;
  
            do {
  
                // l equals to larger value
                l = i + m;
  
                // Compare and swap arr[i] with arr[l]
                if (arr[i] > arr[l]) {
                    swap(arr[i], arr[l]);
  
                    // Decrease smaller value by step size
                    i -= m;
                }
                else
                    break;
            } while (i >= 0);
  
            // Increment the lower limit of i
            j++;
  
        } while (j <= k);
    }
}
  
// Function to print the contents of an array
void printArray(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
  
// Driver code
int main()
{
    int arr[] = { 0, -2, 8, 5, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Sort the array using Shell Metzner Sort
    sort_shell_metzner(arr, n);
  
    // Print the sorted array
    printArray(arr, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of Shell-Metzner Sort 
class GFG 
{
  
    // Function to swap two elements 
    static int[] swap(int[] arr, int i, int j)
    {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
        return arr;
    }
      
    // Function to sort arr[] using Shell Metzner sort 
    static void sort_shell_metzner(int arr[], int n) 
    {
  
        // Declare variables 
        int i, j, k, l, m, temp;
  
        // Set initial step size to 
        // the size of the array 
        m = n;
  
        while (m > 0)
        {
  
            // Step size decreases by half each time 
            m /= 2;
  
            // k is the upper limit for j 
            k = n - m;
  
            // j is the starting point 
            j = 0;
  
            do
            {
  
                // i equals to smaller value 
                i = j;
  
                do
                {
  
                    // l equals to larger value 
                    l = i + m;
  
                    // Compare and swap arr[i] with arr[l] 
                    if (l < n && arr[i] > arr[l])
                    {
                        swap(arr, i, l);
  
                        // Decrease smaller value by step size 
                        i -= m;
                    }
                    else
                    {
                        break;
                    }
                } while (i >= 0);
  
                // Increment the lower limit of i 
                j++;
  
            } while (j <= k);
        }
    }
  
    // Function to print the contents of an array 
    static void printArray(int arr[], int n) 
    {
        for (int i = 0; i < n; i++)
        {
            System.out.print(arr[i] + " ");
        }
    }
  
    // Driver code 
    public static void main(String[] args)
    {
        int arr[] = {0, -2, 8, 5, 1};
        int n = arr.length;
  
        // Sort the array using Shell Metzner Sort 
        sort_shell_metzner(arr, n);
  
        // Print the sorted array 
        printArray(arr, n);
    }
}
  
/* This code contributed by PrinciRaj1992 */

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of Shell-Metzner Sort
using System; 
  
class GFG 
{
  
    // Function to swap two elements 
    static int[] swap(int[] arr, int i, int j)
    {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
        return arr;
    }
      
    // Function to sort arr[] using Shell Metzner sort 
    static void sort_shell_metzner(int []arr, int n) 
    {
  
        // Declare variables 
        int i, j, k, l, m, temp;
  
        // Set initial step size to 
        // the size of the array 
        m = n;
  
        while (m > 0)
        {
  
            // Step size decreases by half each time 
            m /= 2;
  
            // k is the upper limit for j 
            k = n - m;
  
            // j is the starting point 
            j = 0;
  
            do
            {
  
                // i equals to smaller value 
                i = j;
  
                do
                {
  
                    // l equals to larger value 
                    l = i + m;
  
                    // Compare and swap arr[i] with arr[l] 
                    if (l < n && arr[i] > arr[l])
                    {
                        swap(arr, i, l);
  
                        // Decrease smaller value by step size 
                        i -= m;
                    }
                    else
                    {
                        break;
                    }
                } while (i >= 0);
  
                // Increment the lower limit of i 
                j++;
  
            } while (j <= k);
        }
    }
  
    // Function to print the contents of an array 
    static void printArray(int []arr, int n) 
    {
        for (int i = 0; i < n; i++)
        {
            Console.Write(arr[i] + " ");
        }
    }
  
    // Driver code 
    public static void Main(String[] args)
    {
        int []arr = {0, -2, 8, 5, 1};
        int n = arr.Length;
  
        // Sort the array using Shell Metzner Sort 
        sort_shell_metzner(arr, n);
  
        // Print the sorted array 
        printArray(arr, n);
    }
}
  
// This code has been contributed by 29AjayKumar

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// php implementation of Shell-Metzner Sort 
  
// Function to sort arr[] using Shell Metzner sort 
function sort_shell_metzner($arr, $n
    // Set initial step size to 
    // the size of the array 
    $m = $n
  
    while ($m > 0) 
    
  
        // Step size decreases by half each time 
        $m = $m / 2; 
  
        // k is the upper limit for j 
        $k = $n - $m
  
        // j is the starting point 
        $j = 0; 
  
        do
  
            // i equals to smaller value 
            $i = $j
  
            do
  
                // l equals to larger value 
                $l = $i + $m
  
                // Compare and swap arr[i] with arr[l] 
                if ($arr[$i] > $arr[$l]) 
                
                    $temp = $arr[$i]; 
                    $arr[$i] = $arr[$l]; 
                    $arr[$l] = $temp
                  
                    // Decrease smaller value by step size 
                    $i -= $m
                
                else
                    break
                      
            } while ($i >= 0); 
  
            // Increment the lower limit of i 
            $j++; 
  
        } while ($j <= $k); 
    
    return $arr ;
  
// Function to print the contents of an array 
function printArray($arr, $n
    for ($i = 0; $i < $n; $i++) 
        echo $arr[$i], " "
  
// Driver code 
$arr = array( 0, -2, 8, 5, 1 ); 
$n = count($arr); 
  
// Sort the array using Shell Metzner Sort 
$result_array = sort_shell_metzner($arr, $n); 
  
// Print the sorted array 
printArray($result_array, $n); 
  
// This code is contributed by Ryuga
?>

chevron_right


Output:

-2 0 1 5 8

Time Complexity: O(n2)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.