Simple Bubble Sort C#

Simple bubble sort c#

No, your algorithm works but your Write operation is misplaced within the outer loop.

int[] arr = { 800, 11, 50, 771, 649, 770, 240, 9 };

int temp = 0;

for (int write = 0; write < arr.Length; write++) {
for (int sort = 0; sort < arr.Length - 1; sort++) {
if (arr[sort] > arr[sort + 1]) {
temp = arr[sort + 1];
arr[sort + 1] = arr[sort];
arr[sort] = temp;
}
}
}

for (int i = 0; i < arr.Length; i++)
Console.Write(arr[i] + " ");

Console.ReadKey();

What's the most elegant way to bubble-sort in C#?

What you've pasted there isn't a bubble sort. It's a sort of "brute force" sort but it's not bubble sort. Here's an example of a generic bubble sort. It uses an arbitrary comparer, but lets you omit it in which case the default comparer is used for the relevant type. It will sort any (non-readonly) implementation of IList<T>, which includes arrays. Read the above link (to Wikipedia) to get more of an idea of how bubble sort is meant to work. Note how on each loop we go through from start to finish, but only compare each item with its neighbour. It's still an O(n2) sort algorithm, but in many cases it will be quicker than the version you've given.

public void BubbleSort<T>(IList<T> list)
{
BubbleSort<T>(list, Comparer<T>.Default);
}

public void BubbleSort<T>(IList<T> list, IComparer<T> comparer)
{
bool stillGoing = true;
while (stillGoing)
{
stillGoing = false;
for (int i = 0; i < list.Count-1; i++)
{
T x = list[i];
T y = list[i + 1];
if (comparer.Compare(x, y) > 0)
{
list[i] = y;
list[i + 1] = x;
stillGoing = true;
}
}
}
}

Bubble sort in c# using windows forms. Newbies asking

You have mistake just in dumping output

for (int i = 0; i < nummerlista.Count - 1; i++)

should be

for (int i = 0; i < nummerlista.Count; i++)

Bubble Sort Visual Studio

It's best if you ask a specific question about the nature of your problem and what you'd like to have fixed.

But looking at your code I see some strange behavior. It appears you are abusing a for loop to loop forever and ask the user for a number. If the number isn't -1 you loop over a blank List<int> and print every line after doing a swap if the inner loop's indexed value is greater than the outer loop.

If the input is -1, then you break out of your loop and end the program.

Is your problem that you never seem to have any numbers to sort? Is it that your sort gets an index out of bounds because you're starting with i == 1 but indexes of arrays start at 0? Is it that you're writing numbers without completing your sort first? All of the above?

You'd best be served by looking up some pseudo-code that explains the flow of a bubble sort and then implement it yourself. Failing that you can readily find C# implementations of bubble sort with simple Internet searches.

But to give you a jump start, my guess is you meant to:

  1. while(true)
  2. prompt user for number
  3. if number != -1 store number into array
  4. repeat until number == -1 which will break out of the while loop
  5. sort elements of list or array based on bubble sort
  6. loop your sorted elements and print them out

EDIT: Since you asked for help on how to fix your program, I will give you the below untested code to get you started. You need to implement the bubble sort according to your "work" instructions.

static void Main(string[] args)
{
List<int> ints = new List<int>();

//capture numbers from user input
while(true)
{
Console.WriteLine("Enter a number");
int number = Convert.ToInt32(Console.ReadLine());

if (number == -1) //If user enters magic number, we break out of while loop
break;

ints.Add(number); //Unless we've broken out, add the number to the list
}

//do your bubble sort here
//this is up to you to implement!

//print the results
foreach(int sortedNumber in ints)
{
Console.WriteLine(sortedNumber);
}
}

How to bubble sort an array, but keep the context of the values

A simple way of approaching this is to set up an index array, and then you can sort it having the comparison done on values of the corresponding items in the data array, in that manner you won't lose track of the month.

A simple C# implementation of the above would be as follows:

        int[] CustomerByMonth = { 60, 50, 40, 30, 20, 10 };
int[] index = { 0, 1, 2, 3, 4, 5 };

int tmp = 0;
for (int i = 0; i < index.Length; ++i)
{
for (int j = 0; j < index.Length - 1; ++j)
{
if (CustomerByMonth[index[j]] > CustomerByMonth[index[j + 1]])
{
tmp = index[j + 1];
index[j + 1] = index[j];
index[j] = tmp;
}
}
}

// Display month number and customer amount for month with highest amount of customers
Console.Write(index[CustomerByMonth.Length - 1] + " : " + CustomerByMonth[index[CustomerByMonth.Length - 1]]);


Related Topics



Leave a reply



Submit