×

C++ Program to Implement Shell Sort

    C++ Program to Implement Shell Sort

shell sort is basically an Insertion Sort variant. In the insertion sort, we only transfer elements ahead of one location. Many movements are involved when an item has to be pushed far ahead. The idea behind shell sort is to allow the exchange of far-flung items. In shell sort, for a great value of h, we make the array h-sorted. We hold the value of h decreasing until it is 1. If all sublists of any h'th element are sorted, an array is said to be h-sorted.

The complexity of the Shell Sort Technique

  • Time Complexity: O(n log n) for the best-case scenario, and relies heavily on the distance series also in other cases.
  • Space Complexity: O(1)

Input:- The unsorted list: 25, 4, 87, 94, 11, 30, 210, 200, 51, 99, 10

Output:- Array after Sorting: 4, 10, 11, 25, 30, 51, 87, 94, 99, 200, 210

Algorithm

ShellSort(array, size)

Input: A data collection, and the overall number in the array

Output: The sorted Array

shell_sort (A, N)
where A – list to be sorted; N – gap_size
set gap_size = N, flag = 1
while gap_size > 1 or flag = 1, repeat
begin
set flag = 0
set gap_size = (gap_size + 1)/2
end
for i = 0 to i< (N-gap_size) repeat
begin
if A[i + gap_size] > A[i]
swap A[i + gap_size], A[i]
set flag = 0
end
end

Thus, we first set N in the above algorithm, which is the gap to sort the array A using shell sort. In the next step, we divide the whole array into sub-arrays using the distance. Then we sort each of the sub-arrays in the next step so that we get a sorted array at the end of the loop.

Simple Example Code of shell sort

// Program to implementation of Shell Sort in C++
#include <iostream>
using namespace std;
int shellSort(int array[], int p)
{
        for (int gap = p/2; gap > 0; gap /= 2)
    {
        // Do a gapped insertion sort for this gap size.
        // keep adding one more element until the entire array is
        // gap sorted 
        for (int l = gap; l < p; l += 1)
        {
            // add a[l] to the elements that have been gap sorted
            // save a[l] in temp and make a hole at position l
            int temp = array[l];
            // shift earlier gap-sorted elements up until the correct 
            // location for a[l] is found
            int j;             
            for (j = l; j >= gap && array[j - gap] > temp; j -= gap)
                array[j] = array[j - gap];
            //  put temp (the original a[l]) in its correct location
            array[j] = temp;
        }
    }
    return 0;
}
void printArray(int array[], int p)
{
    for (int l=0; l<p; l++)
        cout << array[l] << " ";
}
int main()
{
    int array[] = {70, 5, 3, 16, 25, 77, 110, 40, 21}, l;
    int p = sizeof(array)/sizeof(array[0]);
    cout << "Array before sorting: \n";
    printArray(array, p);
    shellSort(array, p);
    cout << "\nArray after sorting: \n";
    printArray(array, p);
    return 0;
}

Output:

C++ Program to Implement Shell Sort

Related Topics

How to create a button in C++

A button is an option through which a user can control to click a provided input to an application. There are several buttons, each with different styles, to maintain the...

3 minutes read.

goto statement in C and C++

goto statement in C and C++ The goto statement is a jump statement, also sometimes referred to as an unconditional jump statement. Within a function, the goto statement can be used...

3 minutes read.

Reverse a String using Stack C/C++

Reverse the given string using stack. To turn "tutorialandexample" into "elpmaxednalairotut," for instance. Here is a straightforward stack-based technique for reversing strings. Algorithm: 1) Make a stack that is empty. 2) Push each character...

3 minutes read.

System() function in C++

As a part of the c/c+ standard library, the system() function passes commands to be executed by the operating system’s command processor or terminal and returns the completed command. We...

2 minutes read.

Private Inheritance in C++

Private inheritance is an inheritance in object-oriented programming (OOP) languages where a subclass derives from a superclass. Still, the derived class does not inherit the public and protected members of...

7 minutes read.

C++ Queue

C++ queue: Queue in C++ is also a container adapter with the functionality of a queue. Queue is just the opposite of the stack in C++ because stack works on...

4 minutes read.

Structure Vs Class in C++

The structure in C++ is similar to that of a class, with a few exceptions. Both the structure and the stage, the most important thing is safety. The property is...

4 minutes read.

Inheritance Program in C++

What is Inheritance? Inheritance is the ability of a class to inherit traits and properties from another class. One of the most crucial aspects of Object-Oriented Programming is inheritance. The ability or...

9 minutes read.

C++ Recursion Function

A programming method called recursion that uses a function to call itself to address lesser problems. The Fibonacci sequence, factorial computation, and tree traversal are just a few of the...

4 minutes read.

Constructor Overloading

The program contains more than one constructor in a class with the same name, and different types of arguments are called constructor overloading. Calling of constructor depends on the number and types...

2 minutes read.

Principles of Object-Oriented Programming in C++

What is Object-Oriented Programming? Object-oriented programming is about creating obejcts that represent the real-world entity . In object-oriented programming, objects are created for the class. One of the main objectives of...

5 minutes read.

C++ Expressions

C ++ equations are made up of operators, constants and variables arranged according to language rules. It may also include function calls that give results. To calculate the value, the...

4 minutes read.

Dynamic Memory Allocation in C++

In some programming situations, the number of data items changes as the program is running, which is known as dynamic data or input. Consider a real-world situation where a program...

3 minutes read.

Pthreads or POSIX Threads in C++

The thread API for C/C++ is implemented by pthreads or POSIX threads. It enables the multithreading system, which enables parallel and distributed processing, and the creation of new concurrent process...

3 minutes read.

C++ | C Plus Plus Data type

Data type in every language is very important. Data type means the different kinds of data that are supported by a particular programming language. A computer language’s data types specify the...

15 minutes read.

Continue in C++ While loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the...

4 minutes read.

Palindrome using For loop in C++

A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++ also allows...

6 minutes read.

Copy elision in C++

The Copy Omission is another name for the Copy Elision. One of the several compiler optimization techniques is copy elision. It prevents items from being copied inadvertently. This Copy Elision approach...

3 minutes read.

Templates in C++ vs Generics in Java

As the title suggests, there is no rivalry or there is no cut comparison between generics and templates in Java and C++, respectively. The main aim of this article is...

4 minutes read.

gmtime() function in C/C++

C++ language is used to make high-performance applications that can work efficiently. It is one of the world's most popular languages. It is an object-oriented and high-level programming language, which...

4 minutes read.