Thread: Sorting an Array

  1. #1
    Registered User Mcdom34's Avatar
    Join Date
    Jun 2012
    Location
    North Royalton, Ohio, United States
    Posts
    22

    Sorting an Array

    Hello, I am trying to sort an array of random numbers by passing it to a method Sort(), sort the array, and then pass the entire array back to the calling program. Each time I run this though, the array doesn't seem to sort. I'm not sure if the problem lies with my sorting algorithm or if it has something to do with calling the function.
    Code:
    static void HighAverage(int[] a)        {
                for (int i = 0; i < a.Length; i++)
                {
                    if ((i + 1) % 10 == 0 && i != 0)
                        Console.WriteLine(a[i]);
                    else
                        Console.Write("{0} ", a[i]); 
                }
                Console.WriteLine("\n");
                Sort(ref a);
                for (int i = 0; i < a.Length; i++)
                {
                    if ((i + 1) % 10 == 0 && i != 0)
                        Console.WriteLine(a[i]);
                    else
                        Console.Write("{0} ", a[i]);
                }
            }
            static void Sort(ref int[] a)
            {
                int min, t;
    
    
                for (int i = 0; i < a.Length - 1; i++)
                {
                    min = i;
                    for (int j = (i + 1); j < a.Length; j++)
                    {
                        if (a[j] < a[min])
                            min = j;
                    }
                    t = a[min]; a[min] = a[i]; a[i] = a[min];
                }
            }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    in line 32 variable t is not used properly.You should write a[i] = t; at the end of the line

  3. #3
    Registered User Mcdom34's Avatar
    Join Date
    Jun 2012
    Location
    North Royalton, Ohio, United States
    Posts
    22
    Thanks! It works perfectly now

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I am glad to here that.However notice that if the lenght of the array is large,then the complexity of this algorithm will be Θ(nē),which is very bad.

  5. #5
    Registered User Mcdom34's Avatar
    Join Date
    Jun 2012
    Location
    North Royalton, Ohio, United States
    Posts
    22
    If that were the case, what would you suggest I do instead of using selection sort?

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    There is a much simpler and more elegant way to do this. It just so happens the MSDN example uses temperatures.
    IComparable Interface (System)

    Object oriented languages are all about objects. Primitive arrays and the like should be avoided wherever possible. There are generally better constructs available for representing the data.
    Last edited by VirtualAce; 06-28-2012 at 05:31 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with array sorting please
    By SpockRox08 in forum C++ Programming
    Replies: 2
    Last Post: 12-13-2010, 02:25 AM
  2. Adding nodes to an array at the top & array sorting
    By Wolf` in forum C++ Programming
    Replies: 1
    Last Post: 06-25-2010, 12:48 PM
  3. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  4. i need help with sorting an array
    By stubin87 in forum C Programming
    Replies: 31
    Last Post: 04-20-2007, 07:24 PM
  5. 2D array sorting from min. to max.
    By khaled_helmy in forum C Programming
    Replies: 1
    Last Post: 10-14-2004, 02:17 PM