Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C# Your task is to write a method called InsertionSort, which takes an array of integers and sorts them into ascending order: public static void

C#

Your task is to write a method called InsertionSort, which takes an array of integers and sorts them into ascending order:

public static void InsertionSort(int[] array)

Do not use Array.Sort or other predefined sorting methods that are part of the .NET framework to implement this method. This method should be written from scratch.

Lecture 9 contains detailed pseudocode for implementing this method on slides 16-18. The pseudocode is reproduced here for your convenience:

sorted length is 1 // list of single element is already sorted while sorted length < length of list let newItem = first element of unsorted part // insert newItem in its correct position by // moving all the items greater than newItem up one position let currentPos = start of unsorted part while currentPos > 0 and item at currentPos-1 > newItem move the item at currentPos-1 up one position decrement currentPos end while // insert newItem in the vacant position item at currentPos = newItem increment sorted length end while 

If the pseudocode is confusing read the rest of the lecture slides about Insertion Sort first, to ensure you have a good grasp of the algorithm before you attempt to start implementing it.

The provided Main() method contains a simple test for your InsertionSort(), and should produce the following output:

Note that the Main() method of the provided code is never called- only the InsertionSort() method is called in order to test your program. This also means your InsertionSort() method must be public.

If you still have difficulties, here are some tips when writing programs for AMS:

Do not write your program directly into the text box. You should copy and paste the sample code into Visual Studio, write your program there, run it and test it to make sure it works, then paste it into AMS. You only get a limited number of attempts at each AMS exercise, so make sure they count.

Sample code:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace InsertionSort { class Program { public static void InsertionSort(int[] array) { // Write your insertion sort algorithm here } static void Main(string[] args) { int[] array = { 1, 2, 6, 3, 4 }; InsertionSort(array); for (int i = 0; i < array.Length; i++) { if (i > 0) { Console.Write(", "); } Console.Write("{0}", array[i]); } Console.WriteLine(" Press enter to exit."); Console.ReadLine(); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Strategic Database Technology Management For The Year 2000

Authors: Alan Simon

1st Edition

155860264X, 978-1558602649

More Books

Students also viewed these Databases questions

Question

What is RAM as far as telecommunication is concerned?

Answered: 1 week ago

Question

Question 1: What is reproductive system? Question 2: What is Semen?

Answered: 1 week ago

Question

Describe the sources of long term financing.

Answered: 1 week ago

Question

9. Mohawk Industries Inc.

Answered: 1 week ago

Question

8. Satyam Computer Services Limited

Answered: 1 week ago

Question

2. Explain how the role of training is changing.

Answered: 1 week ago