Bubblesort Algorithm
-
Hey all, I'm just trying to figure out why this bubble sort algorithm is ignoring the last element of the array.
namespace Bubble_sort
{
class SortArray
{
void BubblesortArray(int[] array)
{
int Position = 0;
int endpos = 0;
bool swapped = true;
while (swapped)
{
swapped = false;
Position = 0;
endpos = array.Length;
while (Position < endpos)
{
if (array[Position] > array[Position + 1])
{
int Temp = array[Position];
array[Position] = array[Position + 1];
array[Position + 1] = Temp;
swapped = true;
}
Position++;
endpos--;
}
for (int i = 0; i < array.Length; i++)
{
Console.Write(array[i].ToString() + ", ");
}
Console.WriteLine();
}
}public static void Main(string\[\] args) { SortArray Sort = new SortArray(); int\[\] a = {1, 2, 3, 4, 1, 45, 134, 762, 2}; Sort.BubblesortArray(a); for (int i = 0; i < a.Length; i++) { Console.Write(a\[i\].ToString() + ", "); } Console.Read(); } }
}
thats the code (its a console app). I do this out of curiosity, and annoyance( :-D ) but i would like any help you could offer me. Thankyou.
-
Hey all, I'm just trying to figure out why this bubble sort algorithm is ignoring the last element of the array.
namespace Bubble_sort
{
class SortArray
{
void BubblesortArray(int[] array)
{
int Position = 0;
int endpos = 0;
bool swapped = true;
while (swapped)
{
swapped = false;
Position = 0;
endpos = array.Length;
while (Position < endpos)
{
if (array[Position] > array[Position + 1])
{
int Temp = array[Position];
array[Position] = array[Position + 1];
array[Position + 1] = Temp;
swapped = true;
}
Position++;
endpos--;
}
for (int i = 0; i < array.Length; i++)
{
Console.Write(array[i].ToString() + ", ");
}
Console.WriteLine();
}
}public static void Main(string\[\] args) { SortArray Sort = new SortArray(); int\[\] a = {1, 2, 3, 4, 1, 45, 134, 762, 2}; Sort.BubblesortArray(a); for (int i = 0; i < a.Length; i++) { Console.Write(a\[i\].ToString() + ", "); } Console.Read(); } }
}
thats the code (its a console app). I do this out of curiosity, and annoyance( :-D ) but i would like any help you could offer me. Thankyou.
I'm hesitant to just give you the answer because this is a classic homework problem. Instead I'll give you some hints. (You'll feel better if you solve it yourself anyway) Start by attaching a debugger and stepping through the code in the bubble sort routine line by line. Each pass of a bubble sort should compare each adjacent pair of values. This routine doesn't. Look closely at the indexes you are using to the array being sorted. (Put watches on the values)
Simon
-
I'm hesitant to just give you the answer because this is a classic homework problem. Instead I'll give you some hints. (You'll feel better if you solve it yourself anyway) Start by attaching a debugger and stepping through the code in the bubble sort routine line by line. Each pass of a bubble sort should compare each adjacent pair of values. This routine doesn't. Look closely at the indexes you are using to the array being sorted. (Put watches on the values)
Simon
Thankyou for your help, simon. I suppose it does look like a homework problem ;P my apologies for making it so. But the only reason I asked this is the site where I got it from has it written in a similar way i was just trying to figure out why the program didnt work. Again my apologies.
-
Thankyou for your help, simon. I suppose it does look like a homework problem ;P my apologies for making it so. But the only reason I asked this is the site where I got it from has it written in a similar way i was just trying to figure out why the program didnt work. Again my apologies.
No need to apologise. It's not your fault, it's a common question. Did you manage to figure it out?
Simon
-
No need to apologise. It's not your fault, it's a common question. Did you manage to figure it out?
Simon