Question: In C sharp, Modify the following insertion sort algorithm, to sort strings.The insertion sort algorithm will sort the elements in this array. Finally, its output
In C sharp, Modify the following insertion sort algorithm, to sort strings.The insertion sort algorithm will sort the elements in this array. Finally, its output will be a sorted array.
class CArray {
private int[] arr;
private int upper;
private int numElements;
public CArray(int size) {
arr = new int[size];
upper = size - 1;
numElements = 0;
}
public void Insert(int item)
{
arr[numElements] = item;
numElements++;
}
public void DisplayElements()
{ for (int i = 0; i <= upper; i++)
Console.Write(arr[i] + " ");
}
public void Clear()
{
for (int i = 0; i <= upper; i++)
{
arr[i] = 0;
}
numElements = 0;
}
public void InsertionSort()
{
int inner, temp;
for (int outer = 1; outer <= upper; outer++)
{
temp = arr[outer];
inner = outer;
while (inner > 0 && arr[inner - 1] >= temp)
{
arr[inner] = arr[inner - 1];
inner -= 1;
}
arr[inner] = temp;
}
}
static void Main(string[] args)
{
CArray nums = new CArray(20);
Random rnd = new Random(100);
Console.WriteLine(" before inseertion sort");
for (int i = 0; i < 10; i++)
{
nums.Insert((int)(rnd.NextDouble() * 100));
}
nums.DisplayElements();
Console.WriteLine(" insertion sort");
nums.InsertionSort();
nums.DisplayElements();
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
