Skip to content

Commit e740f50

Browse files
authored
Merge branch 'master' into edit-readme
2 parents 174c79f + 7996090 commit e740f50

17 files changed

+857
-0
lines changed

C#/BinaryInsertionSort.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// C# Program implementing
2+
// binary insertion sort
3+
using System;
4+
5+
class GFG {
6+
7+
public static void Main()
8+
{
9+
int []arr = {37, 23, 0, 17, 12, 72, 31,
10+
46, 100, 88, 54 };
11+
12+
sort(arr);
13+
14+
for(int i = 0; i < arr.Length; i++)
15+
Console.Write(arr[i] + " ");
16+
}
17+
18+
public static void sort(int []array)
19+
{
20+
for (int i = 1; i < array.Length; i++)
21+
{
22+
int x = array[i];
23+
24+
// Find location to insert using
25+
// binary search
26+
int j = Math.Abs(Array.BinarySearch(
27+
array, 0, i, x) + 1);
28+
29+
// Shifting array to one location right
30+
System.Array.Copy(array, j, array,
31+
j+1, i-j);
32+
33+
// Placing element at its correct
34+
// location
35+
array[j] = x;
36+
}
37+
}
38+
}

C#/HeapSort.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
class heapsort
3+
{
4+
int[] r = { 2,5,1,10,6,9,3,7,4,8};
5+
public void hsort()
6+
{
7+
int i, t;
8+
for (i = 5; i >= 0; i--)
9+
{
10+
adjust(i, 9);
11+
}
12+
for (i = 8; i >= 0; i--)
13+
{
14+
t = r[i + 1];
15+
r[i + 1] = r[0];
16+
r[0] = t;
17+
adjust(0, i);
18+
}
19+
}
20+
private void adjust(int i, int n)
21+
{
22+
int t, j;
23+
try
24+
{
25+
t = r[i];
26+
j = 2 * i;
27+
while (j <= n)
28+
{
29+
if (j < n && r[j] < r[j + 1])
30+
j++;
31+
if (t >=r[j])
32+
break;
33+
r[j / 2] = r[j];
34+
j *= 2;
35+
}
36+
r[j / 2] = t;
37+
}
38+
catch (IndexOutOfRangeException e)
39+
{
40+
Console.WriteLine("Array Out of Bounds ", e);
41+
}
42+
}
43+
public void print()
44+
{
45+
for (int i = 0; i < 10; i++)
46+
{
47+
Console.WriteLine("{0}", r[i]);
48+
}
49+
50+
}
51+
public static void Main()
52+
{
53+
heap obj = new heap();
54+
Console.WriteLine("Elements Before sorting : ");
55+
obj.print();
56+
obj.hsort();
57+
Console.WriteLine("Elements After sorting : ");
58+
obj.print();
59+
Console.Read();
60+
}
61+
}

C#/InsertionSort.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* C# Program to Perform Insertion Sort
3+
*/
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.IO;
9+
namespace ConsoleApplication1
10+
{
11+
class Program
12+
{
13+
static void Main(string[] args)
14+
{
15+
int[] arr = new int[5] { 83, 12, 3, 34, 60 };
16+
int i;
17+
Console.WriteLine("The Array is :");
18+
for (i = 0; i < 5; i++)
19+
{
20+
Console.WriteLine(arr[i]);
21+
}
22+
insertsort(arr, 5);
23+
Console.WriteLine("The Sorted Array is :");
24+
for (i = 0; i < 5; i++)
25+
Console.WriteLine(arr[i]);
26+
Console.ReadLine();
27+
}
28+
static void insertsort(int[] data, int n)
29+
{
30+
int i, j;
31+
for (i = 1; i < n; i++)
32+
{
33+
int item = data[i];
34+
int ins = 0;
35+
for (j = i - 1; j >= 0 && ins != 1; )
36+
{
37+
if (item < data[j])
38+
{
39+
data[j + 1] = data[j];
40+
j--;
41+
data[j + 1] = item;
42+
}
43+
else ins = 1;
44+
}
45+
}
46+
}
47+
}
48+
}

