-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Shell_Sort.cs Update Shell_Sort.cs
- Loading branch information
1 parent
b595add
commit 3ff9f74
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// C# program implementation of the ShellSort algorithm | ||
using System; | ||
|
||
class ShellSort | ||
{ | ||
//function to implement ShellSort | ||
void shellsort(int []array, int size) | ||
{ | ||
// Beginning with a big gap, and then keep on reducing it. | ||
for (int g = size/2; g >= 1; g /= 2) | ||
{ | ||
// Performing an insertion sort | ||
for (int i = g; i < size; i += 1) | ||
{ | ||
int j, temp = array[i]; | ||
|
||
|
||
//Shift other elements until the correct position of a[i] is found | ||
for (j = i; array[j - g] > temp && j >= g ; j -= g) | ||
array[j] = array[j - g]; | ||
|
||
// put the element a[i] in its correct position | ||
array[j] = temp; | ||
} | ||
} | ||
} | ||
|
||
public static void Main() | ||
{ | ||
|
||
int size; | ||
Console.Write("How many numbers do you want to sort : "); | ||
size = Convert.ToInt32(Console.ReadLine()); | ||
int[] array = new int[size]; | ||
Console.Write("\nEnter the numbers : "); | ||
for (int i = 0; i < size; i++) | ||
array[i] = Convert.ToInt32(Console.ReadLine()); | ||
ShellSort ob = new ShellSort(); | ||
ob.shellsort(array, size); | ||
Console.Write("\nAfter sorting : "); | ||
for (int i = 0; i < size; i++) | ||
Console.Write(array[i] + " "); | ||
} | ||
} | ||
|
||
//SAMPLE INPUT AND OUTPUT | ||
/*SAMPLE 1 | ||
How many numbers do you want to sort : 7 | ||
Enter the numbers : | ||
4 | ||
9 | ||
3 | ||
9 | ||
4 | ||
7 | ||
5 | ||
After sorting : 3 4 4 5 7 9 9 | ||
//SAMPLE 2 | ||
How many numbers do you want to sort : 5 | ||
Enter the numbers : | ||
5 | ||
1 | ||
7 | ||
3 | ||
2 | ||
After sorting : 1 2 3 5 7 | ||
*/ |