C#: Shell sort
C# Sharp Searching and Sorting Algorithm: Exercise-1 with Solution
Write a C# Sharp program to sort a list of elements using Shell sort.
Note : According to Wikipedia "Shell sort or Shell's method, is an in-place comparison sort. It can be seen as either a generalization of sorting by exchange (bubble sort) or sorting by insertion (insertion sort). The method starts by sorting pairs of elements far apart from each other, then progressively reducing the gap between elements to be compared. Starting with far apart elements can move some out-of-place elements into position faster than a simple nearest neighbour exchange."
An example run of Shellsort with gaps 5, 3 and 1 is shown below.
The first pass, 5-sorting, performs insertion sort on separate subarrays (a1, a6, a11), (a2, a7, a12), (a3, a8), (a4, a9), (a5, a10). For instance, it changes the subarray (a1, a6, a11) from (62, 17, 25) to (17, 25, 62). The next pass, 3-sorting, performs insertion sort on the subarrays (a1, a4, a7, a10), (a2, a5, a8, a11), (a3, a6, a9, a12). The last pass, 1-sorting, is an ordinary insertion sort of the entire array (a1,..., a12).
As the example illustrates, the subarrays that Shellsort operates on are initially short; later they are longer but almost ordered. In both cases insertion sort works efficiently.
Shellsort is unstable: it may change the relative order of elements with equal values. It is an adaptive sorting algorithm in that it executes faster when the input is partially sorted.
Shell Sort
Shellsort with gaps 23, 10, 4, 1 in action.
Sample Solution:-
C# Sharp Code:
using System;
using System.Collections;
namespace Shell_Sort
{
public class SortShell
{
// Main method
static void Main(string[] args)
{
int[] arr = new int[] { 5, -4, 11, 0, 18, 22, 67, 51, 6 }; // Initializing an array with integers
int n;
n = arr.Length; // Determining the length of the array
Console.WriteLine("Original Array Elements :");
show_array_elements(arr); // Displaying original array elements
shellSort(arr, n); // Calling the shell sort function
Console.WriteLine("\nSorted Array Elements :");
show_array_elements(arr); // Displaying sorted array elements
}
// Method to perform shell sort
static void shellSort(int[] arr, int array_size)
{
int i, j, inc, temp;
inc = 3; // Initial increment value for shell sort
while (inc > 0)
{
for (i = 0; i < array_size; i++)
{
j = i;
temp = arr[i];
while ((j >= inc) && (arr[j - inc] > temp))
{
arr[j] = arr[j - inc];
j = j - inc;
}
arr[j] = temp;
}
// Updating the increment value
if (inc / 2 != 0)
inc = inc / 2;
else if (inc == 1)
inc = 0;
else
inc = 1;
}
}
// Method to display array elements
static void show_array_elements(int[] arr)
{
foreach (var element in arr)
{
Console.Write(element + " "); // Displaying each element of the array
}
Console.Write("\n"); // Moving to the next line after displaying all elements
}
}
}
Sample Output:
Original Array Elements : 5 -4 11 0 18 22 67 51 6 Sorted Array Elements : -4 0 5 6 11 18 22 51 67
Flowchart:
C# Sharp Code Editor:
Contribute your code and comments through Disqus.
Previous: C# Sharp Searching and Sorting Algorithm Exercises
Next: Write a C# Sharp program to sort a list of elements using Bogosort sort.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/csharp-exercises/searching-and-sorting-algorithm/searching-and-sorting-algorithm-exercise-1.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics