Skip to content

Instantly share code, notes, and snippets.

@Strelok78
Last active May 16, 2023 16:57
Show Gist options
  • Save Strelok78/1fa77157cc4a189bf2d43319654a7d33 to your computer and use it in GitHub Desktop.
Save Strelok78/1fa77157cc4a189bf2d43319654a7d33 to your computer and use it in GitHub Desktop.
Outputs sorted numbers to the console, from the smallest to the largest. Array.Sort is not used.
using System.Runtime.CompilerServices;
namespace MyCode
{
internal class Program
{
static void Main(string[] args)
{
int arraySize = 10;
int maxValue = 20;
int[] ints = new int[arraySize];
Random random = new Random();
Console.WriteLine("Original array:");
for (int i = 0; i < ints.Length; i++)
{
ints[i] = random.Next(maxValue);
Console.Write(ints[i] + " ");
}
for (int i = 0; i < ints.Length; i++)
{
for (int j = 0; j < ints.Length - 1; j++)
{
if (ints[j] > ints[j + 1])
{
int temp = ints[j];
ints[j] = ints[j + 1];
ints[j + 1] = temp;
}
}
}
Console.WriteLine("\nSorted array:");
foreach (int number in ints)
{
Console.Write(number + " ");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment