How to implement the bubble sort algorithm in C#

1 Answer

0 votes
using System;

class Program
{
    static void bubbleSort(int[] arr) {
      int len = arr.Length;
       
      for (int i = 0; i < len - 1; i++) {
         for (int j = 0; j < len - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
               int temp = arr[j];
               arr[j] = arr[j + 1];
               arr[j + 1] = temp;
            }
         }
      }
    }
    static void Main() {
       int[] arr = {6, 8, 0, 3, 1, 6, 4};
         
        bubbleSort(arr);
  
        foreach (int n in arr) {
           Console.Write("{0} ", n);
       }
    }
}




/*
run:

0 1 3 4 6 6 8 

*/

 



answered Mar 30, 2021 by avibootz

Related questions

1 answer 131 views
1 answer 89 views
1 answer 98 views
1 answer 110 views
1 answer 110 views
1 answer 154 views
1 answer 233 views
...