Hi
This code only works when I use array[iter] and array[iter + 1].
I want to replace them with current and next variable in order to make the code more readable.
How do you achieve that ? ( pointer ? )
If I replace array[] the code doesn't update it.
sry I'm a beginner
This code only works when I use array[iter] and array[iter + 1].
I want to replace them with current and next variable in order to make the code more readable.
How do you achieve that ? ( pointer ? )
If I replace array[] the code doesn't update it.
sry I'm a beginner
Bubble:
class Program
{
static int[] array = { 7, 12, 3, 9, 5 };
static bool swap = true;
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
while (swap)
{
for (int iter = 0; iter < array.Length - 1; iter++)
{
Console.WriteLine(array[iter] + " vs " + array[iter + 1]);
int current = array[iter];
int next = array[iter + 1];
swap = false;
//if (array[iter] > array[iter + 1]) // if it's true then swap
if (current > next) // if it's true then swap
{
swap = true;
current = current + next;
next = current - next;
current = current - next;
Console.WriteLine(current + " <-> " + next);
/*
array[iter] = array[iter] + array[iter + 1];
array[iter + 1] = array[iter] - array[iter + 1];
array[iter] = array[iter] - array[iter + 1];
Console.WriteLine(array[iter] + " <-> " + array[iter + 1]);
*/
}
}
Console.WriteLine("\n");
for (int iter = 0; iter < array.Length; iter++)
Console.WriteLine(array[iter]);
}
}
}