Searching/sorting tasks and efficiency analysis - Big-oh
For each problem given below, do the following:
1. Create an algorithm in pseudocode to solve the problem.
2. Identify the factors that would influence the running time of
your algorithm. For example, if your algorithm is to search an
array the factor that influences the running time is the array
size. Assign names (such as n) to each factor.
3. Count the operations performed by the algorithm. Express the
count as a function of the factors you identified in Step 2. To do
that, identify the basic operations of the algorithm. There is no
need count every statement separately only the ones that will
influence the running time.
4. Describe the best case scenario for the algorithm and derive the
big O.
5. Describe the worst case scenario for the algorithm and derive
the big O.
The problems are:
a. Determine if two arrays have no elements in common.
b. Counting the total number of characters that have a duplicate
within a string. (i.e. ”gigi the gato” would result in 7 (g x 3 + i
x 2 + t x 2)
c. Finding a row where every entry is ’x’ in a 2-D array.
a. Determine if two arrays have no elements in common
1. Solution Pseudo Code:
INITIALIZE commonElementFound = False
FOR index1 FROM 0 -> length(Array1):
FOR index2 FROM 0-> length(Array2):
IF Array1[index1] == Array2[index2] :
commonElementFound = True
BREAK
ENDIF
ENDFOR
ENDFOR
IF commonElementFound == True:
PRINT " Two Arrays have common elements "
ELSE :
PRINT " Two Arrays have no elements in common"
2. Factors that would influence the running time of algorithm are length of two arrays, naming the factors as "m" and "n"
3. Each step of Pseudocode takes exactly one operation except for loop, where outer for loop is taking m times inner foor loop operations, and inner for loop takes n operations, so loops are taking m*n operations totally
4. The algorithm totally takes m*n operations
5. Best case scenario of the algorithm is when the first element of two arrays are same. So Best case has complexity of big O(1)
6. Worst case scenario is no elements are common in each of the array or last element of array 1 is mathced with last element of array 2 giving the complexity big O(m*n)
----------------------------------------------------------
b. Counting the total number of characters that have a duplicate within a string.
1. Solution Pseudo Code:
INITIALIZE duplicateCharacters = 0
FUNCTION countOccurences( char, String ):
INITIALIZE count = 0
FOR index FROM 0 -> length(String):
IF String[index] == char:
count++
RETURN count
FOR i FROM 0 -> length(String):
count = countOccurences(String[i], String)
IF (count >1):
duplicateCharacters += count
ENDFOR
IF duplicateCharacters == 0:
PRINT " The string doesn't have any duplicate "
ELSE :
PRINT " Total number of characters that have a duplicate within a
string are:", duplicateCharacters
2. Factor that would influence the running time of algorithm is
length of the string, naming the factor as "n"
3. Each step of Pseudocode takes exactly one operation and for
function, it will take as many steps as length of the string.
4. The algorithm totally takes n*n operations.
5. and 6. The best and worst cases are same since the loops are not
breaking based on any condition the total time taken by the
algorithm is best and worst case so big O(n2) is the complexity
---------------------------------------------------
c. Finding a row where every element is ' x ' in a 2-D array.
1.solution pseudo code :
INITIALIZE foundRow = False ,count = 0,
ROWS = length(array)
COLUMNS = length(array[0])
FOR row FROM 0 -> ROWS:
FOR column FROM 0->COLUMNS :
notMatched = False
IF x != array[row][column] :
notMatched = True
foundRow = True
break
ENDIF
ENDFOR
IF notMatched = False:
print " Row where every element is 'x' is", row
commonRow = True
ENDIF
ENDFOR
IF foundRow == False:
print " No such row exists"
2. Factors that would influence the running time of algorithm are rows and column sizes of matrix. Naming them m for rows and n for columns
3. Each step of Pseudocode takes exactly one unit operation except for loop, where outer for loop is taking m times inner foor loop operations, and inner for loop takes n operations, so loops are taking m*n operations totally
4. The algorithm totally takes m*n operations
5. Best case scenario of the algorithm is when the first element of each row is not ‘x’. and loop just breaks by checking that row. So Best case has complexity of big O(m)
6. Worst case scenario is no elements is ‘x’ in each of the row or last element of each row is not ‘x’ but all starting ones are ‘x’s, giving the complexity big O(m*n)
Searching/sorting tasks and efficiency analysis - Big-oh For each problem given below, do the following: 1....
Answer B java Problem 2 For each problem given below, do the following: 1. Create an algorithm in pseudocode to solve the problem. 2.Identify the factors that would influence the running time of your algorithm. For example, if your algorithm is to search an array the factor that influences the running time is the array size. Assign names (such as n) to each factor. 3. Determine the number of operations in each step of the pseudocode. To do that, identify...
S19-A6.pdf 2/5 Rutgers University CS111 - Inlroduction to Computer Science Spring 2019 Problem 5 Big-oh For cach problen given below, do lhe following 1. Creste a algrithm in psudood to solve the proble 2. Identify the factors that would influence the running time of your algorithm. For exarple, if your alorithm ia to search an array th fator that influeuxs the runnin time is the array size. Assign names (such as n) to each factor. 3. Count the operations performed...
For each problems segment given below, do the following: Create an algorithm to solve the problem Identify the factors that would influence the running time, and which can be known before the algorithm or code is executed. Assign names (such as n) to each factor. Identify the operations that must be counted. You need not count every statement separately. If a group of statements always executes together, treat the group as a single unit. If a method is called, and...
2.1 Searching and Sorting- 5 points each 1. Run Heapsort on the following array: A (7,3, 9, 4, 2,5, 6, 1,8) 2. Run merge sort on the same array. 3. What is the worst case for quick sort? What is the worst case time com- plexity for quick sort and why? Explain what modifications we can make to quick sort to make it run faster, and why this helps. 4. Gi pseudocode for an algorithm that will solve the following...
please I need it urgent thanks algorithms 2.1 Searching and Sorting- 5 points each 3. What is the worst case for quick sort? What is the worst case time com- plexity for quick sort and why? Explain what modifications we can make to quick sort to make it run faster, and why this helps. 4. Give pseudocode for an algorithm that will solve the following problem. Given an array AlL..n) that contains every number between 1 and n +1 in...
9-2. For each of the following problems: design a reduction algorithm a hash table or sorting algorithm that solves the problem; describe your algorithm with clear pseudocode; and prove the time efficiency class of your algorithm. duplicate search problem input: a vector V of comparable objects output: an element of V that appears more than once in V, or None if no such element exists 9-2. For each of the following problems: design a reduction algorithm a hash table or...
Subject: Algorithm need this urgent please. 2.1 Searching and Sorting- 5 points each 1. Run Heapsort on the following array: A 17, 3, 9, 4, 2,5, 6, 1,8) 2. Run merge sort on the same array 3. What is the worst case for quick sort? What is the worst case time com- plexity for quick sort and why? Explain what modifications we can make to quick sort to make it run faster, and why this helps. 2.1 Searching and Sorting-...
1. [5 marks Show the following hold using the definition of Big Oh: a) 2 mark 1729 is O(1) b) 3 marks 2n2-4n -3 is O(n2) 2. [3 marks] Using the definition of Big-Oh, prove that 2n2(n 1) is not O(n2) 3. 6 marks Let f(n),g(n), h(n) be complexity functions. Using the definition of Big-Oh, prove the following two claims a) 3 marks Let k be a positive real constant and f(n) is O(g(n)), then k f(n) is O(g(n)) b)...
Project 2 Sorting, CPU Time, and Big O Analysis Note: you will be using Microsoft Visual Studios 2019 as the compiler. This is an individual assignment. You will have lab time to work on this assignment as well as homework time. Each person will analyze two sorts. The first sort will be either bubble sort or insertion sort. The second sort will be either quick sort or merge sort. Create two projects, one for each sort in the given choices....
4. Big-Oh and Rune time Analysis: describe the worst case running time of the following pseudocode functions in Big-Oh notation in terms of the variable n. howing your work is not required (although showing work may allow some partial t in the case your answer is wrong-don't spend a lot of time showing your work.). You MUST choose your answer from the following (not given in any particular order), each of which could be re-used (could be the answer for...