Bubble Sort

 

 

using System;

namespace ZeroToCSharp {
    class Program {
        static void Main(string[] args) {

            int[] array = new int[6] { 24, 6, 26, 11, 15, 3 };
            int temp;
            bool isSwapped;
            for (int i = 0; i < array.Length - 1; i++) {
                isSwapped = false;
                for (int j = 0; j < array.Length - 1; j++) {
                    if (array[j] > array[j + 1]) {
                        temp = array[j];
                        array[j] = array[j + 1];
                        array[j + 1] = temp;
                        isSwapped = true;
                    }
                }
                if (!isSwapped) {
                    break;
                }
            }

            //To print output
            Console.Write("Sorted Array: ");
            for (int i = 0; i < array.Length; i++) {
                Console.Write(array[i] + " ");
            }
            Console.ReadKey();
        }
    }
}

 

Output:

Sorted Array: 3 6 11 15 24 26

 

 

 

 

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x