Skip to content

e. Extras

These C# implementations are drop-in helpers. The key skill is knowing when to call them and how to use their returned results.


1) Drop-in algorithm code (paste once)

SearchAlgorithms.cs

Click to view SearchAlgorithms.cs
using System;
using System.Collections.Generic;

public static class SearchAlgorithms
{
    // Integer versions: work on sorted OR unsorted arrays/lists
    public static bool LinearSearch(int[] data, int target)
    {
        return LinearSearchInternal(data, target);
    }

    public static bool LinearSearch(List<int> data, int target)
    {
        return LinearSearchInternal(data, target);
    }

    // Integer versions: require data sorted ascending
    public static bool BinarySearch(int[] data, int target)
    {
        return BinarySearchInternal(data, target);
    }

    public static bool BinarySearch(List<int> data, int target)
    {
        return BinarySearchInternal(data, target);
    }

    private static bool LinearSearchInternal(IList<int> data, int target)
    {
        for (int i = 0; i < data.Count; i++)
        {
            if (data[i] == target) return true;
        }
        return false;
    }

    private static bool BinarySearchInternal(IList<int> data, int target)
    {
        int low = 0;
        int high = data.Count - 1;

        while (low <= high)
        {
            int mid = (low + high) / 2;

            if (data[mid] == target) return true;
            if (target < data[mid]) high = mid - 1;
            else low = mid + 1;
        }

        return false;
    }

    // Float versions: work on sorted OR unsorted arrays/lists
    public static bool LinearSearch(float[] data, float target, float tolerance = 0.0001f)
    {
        return LinearSearchInternal(data, target, tolerance);
    }

    public static bool LinearSearch(List<float> data, float target, float tolerance = 0.0001f)
    {
        return LinearSearchInternal(data, target, tolerance);
    }

    // Float versions: require data sorted ascending
    public static bool BinarySearch(float[] data, float target, float tolerance = 0.0001f)
    {
        return BinarySearchInternal(data, target, tolerance);
    }

    public static bool BinarySearch(List<float> data, float target, float tolerance = 0.0001f)
    {
        return BinarySearchInternal(data, target, tolerance);
    }

    private static bool LinearSearchInternal(IList<float> data, float target, float tolerance)
    {
        for (int i = 0; i < data.Count; i++)
        {
            if (Math.Abs(data[i] - target) < tolerance) return true;
        }
        return false;
    }

    private static bool BinarySearchInternal(IList<float> data, float target, float tolerance)
    {
        int low = 0;
        int high = data.Count - 1;

        while (low <= high)
        {
            int mid = (low + high) / 2;

            if (Math.Abs(data[mid] - target) < tolerance) return true;
            if (target < data[mid]) high = mid - 1;
            else low = mid + 1;
        }

        return false;
    }

    // String versions: work on sorted OR unsorted arrays/lists
    public static bool LinearSearch(string[] data, string target)
    {
        return LinearSearchInternal(data, target);
    }

    public static bool LinearSearch(List<string> data, string target)
    {
        return LinearSearchInternal(data, target);
    }

    // String versions: require data sorted alphabetically
    public static bool BinarySearch(string[] data, string target)
    {
        return BinarySearchInternal(data, target);
    }

    public static bool BinarySearch(List<string> data, string target)
    {
        return BinarySearchInternal(data, target);
    }

    private static bool LinearSearchInternal(IList<string> data, string target)
    {
        for (int i = 0; i < data.Count; i++)
        {
            if (string.Equals(data[i], target, StringComparison.OrdinalIgnoreCase)) return true;
        }
        return false;
    }

    private static bool BinarySearchInternal(IList<string> data, string target)
    {
        int low = 0;
        int high = data.Count - 1;

        while (low <= high)
        {
            int mid = (low + high) / 2;
            int comparison = string.Compare(data[mid], target, StringComparison.OrdinalIgnoreCase);

            if (comparison == 0) return true;
            if (comparison > 0) high = mid - 1;
            else low = mid + 1;
        }

        return false;
    }
}

SortAlgorithms.cs

Click to view SortAlgorithms.cs
using System;
using System.Collections.Generic;

public static class SortAlgorithms
{
    // Integer versions: sort the array/list IN PLACE
    public static void SelectionSort(int[] data)
    {
        SelectionSortInternal(data);
    }

    public static void SelectionSort(List<int> data)
    {
        SelectionSortInternal(data);
    }

    // Integer versions: public wrapper (students call this)
    public static void QuickSort(int[] data)
    {
        QuickSortInternal(data);
    }

    public static void QuickSort(List<int> data)
    {
        QuickSortInternal(data);
    }

    private static void SelectionSortInternal(IList<int> data)
    {
        for (int i = 0; i < data.Count - 1; i++)
        {
            int smallestIndex = i;

            for (int j = i + 1; j < data.Count; j++)
            {
                if (data[j] < data[smallestIndex])
                    smallestIndex = j;
            }

            Swap(data, i, smallestIndex);
        }
    }

    private static void QuickSortInternal(IList<int> data)
    {
        QuickSortRecursive(data, 0, data.Count - 1);
    }

    private static void QuickSortRecursive(IList<int> data, int low, int high)
    {
        if (low < high)
        {
            int pivotIndex = Partition(data, low, high);
            QuickSortRecursive(data, low, pivotIndex - 1);
            QuickSortRecursive(data, pivotIndex + 1, high);
        }
    }

    private static int Partition(IList<int> data, int low, int high)
    {
        int pivot = data[high];
        int i = low - 1;

        for (int j = low; j < high; j++)
        {
            if (data[j] < pivot)
            {
                i++;
                Swap(data, i, j);
            }
        }

        Swap(data, i + 1, high);
        return i + 1;
    }

    private static void Swap(IList<int> data, int i, int j)
    {
        int temp = data[i];
        data[i] = data[j];
        data[j] = temp;
    }

    // Float versions: sort the array/list IN PLACE
    public static void SelectionSort(float[] data)
    {
        SelectionSortInternal(data);
    }

    public static void SelectionSort(List<float> data)
    {
        SelectionSortInternal(data);
    }

    public static void QuickSort(float[] data)
    {
        QuickSortInternal(data);
    }

    public static void QuickSort(List<float> data)
    {
        QuickSortInternal(data);
    }

    private static void SelectionSortInternal(IList<float> data)
    {
        for (int i = 0; i < data.Count - 1; i++)
        {
            int smallestIndex = i;

            for (int j = i + 1; j < data.Count; j++)
            {
                if (data[j] < data[smallestIndex])
                    smallestIndex = j;
            }

            Swap(data, i, smallestIndex);
        }
    }

    private static void QuickSortInternal(IList<float> data)
    {
        QuickSortRecursive(data, 0, data.Count - 1);
    }

    private static void QuickSortRecursive(IList<float> data, int low, int high)
    {
        if (low < high)
        {
            int pivotIndex = Partition(data, low, high);
            QuickSortRecursive(data, low, pivotIndex - 1);
            QuickSortRecursive(data, pivotIndex + 1, high);
        }
    }

    private static int Partition(IList<float> data, int low, int high)
    {
        float pivot = data[high];
        int i = low - 1;

        for (int j = low; j < high; j++)
        {
            if (data[j] < pivot)
            {
                i++;
                Swap(data, i, j);
            }
        }

        Swap(data, i + 1, high);
        return i + 1;
    }

    private static void Swap(IList<float> data, int i, int j)
    {
        float temp = data[i];
        data[i] = data[j];
        data[j] = temp;
    }

    // String versions: sort alphabetically (case-insensitive)
    public static void SelectionSort(string[] data)
    {
        SelectionSortInternal(data);
    }

    public static void SelectionSort(List<string> data)
    {
        SelectionSortInternal(data);
    }

    public static void QuickSort(string[] data)
    {
        QuickSortInternal(data);
    }

    public static void QuickSort(List<string> data)
    {
        QuickSortInternal(data);
    }

    private static void SelectionSortInternal(IList<string> data)
    {
        for (int i = 0; i < data.Count - 1; i++)
        {
            int smallestIndex = i;

            for (int j = i + 1; j < data.Count; j++)
            {
                if (string.Compare(data[j], data[smallestIndex], StringComparison.OrdinalIgnoreCase) < 0)
                    smallestIndex = j;
            }

            Swap(data, i, smallestIndex);
        }
    }

    private static void QuickSortInternal(IList<string> data)
    {
        QuickSortRecursive(data, 0, data.Count - 1);
    }

    private static void QuickSortRecursive(IList<string> data, int low, int high)
    {
        if (low < high)
        {
            int pivotIndex = Partition(data, low, high);
            QuickSortRecursive(data, low, pivotIndex - 1);
            QuickSortRecursive(data, pivotIndex + 1, high);
        }
    }

    private static int Partition(IList<string> data, int low, int high)
    {
        string pivot = data[high];
        int i = low - 1;

        for (int j = low; j < high; j++)
        {
            if (string.Compare(data[j], pivot, StringComparison.OrdinalIgnoreCase) < 0)
            {
                i++;
                Swap(data, i, j);
            }
        }

        Swap(data, i + 1, high);
        return i + 1;
    }

    private static void Swap(IList<string> data, int i, int j)
    {
        string temp = data[i];
        data[i] = data[j];
        data[j] = temp;
    }
}

Type-specific notes

  • Use int[] or List<int> for whole numbers such as IDs and quantities
  • Use float[] or List<float> for decimal values such as prices, weights, or temperatures
  • Use string[] or List<string> for text such as names, suburbs, or product codes
  • The helper methods above work with either arrays or List<T>
  • The string examples below sort and search case-insensitively

2) FULL program examples (Console)

These demonstrate the same realistic sequence:

  1. Load data
  2. Sort it
  3. Use binary search (only after sorting)
  4. Show results to the user

Each example includes an array version and a List<T> version.

Integer example

Array version

using System;

class Program
{
    static void Main()
    {
        int[] studentIds = { 1042, 1003, 1099, 1015, 1030 };

        Console.WriteLine("Original IDs: " + string.Join(", ", studentIds));

        SortAlgorithms.QuickSort(studentIds);
        Console.WriteLine("Sorted IDs:   " + string.Join(", ", studentIds));

        int targetId = 1015;
        bool existsBinary = SearchAlgorithms.BinarySearch(studentIds, targetId);
        Console.WriteLine($"Binary search: ID {targetId} exists? {existsBinary}");

        int missingId = 2000;
        bool existsLinear = SearchAlgorithms.LinearSearch(studentIds, missingId);
        Console.WriteLine($"Linear search: ID {missingId} exists? {existsLinear}");
    }
}

List version

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<int> studentIds = new List<int>();
        studentIds.Add(1042);
        studentIds.Add(1003);
        studentIds.Add(1099);
        studentIds.Add(1015);
        studentIds.Add(1030);

        // Lists can grow while the program is running
        studentIds.Add(1066);

        Console.WriteLine("Original IDs: " + string.Join(", ", studentIds));

        SortAlgorithms.QuickSort(studentIds);
        Console.WriteLine("Sorted IDs:   " + string.Join(", ", studentIds));

        int targetId = 1015;
        bool existsBinary = SearchAlgorithms.BinarySearch(studentIds, targetId);
        Console.WriteLine($"Binary search: ID {targetId} exists? {existsBinary}");

        int missingId = 2000;
        bool existsLinear = SearchAlgorithms.LinearSearch(studentIds, missingId);
        Console.WriteLine($"Linear search: ID {missingId} exists? {existsLinear}");
    }
}

Float example

Array version

using System;

class Program
{
    static void Main()
    {
        float[] raceTimes = { 12.4f, 11.8f, 13.1f, 12.0f, 11.6f };

        Console.WriteLine("Original times: " + string.Join(", ", raceTimes));

        SortAlgorithms.QuickSort(raceTimes);
        Console.WriteLine("Sorted times:   " + string.Join(", ", raceTimes));

        float targetTime = 12.0f;
        bool timeExists = SearchAlgorithms.BinarySearch(raceTimes, targetTime);
        Console.WriteLine($"Binary search: time {targetTime} exists? {timeExists}");

        float missingTime = 10.5f;
        bool missingExists = SearchAlgorithms.LinearSearch(raceTimes, missingTime);
        Console.WriteLine($"Linear search: time {missingTime} exists? {missingExists}");
    }
}

List version

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<float> raceTimes = new List<float>();
        raceTimes.Add(12.4f);
        raceTimes.Add(11.8f);
        raceTimes.Add(13.1f);
        raceTimes.Add(12.0f);
        raceTimes.Add(11.6f);

        // Extra values can be collected during runtime
        raceTimes.Add(12.7f);

        Console.WriteLine("Original times: " + string.Join(", ", raceTimes));

        SortAlgorithms.QuickSort(raceTimes);
        Console.WriteLine("Sorted times:   " + string.Join(", ", raceTimes));

        float targetTime = 12.0f;
        bool timeExists = SearchAlgorithms.BinarySearch(raceTimes, targetTime);
        Console.WriteLine($"Binary search: time {targetTime} exists? {timeExists}");

        float missingTime = 10.5f;
        bool missingExists = SearchAlgorithms.LinearSearch(raceTimes, missingTime);
        Console.WriteLine($"Linear search: time {missingTime} exists? {missingExists}");
    }
}

