Bubble sort implementation in c#

Jack Herer 110 Reputation points
2023-04-26T17:46:47.1+00:00

Here's how to implement a bubble sort in c#.

Follow for more content like this :)

int[] array = { 108, 23, 69, 420, 9 };
int temporary;

for (int j = 0; j <= array.Length - 2; j++)
{
    for (int i = 0; i <= array.Length - 2; i++)
    {
        if (array[i] > array[i + 1])
        {
            temporary= array[i + 1];
            array[i + 1] = array[i];
            array[i] = temporary;
        }
    }
}

Console.WriteLine("Sorted:");
foreach (int p in array)
{
    Console.Write($"{p} ");
}
Console.ReadLine();
Developer technologies .NET Other
Developer technologies C#
{count} votes

3 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2024-10-11T16:41:15.0366667+00:00

    while it returns the correct result, it is not correct. there is no need to process the last element as the exchange has already happened and you are reprocessing swapped items:

    int[] array = { 108, 23, 69, 420, 9 };
    
    for (int j = 0; j < array.Length - 1; j++)
    {
        for (int i = 0; i < array.Length - j - 1; i++)
        {
            if (array[i] > array[i + 1])
            {
                int temporary = array[i + 1];
                array[i + 1] = array[i];
                array[i] = temporary;
            }
        }
    }
    
    Console.WriteLine("Sorted:");
    
    foreach (int p in array)
    {
        Console.WriteLine(p);
    }
    

    and with early exit optimization:

    int[] array = { 108, 23, 69, 420, 9};
    
    for (int j = 0; j < array.Length - 1; j++)
    {
    	bool swapped = false;
        for (int i = 0; i < array.Length - j - 1; i++)
        {
            if (array[i] > array[i + 1])
            {
                int temporary = array[i + 1];
                array[i + 1] = array[i];
                array[i] = temporary;
    			swapped = true;
            }
        }
    	if (swapped == false)
           break;
    }
    
    
    1 person found this answer helpful.
    0 comments No comments

  2. Aleno Jankowsky 0 Reputation points
    2024-10-11T11:06:43.7266667+00:00

    Very nice code! Well done :)

    However, your implementation is wrong, because instead of array.length -2 it should be one:

    First for loop: array.length

    On the other hand:

    Second for loop: array.length -1

    It would not sort the whole array if you wrote it like this.

    But otherwise good code!


  3. Aleno Jankowsky 0 Reputation points
    2024-10-11T11:36:23.3566667+00:00

    So the correct solution should look like this, for everyone searching for a correct algorithm:

    int[] array = { 108, 23, 69, 420, 9 };
    int temporary;
    
    for (int j = 0; j < array.Length; j++)
    {
        for (int i = 0; i < array.Length - 1; i++)
        {
            if (array[i] > array[i + 1])
            {
                temporary = array[i + 1];
                array[i + 1] = array[i];
                array[i] = temporary;
            }
        }
    }
    
    Console.WriteLine("Sorted:");
    
    foreach (int p in array)
    {
        Console.WriteLine(p);
    }
    
    Console.ReadLine();
    
    

    Or complementary for the logical operator <= like implemented above:

    int[] array = { 108, 23, 69, 420, 9 };
    int temporary;
    
    for (int j = 0; j <= array.Length - 1; j++)
    {
        for (int i = 0; i <= array.Length - 2; i++)
        {
            if (array[i] > array[i + 1])
            {
                temporary = array[i + 1];
                array[i + 1] = array[i];
                array[i] = temporary;
            }
        }
    }
    
    Console.WriteLine("Sorted:");
    foreach (int p in array)
    {
        Console.WriteLine(p);
    }
    Console.ReadLine();
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.