C# Coding Test Course, Bubble Sort Program 2

This article describes how to implement the bubble sort algorithm in C#. Bubble sort is one of the most basic sorting algorithms and often appears in various algorithm tests. In this post, we will take a detailed look at the principle of bubble sort and examine an example implemented in C#.

What is Bubble Sort?

Bubble sort is a simple sorting algorithm that compares adjacent elements of an array to arrange the order of the two elements. One can visualize the largest (or smallest) element ‘bubbling’ up to the end of the array. This algorithm can be implemented simply, but it is not an efficient sorting algorithm. Its time complexity is O(n^2).

Problem Description

Sort the given integer array in ascending order using the bubble sort algorithm.

Input

- Input: Integer array arr = [64, 34, 25, 12, 22, 11, 90]

Output

- Output: Sorted array [11, 12, 22, 25, 34, 64, 90]

Explanation of the Bubble Sort Algorithm

Bubble sort follows these steps:

  1. Check the length of the array.
  2. Use two nested loops to iterate through all elements of the array.
  3. Compare two adjacent elements and swap them if they are in the wrong order.
  4. After one full comparison, the largest element moves to the end of the array.
  5. Repeat this process N-1 times to sort the array.

Implementing Bubble Sort in C#

Now, let’s implement the above process in C# code.


using System;

class Program
{
    static void Main()
    {
        int[] arr = { 64, 34, 25, 12, 22, 11, 90 };
        Console.WriteLine("Original array:");
        PrintArray(arr);
        
        BubbleSort(arr);
        
        Console.WriteLine("Sorted array:");
        PrintArray(arr);
    }

    static void BubbleSort(int[] arr)
    {
        int n = arr.Length;
        for (int i = 0; i < n - 1; i++)
        {
            for (int j = 0; j < n - i - 1; j++)
            {
                if (arr[j] > arr[j + 1])
                {
                    // Swap arr[j] and arr[j + 1]
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

    static void PrintArray(int[] arr)
    {
        foreach (var item in arr)
        {
            Console.Write(item + " ");
        }
        Console.WriteLine();
    }
}

Explanation of the Code

The above code is an example of implementing bubble sort using a simple C# console application.

  • BubbleSort(int[] arr): A method that sorts the given array. This method uses two nested loops to iterate through the array, comparing and swapping adjacent elements.
  • PrintArray(int[] arr): A helper method that prints the elements of the array, separating each element with a space.

Conclusion

Bubble sort is an easy-to-learn and understand sorting algorithm, but it is advisable to use other algorithms with better performance in practice. However, understanding the concepts of basic sorting algorithms is important in preparing for coding tests.

I hope this article helps with your future coding test preparations. If you have any questions or points for discussion, please leave a comment!