Question Bubble sort

Nessuno

New member
Joined
Nov 14, 2020
Messages
1
Location
Italy
Programming Experience
Beginner
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

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]);
            }

        }
    }
 
Those variables are completely independent of the array. The variables are not pointers to the values in the array. They are copies of those values. Any changes you make to those variables have zero effect on the array. If you want to use those variables then you have to:
  1. Get the initial values out of the array and into the variables.
  2. Modify the variables as required.
  3. Push the values from the variables back into the array.
You're doing steps 1 and 2 but you're not doing step 3.
 
As a quick aside, C# does support using pointers, but you'll have to mark the code as unsafe, and compile with the unsafe flag on. Not really recommended for a beginner. And only suggested as a last resort for an experienced programmer as a last resort for getting that extra bit of performance when he/she already has determined that they are already using an optimal algorithm. With C# 7.0's support for Span's and Memory's, there's not even as much need for pointers.
 
Comparison of 7 bubble sorts with pictures in 2023


Won "Russian sorting halves"
whose implementation is in C#


2 times faster according to report of American author of topic

rsp2023.png
 
Back
Top Bottom