COMP.6211 Algorithms and Data Structures Assignment - Sorting and Searching, Toi Ohomai Institute of Technology, New Zealand
Do Want To Hire Tutor For Original COMP 6211 Algorithms And Data Structures Assignment Solution? Avail Quality COMP 6211 Algorithms And Data Structures Assignment Writing Service At Best Rates!
Question 1 - You are required to write a console application with an Algorithm class
You are to write a Display method, which will display the contents of this array to the screen. Add the appropriate code to Main, so the numbers are displayed.
Answer -
using System;
class Algorithm
{
public static int[] numbers= new int[50];
static void Main()
{
Algorithm a=new Algorithm();
Random randNum = new Random();
for (int i = 0; i < numbers.Length; i++) // read random no
{
numbers[i] = randNum.Next(1, 100);
}
a.display(); // call method
Console.WriteLine( "\n How many maximum values you wanted to find in this array: ");
int n = Convert.ToInt32(Console.ReadLine());
a.findMaximum(numbers,n);
}
public void display()
{
Console.WriteLine("Original Array \n"); // display random numbers
for (int i = 0; i < numbers.Length; i++)
{
Console.Write(" "+numbers[i]);
}
}
public void findMaximum (int[] numbers,int n)
{
int[] result = new int[n];
for (int i = 0; i < numbers.Length; i++)
{
//if bigger than the smallest node
if (numbers[i] <= result[0])
{
continue;
}
else
{
//if bigger than all
if (numbers[i] > result[n - 1])
{
for (int l = 0; l < n - 1; l++)
{
result[l] = result[l + 1];
}
result[n - 1] = numbers[i];
}
else
{
int indexLeft = 0;
int indexRight = n - 1;
int currIndex = (indexRight + indexLeft) / 2;
while (indexRight - indexLeft > 1)
{
if (numbers[i] >= result[currIndex])
{
indexLeft = currIndex;
}
else
{
indexRight = currIndex;
}
currIndex = (indexRight + indexLeft) / 2;
}
for (int l = 0; l < currIndex; l++)
{
result[l] = result[l + 1];
}
result[currIndex] = numbers[i];
}
}
}
Console.WriteLine("{0} Maximum Values found are:",n);
Array.Reverse(result);
for (int l = 0; l<result.Length;l++)
{
Console.Write(" " +result[l]);
}
}
}
Getting Stuck With Similar COMP 6211 Algorithms And Data Structures Assignment? Enrol With Expertsminds's COMP 6211 Algorithms And Data Structures Assignment Help Services And Get Distressed With Your Assignment Worries!
Question 2 - When using a sequential search algorithm (such as linear) to find a value, it will always find the first occurrence of that item in the array.
Extend the program and class created in question 1 by adding a NumOccuranceSearch method, which is a modified sequential search that receives three parameters: an array, an integer (the value the user wants to search for i.e. 4) and a second integer (the occurrence of the item they want to search for i.e. 2nd).
Answer -
using System;
class Algorithm
{
public static int[] numbers= {2, 4, 8, 6, 18, 4, 9, 4, 3}; // initialize array
static void Main()
{
Algorithm a=new Algorithm(); // create object for class to call methods
Console.WriteLine("Original Array");
for (int i = 0; i < numbers.Length; i++) // print original array values
{
Console.Write(" "+numbers[i]);
}
Console.WriteLine("\n What Value you wanted to find:"); // read finding values
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\n Which Occurrence you wanted to find:"); // read Occurrence value
int occ = Convert.ToInt32(Console.ReadLine());
int index=a.NumOccuranceSearch(numbers,n,occ); // call method
Console.WriteLine("The item {0} has occurrence {1} at index: {2}",n,occ,index); // print result
}
public int NumOccuranceSearch (int [] numbers,int n,int occ)
{
int pos = 0; int count=0;
for (int k = 0; k<numbers.Length; k++) // loop to pick each value from array
{
if (numbers[k] == n) // check each value from array to given number
{
pos = k; // read position
count++; // increament count to check number of occurrence
if(count==occ)
{
break; // if match no of occurrence with count then stop
}
}
}
return pos; // return position
}
}
Order New Copy Of COMP 6211 Algorithms And Data Structures Assignment And Secure Higher Marks!
Question 3 - For this question you are required to extend the program and class even further by adding a LastOccuranceSearch method, which finds the last occurrence of the searched item.
Answer -
using System;
class Algorithm
{
public static int[] numbers= {2, 4, 8, 6, 18, 4, 9, 4, 3};
static void Main()
{
Algorithm a=new Algorithm(); // print array
Console.WriteLine("Original Array");
for (int i = 0; i < numbers.Length; i++)
{
Console.Write(" "+numbers[i]);
}
Console.WriteLine("\n What Value you wanted to find:"); // read input
int n = Convert.ToInt32(Console.ReadLine());
int index=a.LastOccuranceSearch(numbers,n);
Console.WriteLine("The item {0} has occurance at index {1}",n,index); // print result
}
public int LastOccuranceSearch (int [] numbers,int n)
{
int pos = 0;
for (int k = numbers.Length -1; k >= 0; k--) // check in reverse order to find last occurance
{
if (numbers[k] == n) // compare with given no
{
pos = k; // read position
break;
}
}
return pos; // return position
}
}
Never Be Caught In Plagiarism, Avail COMP 6211 Algorithms And Data Structures Assignment Help Service Of Expertsminds.Com And Save Higher Marks!
Question 4 - The challenge for this question is to look at improving the bubble sort. Consider the following: You will notice after the first pass of the algorithm the highest number is "in place", after the second pass the two highest numbers are "in place" and so on. Consider as the array elements are moved so the highest numbers in place, do we need to continue to make 49 passes of a 50 element array? Or can this be reduced?
For this question you are required to extend your program and class again by adding the following two methods: bubbleSort () and improvedBubbleSort ().
Answer -
using System;
using System.Diagnostics;
class Algorithm
{
public static int[] numbers= {2, 4, 8, 6, 18, 4, 9, 4, 3}; // initialize array
static void Main()
{
Algorithm a=new Algorithm();
Console.WriteLine("Original Array"); // print array
for (int i = 0; i < numbers.Length; i++)
{
Console.Write(" "+numbers[i]);
}
a.bubbleSort(numbers); // call methods
a.improvedBubbleSort(numbers);
}
public void bubbleSort (int [] numbers)
{
Stopwatch sw = Stopwatch.StartNew(); // create object for Stopwatch class
int t;
for (int j = 0; j <= numbers.Length - 2; j++) // no of passes
{
for (int i = 0; i <= numbers.Length - 2; i++) // no of comparision
{
if (numbers[i] > numbers[i + 1]) // comparison to do swapping
{
t = numbers[i + 1]; // swapping
numbers[i + 1] = numbers[i];
numbers[i] = t;
}
}
}
sw.Stop(); // stop watch to calculate execution time for above algorithm
Console.WriteLine("\n Bubble Sorted array is :");
for (int i = 0; i < numbers.Length; i++) // print sorted array
{
Console.Write(" "+numbers[i]);
}
Console.WriteLine(" \n Time taken for Bubble Sort: {0} ms", sw.Elapsed.TotalMilliseconds);
}
public void improvedBubbleSort (int [] numbers)
{
Stopwatch sw = Stopwatch.StartNew();
int temp = 0;
bool swap = false;
for(int i = 0 ; i < numbers.Length ; i++)
{
swap = false;
for(int j = 0 ; j< numbers.Length - i-1 ; j++) // reduced no of passes
{
if(numbers[j] > numbers[j+1])
{
temp = numbers[j];
numbers[j] = numbers[j+1];
numbers[j+1]=temp;
swap=true;
}
}
if(!swap)
break;
}
sw.Stop();
Console.WriteLine("\n Improved Bubble Sorted array is :"); // print array with execution time
for (int i = 0; i < numbers.Length; i++)
{
Console.Write(" "+numbers[i]);
}
Console.WriteLine(" \n Time taken for Improved Bubble Sort: {0} ms", sw.Elapsed.TotalMilliseconds);
}
}
Avail the best Toi Ohomai Institute of Technology, New Zealand assignment help service offered for different courses and units, such as:-
- Information Technology Operations Assignment Help
- Fundamentals of Programming and Problem Solving Assignment Help
- Professional Practice Assignment Help
- Enterprise Assignment Help
- Database Administration Assignment Help
- Routing and Switching Assignment Help
- Scaling Networks Assignment Help
- Connecting Networks Assignment Help
- Large Network Technologies Assignment Help
- System Security Assignment Help
- Cyber Security Assignment Help
- Data Management Assignment Help