简体   繁体   中英

Hello, I am trying to arrange an array from the small value to big value, but its not working

I am trying to arrange an array from the small value to grow, and for some reason this function does not work (SmallToGrow), the rest is excellent. Thanks.

There are auxiliary functions that I use and are excellent facts, only the (SmallToGrow) function does not work for me and I cannot understand why. I'd love anyone who can help. Thanks enter code here

  //this check if all cells equals
   public bool EverybodyAreEqual(int [] array)
        {
            for (int i = 0; i < array.Length - 1; i++)
                if (array[i] != array[i + 1])
                return false;
            return true;
        }
//This function changes all the values ​​in the array that contain the -numInArray- value you entered, 
 // to the -numChanged- value you entered
 public void ChangNumWhere(ref  int []  array,int numInArray,int numChanged)
        {
            for (int i = 0; i < array.Length; i++)
                if (array[i] == numInArray)
                    array[i] = numChanged;
        }
   //A function that returns the number of values ​​that are not equal in the array
    public int NumDifferentArray(int [] array)
        {
             int[] arr = new int[array.Length];
               for (int i = 0; i < arr.Length; i++)
                  arr[i] = array[i];
             bool con = true;
             int contain = 0;
             int index = 0;
             for(int i=0; con;i++)
             {
                if (!arr.Contains(i))
                 {
                  contain = i;
                  con = false;
                }
             }
            while(!this.EverybodyAreEqual(arr))
            {
                for (int i = 0; i < arr.Length; i++)
                    if (arr[i] != contain)
                    {
                        this.ChangNumWhere(ref arr, arr[i], contain);
                        index++;
                    }             
            }
            return index;
        }
 public int HowTimesExsist(int [] array,int num)
        {
            int index = 0;
            for(int i=0;i<array.Length;i++)
            {
                if (array[i] == num)
                    index++;
            }
            return index;
        }
  /// This function returns a minimum value as required,
  /// for example if you requested 0 the smallest value is returned,
  /// if 1 is returned one above it and so on,
  /// if the index is greater than the length of the array the largest number is returned
    public int MinBottom(int[] array, int num)
        {
            if (num < 0)
                throw new Exception("num canot be less then 0");         
            int max = this.MaxArray(array);
            while (num > 0)
            {
               int min = this.MinArray(array);
               for (int i = 0; i < array.Length; i++)             
                    if(array[i]==min)
                         array[i] = max;            
                num--;
            }
            return this.MinArray(array);               
        }
 public  int[]  SmallToGrow(int [] array)
        {
            int i = 0;
            int[] arr = new int[array.Length];
             for (int j = 0; j < this.NumDifferentArray(array); j++)
                for (int b = 0; b < this.HowTimesExsist(array, MinBottom(array, j)); i++, b++)
                    arr[i] = this.MinBottom(array, j);
            return arr;
         }

Why don't you use a list instead? They have a method attached to it that sorts it all for you.

List<int> sortInt = new List<int>() { 2, 5, 1, 50, 258, 87, 63, 52, 100, 85, 21 };

sortInt.Sort();

Returned them in numerical order with [0] being 1 and [10] being 258.

you can then turn the list to an array with sortint.ToArray(); .

Edit

As dymanoid mentioned you can just use your array and just run the array.Sort() method with it. Learn something new every day.

It looks like you are in large part re-engineering some common functionality in System.Collections.Generic and System.Linq -

public bool EverybodyAreEqual(int[] array)
{
    // If all items are the same,
    // there should only be one distinct item in the collection
    return array.Distinct().Length == 1;
}

public int NumDifferentArray(int[] array)
{
    // Group the numbers in the array and
    // count the number of groups with only one item
    return array.GroupBy(number => number).Where(g => g.Count() == 1);
}

public int HowTimesExsist(int[] array, int num)
{
    // Count the number of times a number appears in the array
    return array.Count(n => n == num);
}

/// This function returns a minimum value as required,
/// for example if you requested 0 the smallest value is returned,
/// if 1 is returned one above it and so on,
/// if the index is greater than the length of the array the largest number is returned
public int MinBottom(int[] array, int num)
{
    if (num < 0)
    {
        // Be specific about the type of exception you are throwing
        throw new ArgumentOutOfRangeException(nameof(num));
    }

    // Sort a copy of your array
    var sorted = Array.Copy(array);
    Array.Sort(sorted);

    // If there are any items over the specified minimum, return those
    // otherwise, return the highest number in the array
    // Using LastOrDefault for the maximum will return 0 if the initial array is empty
    var itemsOverMinimum = sorted.Where(n => n >= num);
    return itemsOverMinimum.Any() ? itemsOverMinimum.First() : sorted.LastOrDefault();
}

public int[] SmallToGrow(int[] array)
{
    // Because you are returning an array, that implies that the original array should not change
    // Copy the array and sort it
    var copy = Array.Copy(array);
    Array.Sort(copy);
    return copy;
}

I saw that you mentioned that you are trying to find alternative ways to accomplish some of these things, and I want to give you some advice about that.

I think it's pretty cool that you want to challenge yourself. However, this specific functionality is part of System libraries. One of the best parts about working in C# is how much of this sort of thing is already written for you, and this functionality being added to System means that Microsoft believes these pieces are the core (pun intended) building blocks for working in .NET.

Unless your project is to specifically write a better sorting algorithm, you are not going to write this better than it is in those libraries. I've been doing this for a while, and I'm not going to be able to either.

But that doesn't mean you should stop learning. Instead, I would encourage you to look at the github source for the methods I used in my snippets above. I think that will probably be a lot more helpful than re-engineering this stuff from scratch.

https://github.com/dotnet/runtime/blob/master/src/libraries/System.Linq/src/System/Linq/Distinct.cs https://github.com/dotnet/runtime/blob/master/src/libraries/System.Linq/src/System/Linq/Grouping.cs https://github.com/dotnet/runtime/blob/master/src/libraries/System.Linq/src/System/Linq/Where.cs https://github.com/dotnet/runtime/blob/master/src/libraries/System.Linq/src/System/Linq/Count.cs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM