In this example, i’ll show you How to sorting Array with numbers without sort() method in C#
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace examples { internal class Program { static int[] bubbleSort(int[] arr) { var done = false; while (!done) { done = true; for (var i = 1; i < arr.Length; i += 1) { if (arr[i - 1] > arr[i]) { done = false; var tmp = arr[i - 1]; arr[i - 1] = arr[i]; arr[i] = tmp; } } } return arr; } static void Main(string[] args) { int[] numbers = { 10, 20, 30, 50, 5, 25, 15, 45 }; bubbleSort(numbers); foreach(int i in numbers) { Console.WriteLine(i); } Console.ReadLine(); } } } |
Output: