I have to create a bubble sort program in C# that sorts random integers stored in array. I have to do these for arrays with lengths 100, 1,000, 10,000 ect. I have some code that runs and compiles correctly, but does not perform right. The code is below:
using System;
namespace SortingProject
{
class MainClass
{
public static void Main(string[] args)
{
int[] list = {100};
Random rand = new Random();
for (int i = 0; i < list.Length; i++) {
list[i] = rand.Next(1,100);
}
BubbleSorting(list);
}
public static void BubbleSorting(int [] array) {
int first = 0;
for (int sorted = 0; sorted < array.Length; sorted++)
{
for (int sort = 0; sort < array.Length - 1; sort++)
{
if (array[sort] > array[sort + 1])
{
first = array[sort + 1];
array[sort + 1] = array[sort];
array[sort] = first;
}
}
}
for (int i = 0; i < array.Length; i++)
Console.Write(array[i] + " ");
Console.ReadKey();
}
}
}
When I run the program, the output is only one randomly generated integer and I was wondering why this was happening? I know something in my code is not working properly but am I properly executing a bubble sort? I am not seeing what is wrong the code.
Change this:
int[] list = {100};
for this:
int[] list = new int[100];