diff --git a/CSharp/BubbleSort.cs b/CSharp/BubbleSort.cs new file mode 100644 index 0000000..b2197d1 --- /dev/null +++ b/CSharp/BubbleSort.cs @@ -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); + } + } +} diff --git a/CSharp/InsertionSort.cs b/CSharp/InsertionSort.cs new file mode 100644 index 0000000..b750b8e --- /dev/null +++ b/CSharp/InsertionSort.cs @@ -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); + } + } +} diff --git a/CSharp/QuickSort.cs b/CSharp/QuickSort.cs new file mode 100644 index 0000000..f5d76f5 --- /dev/null +++ b/CSharp/QuickSort.cs @@ -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); + } + } +} diff --git a/CSharp/SelectionSort.cs b/CSharp/SelectionSort.cs new file mode 100644 index 0000000..dee8a9e --- /dev/null +++ b/CSharp/SelectionSort.cs @@ -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); + } + } +}