0% found this document useful (0 votes)
5 views3 pages

Shell 2

This C# program implements the Shell Sort algorithm to sort an array of integers. It includes methods to read an array from user input and print the sorted array. The sorting process uses a gap-based insertion sort, reducing the gap until it reaches zero.

Uploaded by

riawanston80
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Shell 2

This C# program implements the Shell Sort algorithm to sort an array of integers. It includes methods to read an array from user input and print the sorted array. The sorting process uses a gap-based insertion sort, reducing the gap until it reaches zero.

Uploaded by

riawanston80
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace shell_sort

class Program

static void Main(string[] args)

int[] array;

array = ReadArray();

/* function to sort arr using shellSort */

int n = array.Length;

// Start with a big gap,

// then reduce the gap

int gap =(n/2);

while (gap >0)

{
// Do a gapped insertion sort for this gap size.

for (int i = gap; i < n; i++)

{int temp = array[i];

int j= i ;

while ( j >= gap && array[j - gap] > temp)

array[j] = array[j - gap];

j-= gap;

array[j] = temp;

gap = (gap / 2);

Console.WriteLine("The Sorted Array is:"); PrintArray(array);

static int[] ReadArray() { Console.WriteLine("Enter array length:");

int n = int.Parse(Console.ReadLine());

int[] arr = new int[n];

Console.WriteLine("Enter array data:");

for (int i = 0; i < n; i++)

arr[i] = int.Parse(Console.ReadLine());

return arr;
}

static void PrintArray(int[] arr)

{ for (int i = 0; i < arr.Length; i++)

Console.WriteLine(arr[i]);

Console.ReadKey();

You might also like