Selection Sort
using System; namespace ZeroToCSharp { class Program { static void Main(string[] args) { int[] array = new int[6] { 24, 6, 26, 11, 15, 3 }; int min, temp; for (int i = 0; i < array.Length - 1; i++) { min = i; for (int j = i + 1; j < array.Length; j++) { if (array[j] < array[min]) min = j; } if (array[min] != array[i]) { temp = array[min]; array[min] = array[i]; array[i] = temp; } } //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
Subscribe
0 Comments