How do shell sort algorithms work

Sorting algorithms

Principle:

The algorithm compare the series to two neighboring elements and
swaps themif they are in the wrong order are present.
This process will as long as repeated until no more exchanges are necessary. This usually requires several runs.

Depending on whether it is sorted in ascending or descending order, the larger or smaller elements rise like Blow in the water (hence the name) further and further up, that is, to the end of the row.

The BubbleSort algorithm is particularly suitable for presorted data series.

Example:

The numbers 5, 3, 6, 1 and 4 should be sorted in ascending order.

1st run
5 3 6 1 4 exchange
3 5 6 1 4 no exchange
3 5 6 1 4 exchange
3 5 1 6 4 exchange
2nd run
3 5 1 4 6 no exchange
3 5 1 4 6 exchange
3 1 5 4 6 exchange
3rd pass
3 1 4 5 6 exchange
1 3 4 5 6 no exchange
4th pass
1 3 4 5 6 no exchange
The array is now sorted
1 3 4 5 6

Structogram / syntax:

 

n = number;
do
{
swapped = false;
for (i = 0; i i++)
  {
if (number [i + 1]     {
//-----Exchange-----
h = number [i];
number [i] = number [i + 1];
number [i + 1] = h;
swapped = true;
    }
  }
n--;
}
while (swapped == true);

Example: Sorting 50 numbers in ascending order in the number field [50]

 

n = 50;
do
{
swapped = false;
for (i = 0; i i++)
  {
if (number [i + 1]     {
//-----Exchange-----
h = number [i];
number [i] = number [i + 1];
number [i + 1] = h;
swapped = true;
    }
  }
n--;
}
while (swapped == true);