Bubble sort
Last updated:
8/23/2020
⁃
Difficulty:
Intermediate
Create a C# program that requests 10 integers from the user and orders them by implementing the bubble sort algorithm.
Input
7
8
6
2
3
2
4
1
8
4
Output
1 2 2 3 4 4 6 7 8 8
Solution
using System;
public class BubbleSort
{
public static void Main(string[] args)
{
int total = 10;
int[] numbers = new int[total];
for (int i = 0; i < total; i++)
{
numbers[i] = Convert.ToInt32(Console.ReadLine());
}
// Bubble Sort
for (int i = 0; i < total - 1; i++)
{
for (int j = i + 1; j < total; j++)
{
if (numbers[i] > numbers[j])
{
int aux = numbers[i];
numbers[i] = numbers[j];
numbers[j] = aux;
}
}
}
foreach (int n in numbers)
{
Console.Write("{0} ", n);
}
}
}