String example

Array version

using System;

class Program
{
    static void Main()
    {
        string[] studentNames = { "Mia", "Lucas", "Amira", "Noah", "Zara" };

        Console.WriteLine("Original names: " + string.Join(", ", studentNames));

        SortAlgorithms.QuickSort(studentNames);
        Console.WriteLine("Sorted names:   " + string.Join(", ", studentNames));

        string targetName = "Amira";
        bool nameExists = SearchAlgorithms.BinarySearch(studentNames, targetName);
        Console.WriteLine($"Binary search: {targetName} exists? {nameExists}");

        string missingName = "Priya";
        bool missingExists = SearchAlgorithms.LinearSearch(studentNames, missingName);
        Console.WriteLine($"Linear search: {missingName} exists? {missingExists}");
    }
}

List version

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<string> studentNames = new List<string>();
        studentNames.Add("Mia");
        studentNames.Add("Lucas");
        studentNames.Add("Amira");
        studentNames.Add("Noah");
        studentNames.Add("Zara");

        // New names can be added while the program runs
        studentNames.Add("Ava");

        Console.WriteLine("Original names: " + string.Join(", ", studentNames));

        SortAlgorithms.QuickSort(studentNames);
        Console.WriteLine("Sorted names:   " + string.Join(", ", studentNames));

        string targetName = "Amira";
        bool nameExists = SearchAlgorithms.BinarySearch(studentNames, targetName);
        Console.WriteLine($"Binary search: {targetName} exists? {nameExists}");

        string missingName = "Priya";
        bool missingExists = SearchAlgorithms.LinearSearch(studentNames, missingName);
        Console.WriteLine($"Linear search: {missingName} exists? {missingExists}");
    }
}

What students must understand

  • QuickSort changes the array or list in place
  • BinarySearch returns true/false based on whether the item exists
  • Binary search is only valid once sorted
  • The same workflow works for integers, floats, and strings
  • The same helper methods work with arrays and List<T>

3) Typical GUI usage (WinForms/WPF pattern)

This is how it looks inside a button click, which matches how many students build their Outcome 1 modules.

// Example: Button click event handler
private void btnCheckId_Click(object sender, EventArgs e)
{
    int[] studentIds = { 1042, 1003, 1099, 1015, 1030 };

    // Sort (so binary search works)
    SortAlgorithms.SelectionSort(studentIds); // or QuickSort(studentIds)

    int targetId = int.Parse(txtStudentId.Text);

    bool exists = SearchAlgorithms.BinarySearch(studentIds, targetId);

    lblResult.Text = exists
        ? "That Student ID already exists."
        : "Student ID is available.";
}

Example: checking a float value from a TextBox

private void btnCheckTime_Click(object sender, EventArgs e)
{
    float[] raceTimes = { 12.4f, 11.8f, 13.1f, 12.0f, 11.6f };

    SortAlgorithms.SelectionSort(raceTimes);

    float targetTime = float.Parse(txtRaceTime.Text);
    bool exists = SearchAlgorithms.BinarySearch(raceTimes, targetTime);

    lblResult.Text = exists
        ? "That race time is already recorded."
        : "That race time is not in the list.";
}

Example: checking a string value from a TextBox

private void btnCheckName_Click(object sender, EventArgs e)
{
    string[] studentNames = { "Mia", "Lucas", "Amira", "Noah", "Zara" };

    SortAlgorithms.SelectionSort(studentNames);

    string targetName = txtStudentName.Text.Trim();
    bool exists = SearchAlgorithms.BinarySearch(studentNames, targetName);

    lblResult.Text = exists
        ? "That student name already exists."
        : "Student name is available.";
}

Key learning

The algorithm functions are called inside the module logic, usually after input is read and before output is displayed.


4) "Correct order" templates (students can memorise)

If list is NOT sorted:

SortAlgorithms.QuickSort(data);
bool found = SearchAlgorithms.BinarySearch(data, target);

If list IS already sorted:

bool found = SearchAlgorithms.BinarySearch(sortedData, target);

If you can't guarantee sorting:

bool found = SearchAlgorithms.LinearSearch(data, target);

Array or List

These templates work the same way for int[], float[], string[], List<int>, List<float>, and List<string>.


Important

When asked to justify algorithm choice, students should write things like:

"Binary search is appropriate because the list is sorted and the algorithm halves the search space each iteration."

"Linear search is appropriate because the list is unsorted."

Not just: "I used binary search" (no justification = no marks)