How can I change a List method to an Array method?

MurplePunky

I'm trying to use a merge-sort algorithm to sort an Array of numbers, but the algorithm takes in a List<> not an Array[]

I've tried changing the algorithm to use an Array[] instead by replacing '.Count' with '.Length', but because of the fixed-size nature of Arrays I'm having trouble with the lines that use 'add' and 'remove'

private static List<int> MergeSort(List<int> unsorted)
        {
            if (unsorted.Count <= 1)
                return unsorted;

            List<int> left = new List<int>();
            List<int> right = new List<int>();

            int middle = unsorted.Count / 2;
            for (int i = 0; i < middle; i++)  
            {
                left.Add(unsorted[i]);
            }
            for (int i = middle; i < unsorted.Count; i++)
            {
                right.Add(unsorted[i]);
            }

            left = MergeSort(left);
            right = MergeSort(right);
            return Merge(left, right);
        }

        private static List<int> Merge(List<int> left, List<int> right)
        {
            List<int> result = new List<int>();

            while (left.Count > 0 || right.Count > 0)
            {
                if (left.Count > 0 && right.Count > 0)
                {
                    if (left.First() <= right.First())  
                    {
                        result.Add(left.First());
                        left.Remove(left.First());      
                    }
                    else
                    {
                        result.Add(right.First());
                        right.Remove(right.First());
                    }
                }
                else if (left.Count > 0)
                {
                    result.Add(left.First());
                    left.Remove(left.First());
                }
                else if (right.Count > 0)
                {
                    result.Add(right.First());

                    right.Remove(right.First());
                }
            }
            return result;
        }

I would like to be able to pass an unsorted array into the method and it output a sorted array.

Aiyuni

Arrays are fixed sized and do not share the same methods as Lists, which are variable sized. Thus you need to convert your Array to a List then pass the List as the param to the method. You can use:

List<object> list = myArray.Cast<Object>().ToList();

Or if you prefer linq:

List<object> list = myArray.ToList<object>();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I convert an array to a list without using Arrays.asList() method or Java List Interface

How can I call a method on each element of a List?

How can I pass the contents of a list to a varargs method?

how can i change step of index in forEach method at javascript?

How can I create a list of method references?

How can I pass List in my method parameter?

How I can make list length method in scala?

How can I change the package type when calling a method?

How can I initialize this array by calling this method?

Javascript - How can I change scope and access method without 'this'

How can I create a getter method and a setter method for a multidimensionnal array?

How can I change my fcitx input method from the terminal?

How can I directly supply a method reference to an optional list?

How can I change selected value of a DropDownList with having change method?

How can I pass an array to a method?

How can I use the Linux CLI to change keyboard input method?

how can I mock a service to throw an exception a method that returns a List?

How can I setup List<string> ReadBooklets method with mock?

How can I change the value inside the data method

How can i save a reference to an Array method?

what is "method" type in pandas, and how can I change it to List type?

Why can't I change the values of a list in the class using a method?

How can I bind checkbox value and invoke a method on change?

How can I change the CSS style using Typescript method in Angular?

How can I create a method to /2 the array

How to change an array in a different method?

How can I remove an object from a list using its method?

How can I implement a method that change directly the value this with dart extension?

How can I make an array of method pointers?