SoFunction
Updated on 2025-03-06

Example of c# bubble sorting algorithm

namespace bubble sort
{
    class Program
    {
static void swap( ref int  atemp, ref int  btemp)// Pay attention to the use of ref
        {
            int temp = atemp;
            atemp = btemp;
            btemp = temp;
        }
        static void Main(string[] args)
        {
            int temp=0;
            int[]arr={23,44,66,76,98,11,3,9,7};
("Array before sorting:");
            foreach(int item in arr)
            {
                (item+" ");
            }
            ();
            for(int i=0;i<-1;i++)
            {
                for(int j=0;j<-1-i;j++)
                {
                    if (arr[j] > arr[j + 1])
                          swap( ref  arr[j], ref arr[j + 1]);
                }
            }
("Sorted array:");
           foreach(int item in arr)
           {
               (item+" ");
            }
            ();
            ();