C#/MergeSort.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace CSharpMergeSort
6+
{
7+
class Mergesort
8+
{
9+
10+
static public void DoMerge(int [] numbers, int left, int mid, int right)
11+
{
12+
int [] temp = new int[25];
13+
int i, left_end, num_elements, tmp_pos;
14+
15+
left_end = (mid - 1);
16+
tmp_pos = left;
17+
num_elements = (right - left + 1);
18+
19+
while ((left <= left_end) && (mid <= right))
20+
{
21+
if (numbers[left] <= numbers[mid])
22+
temp[tmp_pos++] = numbers[left++];
23+
else
24+
temp[tmp_pos++] = numbers[mid++];
25+
}
26+
27+
while (left <= left_end)
28+
temp[tmp_pos++] = numbers[left++];
29+
30+
while (mid <= right)
31+
temp[tmp_pos++] = numbers[mid++];
32+
33+
for (i = 0; i < num_elements; i++)
34+
{
35+
numbers[right] = temp[right];
36+
right--;
37+
}
38+
}
39+
40+
static public void MergeSort_Recursive(int [] numbers, int left, int right)
41+
{
42+
int mid;
43+
44+
if (right > left)
45+
{
46+
mid = (right + left) / 2;
47+
MergeSort_Recursive(numbers, left, mid);
48+
MergeSort_Recursive(numbers, (mid + 1), right);
49+
50+
DoMerge(numbers, left, (mid+1), right);
51+
}
52+
}
53+
54+
static void Main(string[] args)
55+
{
56+
int[] numbers = { 3, 8, 7, 5, 2, 1, 9, 6, 4 };
57+
int len = 9;
58+
59+
Console.WriteLine("MergeSort By Recursive Method");
60+
MergeSort_Recursive(numbers, 0, len - 1);
61+
for (int i = 0; i < 9; i++)
62+
Console.WriteLine(numbers[i]);
63+
64+
Console.WriteLine(numbers[i]);
65+
66+
}
67+
}
68+
}

C#/QuickSort.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace sortQuickAlgorithm
7+
{
8+
class quickSortAlgorithm
9+
{
10+
11+
private int[] array = new int[20];
12+
private int len;
13+
14+
public void QuickSortAlgorithm()
15+
{
16+
sort(0, len - 1);
17+
}
18+
19+
public void sort(int left, int right)
20+
{
21+
int pivot, leftend, rightend;
22+
23+
leftend = left;
24+
rightend = right;
25+
pivot = array[left];
26+
27+
while (left < right) { while ((array[right] >= pivot) &amp;&amp; (left < right))
28+
{
29+
right--;
30+
}
31+
32+
if (left != right)
33+
{
34+
array[left] = array[right];
35+
left++;
36+
}
37+
38+
while ((array[left] >= pivot) &amp;&amp; (left < right))
39+
{
40+
left++;
41+
}
42+
43+
if (left != right)
44+
{
45+
array[right] = array[left];
46+
right--;
47+
}
48+
}
49+
50+
array[left] = pivot;
51+
pivot = left;
52+
left = leftend;
53+
right = rightend;
54+
55+
if (left < pivot) { sort(left, pivot - 1); } if (right > pivot)
56+
{
57+
sort(pivot + 1, right);
58+
}
59+
}
60+
61+
public static void Main()
62+
{
63+
quickSortAlgorithm q_Sort = new quickSortAlgorithm();
64+
65+
int[] array = { 41, 32, 15, 45, 63, 72, 57, 43, 32, 52, 183};
66+
q_Sort.array = array;
67+
q_Sort.len = q_Sort.array.Length;
68+
q_Sort.QuickSortAlgorithm();
69+
70+
for (int j = 0; j &lt; q_Sort.len; j++)
71+
{
72+
Console.WriteLine(q_Sort.array[j]);
73+
}
74+
Console.ReadKey();
75+
}
76+
}
77+
}

C#/SelectionSort.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* C# Program to Perform a Selection Sort
3+
*/
4+
using System;
5+
class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
int array_size = 10;
10+
int[] array = new int[10] { 100, 50, 20, 40, 10, 60, 80, 70, 90, 30 };
11+
Console.WriteLine("The Array Before Selection Sort is: ");
12+
for (int i = 0; i &lt; array_size; i++)
13+
{
14+
Console.WriteLine(array[i]);
15+
}
16+
int tmp, min_key;
17+
18+
for (int j = 0; j < array_size - 1; j++)
19+
{
20+
min_key = j;
21+
22+
for (int k = j + 1; k < array_size; k++)
23+
{
24+
if (array[k] < array[min_key])
25+
{
26+
min_key = k;
27+
}
28+
}
29+
30+
tmp = array[min_key];
31+
array[min_key] = array[j];
32+
array[j] = tmp;
33+
}
34+
35+
Console.WriteLine("The Array After Selection Sort is: ");
36+
for (int i = 0; i < 10; i++)
37+
{
38+
Console.WriteLine(array[i]);
39+
}
40+
Console.ReadLine();
41+
}
42+
}

C#/ShellSort.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
private void SortArrayWithShellSort()
2+
{
3+
int[] array = { 297,183, 464 };
4+
ShellSort(array);
5+
}
6+
7+
8+
private void ShellSort(int[] array)
9+
{
10+
int n = array.Length;
11+
int gap = n / 2;
12+
int temp;
13+
14+
while (gap > 0)
15+
{
16+
for (int i = 0; i + gap < n; i++)
17+
{
18+
int j = i + gap;
19+
temp = array[j];
20+
21+
while (j - gap >= 0 && temp < array[j - gap])
22+
{
23+
array[j] = array[j - gap];
24+
j = j - gap;
25+
}
26+
27+
array[j] = temp;
28+
}
29+
30+
gap = gap / 2;
31+
}
32+
}

0 commit comments

Comments
 (0)