Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions CSharp/BubbleSort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;

namespace BubbleSort
{
class Program
{

public static void bubbleSort(int[] x)
{
for (int pass = 1; pass < x.Length - 1; pass++)
{
// Count how many times this next loop
// becomes shorter and shorter
for (int i = 0; i < x.Length - pass; i++)
{
if (x[i] > x[i + 1])
{
// Exchange elements
int temp = x[i];
x[i] = x[i + 1];
x[i + 1] = temp;
}
}
}
}

static void Main()
{
int[] A = { 4, 26, 3, 44, 78, 777, 123, 435, 54, 43 }; // Random test data
bubbleSort(A);
}
}
}
31 changes: 31 additions & 0 deletions CSharp/InsertionSort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;

namespace InsertionSort
{
class Program
{

public static void InsertionSort(int[] x)
{
int n = x.Length - 1;
int i, j, temp;

for (i = 1; i <= n; ++i)
{
temp = x[i];
for (j = i - 1; j >= 0; --j)
{
if (temp < x[j]) x[j + 1] = x[j];
else break;
}
x[j + 1] = temp;
}
}

static void Main()
{
int[] A = { 4, 26, 3, 44, 78, 777, 123, 435, 54, 43 }; // Random test data
InsertionSort(A);
}
}
}
46 changes: 46 additions & 0 deletions CSharp/QuickSort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;

namespace QuickSort
{
class Program
{

public static void QuickSort(int[] x)
{
qs(x, 0, x.Length - 1);
}

public static void qs(int[] x, int left, int right)
{
int i, j;
int pivot, temp;

i = left;
j = right;
pivot = x[(left + right) / 2];

do
{
while ((x[i] < pivot) && (i < right)) i++;
while ((pivot < x[j]) && (j > left)) j--;

if (i <= j)
{
temp = x[i];
x[i] = x[j];
x[j] = temp;
i++; j--;
}
} while (i <= j);

if (left < j) qs(x, left, j);
if (i < right) qs(x, i, right);
}

static void Main()
{
int[] A = { 4, 26, 3, 44, 78, 777, 123, 435, 54, 43 }; // Random test data
QuickSort(A);
}
}
}
35 changes: 35 additions & 0 deletions CSharp/SelectionSort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;

namespace SelectionSort
{
class Program
{

public static void SelectionSort(int[] x)
{
int i, j;
int min, temp;

for (i = 0; i < x.Length - 1; i++)
{
min = i;
for (j = i + 1; j < x.Length; j++)
{
if (x[j] < x[min])
{
min = j;
}
}
temp = x[i];
x[i] = x[min];
x[min] = temp;
}
}

static void Main()
{
int[] A = { 4, 26, 3, 44, 78, 777, 123, 435, 54, 43 }; // Random test data
SelectionSort(A);
}
}
}