1,105,371 Community Members

Sorting arrays without array.sort

Member Avatar
write.me
Newbie Poster
5 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Here is the assignent:

Sort the integer elements within the array from lowest (element 0) to highest (element 4). Do not use the preexisting Array.Sort method; code your own. Output the sorted array as follows:

Element 0 => smallest value
Element 1 => next smallest value
Element 2 => next smallest value
Element 3 => next smallest value
Element 4 => Largest value

Suggestions: Use a for() loop inside of a while() loop. Probably the easiest way to code this is to compare array element i with element i+1 in the for() loop. If i+1 < i then swap the values. Be careful not to overflow the array. The while() loop checks whether a swap occurred. When you can go through the array without swapping any values then the array is sorted and you can exit the while() loop.

        static void Main(string[] args)


                    {
                        string myName = "";

                        Console.WriteLine("What is your name?");
                        myName = Console.ReadLine();

                        Console.WriteLine();
                        Console.WriteLine("Your name is {0}.", myName);
                        Console.ReadKey();
                        //-------------------------------------------------------------------------



                        int[] Inputs= new int[5];
                        int intValue = 0; 
                        float Average = 0;
                        float Variance = 0;
                        for (int i = 0; i < Inputs.Length; i++)
                        {
                            while (intValue < 10 || intValue > 50)
                            {
                                Console.Clear();
                                Console.WriteLine("Please enter {0} integers between 10 and 50 one at a time: ", Inputs.Length - i);
                                if (!int.TryParse(Console.ReadLine(), out intValue))


                           if (!int.TryParse(Console.ReadLine(), out intValue))
                               intValue = i++;
                           Inputs[i++] = intValue;
                               if (!int.TryParse(Console.ReadLine(), out intValue))
                                   intValue = i++;
                               Inputs[i++] = intValue;
                                   if (!int.TryParse(Console.ReadLine(), out intValue))
                                       intValue = i++;
                                   Inputs[i++] = intValue;
                                       if (!int.TryParse(Console.ReadLine(), out intValue))
                                           intValue = i++;
                                       Inputs[i++] = intValue;
                                           if (!int.TryParse(Console.ReadLine(), out intValue))
                                               intValue = i++;
                                           Inputs[i++] = intValue;



                                {



                            }



                            }

                        }
                        Average = (float)(Inputs.Sum() / 5.0f);
                        Console.Clear();
                        for (int i = 0; i < Inputs.Length; i++)
                        {
                    Console.WriteLine(Inputs[i]);
                    Variance +=(Inputs[i]-Average)*(Inputs[i]-Average);
                }
                Console.WriteLine("The average of the integers is {0}\n", Average);
                Variance /= 4;
                Console.WriteLine("The variance of the integers is {0}\n", Variance);
                Console.ReadKey();
                Console.WriteLine("Your name is {0}.", myName);
                Console.ReadKey();





                    }
                }
            }





Now how do I sort my Integers without using array.sort?
Member Avatar
ddanbe
Posting Expert
5,355 posts since Oct 2008
Reputation Points: 2,066 [?]
Q&As Helped to Solve: 908 [?]
Skill Endorsements: 46 [?]
Featured
 
0
 
Member Avatar
Momerath
Senior Poster
3,830 posts since Aug 2010
Reputation Points: 1,327 [?]
Q&As Helped to Solve: 664 [?]
Skill Endorsements: 19 [?]
Featured
 
0
 

And this

Member Avatar
write.me
Newbie Poster
5 posts since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks for the link. Helped a lot didn't mean to ask for the answer, I should've asked a gateway question.

Member Avatar
Momerath
Senior Poster
3,830 posts since Aug 2010
Reputation Points: 1,327 [?]
Q&As Helped to Solve: 664 [?]
Skill Endorsements: 19 [?]
Featured
 
0
 

Searching for sorts on the internet will give you lots of answers, it's hard not to find them. Explaining how it works on the other hand ...

Member Avatar
tinstaafl
Postaholic
2,012 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 402 [?]
Skill Endorsements: 35 [?]
 
0
 

The simplest is probably the bubble sort, just as your instructor suggested. There's even a code snippets posted here for that type of sort.

Member Avatar
ddanbe
Posting Expert
5,355 posts since Oct 2008
Reputation Points: 2,066 [?]
Q&As Helped to Solve: 908 [?]
Skill Endorsements: 46 [?]
Featured
 
0
 

@Mmomerath: You definitely should turn your awesome sorting into some kind of tutorial! :o)

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article