Write a program in C# to sort the elements of the array in ascending order using Bubble Sort technique.
Following is the simple C# code :
Following is the simple C# code :
class Lab { static void Main(string[] args) { int[] myArray = { 875, 15, 55, 775, 582, 476, 351, 7 }; int temp = 0; Console.Write("elements of the array in without sorting\n"); for (int i = 0; i < myArray.Length; i++) { Console.Write(myArray[i] + " "); } for (int i = 0; i < myArray.Length; i++) { for (int j = 0; j < myArray.Length - 1; j++) { if (myArray[j] > myArray[j + 1]) { temp = myArray[j + 1]; myArray[j + 1] = myArray[j]; myArray[j] = temp; } } } Console.Write("\n\nelements of the array in ascending order\n"); for (int i = 0; i < myArray.Length; i++) { Console.Write(myArray[i] + " "); } Console.ReadKey(); } }
Output:-
elements of the array in without sorting 875 15 55 775 582 476 351 7 elements of the array in ascending order 7 15 55 351 476 582 775 875
No comments:
Post a comment