Search code examples
c#selection-sort

Selection sort method does not do anything C#


i'm trying to make a selection sort algorithm work using an implemented Data Array class with a swap method. The problem is when i try to run the code to see if it works nothing changes in the output and i'm seriously confused as to why that is. The numbers are randomly generated every time

public static void SelectionSort(DataArray items)
        {
            int index = 0;
            double minValue = 0;
            for(int i = 0; i < items.Length - 1; i++)
            {
                index = i;

                minValue = items[i];

                for(int j = i + 1; j < items.Length; j++)
                {

                    if(minValue > items[j])
                    {                    
                        minValue = items[j];                     
                        index = j;
                    }
                }

                items.Swap(i, index, items[i], minValue);              

            }

My swap method

   class MyDataArray:DataArray
    {
        double[] data;
        public MyDataArray(int n, int seed)

        {
            data = new double[n];
            length = n;
            Random rand = new Random(seed);
            for
           (int i = 0; i < length; i++)

            {
                data[i] = rand.NextDouble();

            }

        }

        public MyDataArray(int n)
        {
            data = new double[n];
            length = n;
        }
        public override double this[int index]
        {
            get { return data[index]; }

        }
        public override void Swap(int i, int j, double a, double b)
        {
            data[i] = a;
            data[j] = b;
        }

    }
}

Data Array class

abstract class DataArray
    {
        public int length;
        public int Length { get { return length; } }
        public abstract double this[int index] { get; }
        public abstract void Swap(int i, int j, double a, double b);
        public void Print(int n)
        {
            for (int i = 0; i < n; i++)
                Console.Write(" {0:F3} ", this[i]);
            Console.WriteLine();
        }

    }

When i try to run the code using this method

    public static void Test_Array_List(int seed)
    {
        int n = 12;
        MyDataArray myarray = new MyDataArray(n, seed);
        Console.WriteLine("Before sorting");
        myarray.Print(n);
        Console.WriteLine("After sorting");
        SelectionSort(myarray);
        myarray.Print(n);

    }

I get this result where nothing changes and i have no idea why

Before sorting
 0.216  0.578  0.831  0.898  0.653  0.496  0.380  0.567  0.230  0.611  0.091  0.076
After sorting
 0.216  0.578  0.831  0.898  0.653  0.496  0.380  0.567  0.230  0.611  0.091  0.076
Press any key to continue . . 

Thank you in advanced


Solution

  • You should learn to use the debugger to diagnose these types of problems. Run through the program line-by-line, examining the variables and outputs of functions, to see where something doesn't work as expected.

    That said, I suspect that your use of Swap is incorrect, since you are passing the values at i and j back to it in that order, so it's not really swapping anything. You can avoid this type of bug by just passing the indices:

    public override void Swap(int i, int j)
    {
        var temp = data[i];
        data[i] = data[j];
        data[j] = temp;
    }