Date:

Share:

Bubble Sort in C# – Programming Empire

Related Articles

This article explains Bubble Sort in C #. Basically, bubble sorting is the simplest sorting algorithm. In general, bubble sorting works by scanning each element in the list. Furthermore, it checks the order of the adjacent elements in the list. If the algorithm detects that the adjacent elements are in the wrong order, it replaces those elements. For example, if sorting the bubbles is in ascending order, it will check if the next element is smaller than the previous one. If so, it will replace the two elements. Otherwise, it leaves the elements as they are.

Similarly, in the case of sorting in descending order, sorting the bubbles ensures that the next element is smaller than the previous one. Otherwise, it replaces the two elements. The following example shows how bubble sorting works.

Example of Bubble Sort

Basically, the bubble sorting algorithm takes several transitions as the number of elements in the list. For example, suppose the list is

[9, 3, 2, 1, 5, 8]. Therefore, it will take 6 transitions.

Passage 1: [9, 3, 2, 1, 5, 8] -> [3, 9, 2, 1, 5, 8] -> [3, 2, 9, 1, 5, 8] -> [3, 2, 1, 9, 5, 8] -> [3, 2, 1, 5, 9, 8] -> [3, 2, 1, 5, 8, 9]

Then, the second transition begins.

Passage 2: [3, 2, 1, 5, 8, 9] -> [2, 3, 1, 5, 8, 9] -> [2, 1, 3, 5, 8, 9] -> [2, 1, 3, 5, 8, 9] -> [2, 1, 3, 5, 8, 9] -> [2, 1, 3, 5, 8, 9]

Passage 3: [2, 1, 3, 5, 8, 9] -> [1, 2, 3, 5, 8, 9] -> [1, 2, 3, 5, 8, 9] -> [1, 2, 3, 5, 8, 9] -> [1, 2, 3, 5, 8, 9] -> [1, 2, 3, 5, 8, 9]

As can be seen in the example above, after transition 3, the list was sorted. However, the bubble sorting algorithm has no way of knowing about this information. Thus, he continues to make the remaining transitions. Furthermore, in Passage 2, after the second iterations, no substitution is made. Since then, the elements are in the right order. Similarly, in transition 3, the substitution does not occur after the first iteration. Still, the rest of the iterations are performed. Therefore, the bubble sorting algorithm is not the most efficient sorting algorithm.

Application of Bubble Sort in C #

The following code shows how to implement bubble sorting in C #.

using System;

namespace BubbleSort
{
    class Program
    
        static void Main(string[] args)
        
            int[] myarray = 4,1, 9, -13, 90, 56, 81, 34, -2, -15, 60, 88;
            Console.WriteLine("Array before sorting...");
            foreach (int x in myarray)
                Console.Write(x + "  ");
            Sort.BubbleSort(myarray);
            Console.WriteLine("nArray after sorting...");
            foreach (int x in myarray)
                Console.Write(x + "  ");
            Console.WriteLine();
        
    
    class Sort
    
        public static void BubbleSort(int[] arr)
        
            for(int i=0;i<arr.Length-1;i++)
            
                for(int j=0;j<arr.Length-i-1;j++)
                
                    if (arr[j] > arr[j + 1])
                        swap(ref arr[j], ref arr[j + 1]);
                
            
        
        static void swap(ref int m, ref int n)
        
            int t = m;
            m = n;
            n = t;
        
    
}

Productivity

Sort array by sorting bubbles in C #

Source

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Popular Articles