Question

Consider the function FINDDUPLICATES(A[0. 1) that returns a list of all duplicate items in the unsorted integer array A of si

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a)

Pre-Sorting based algorithm

  • INPUT arr = array of elements
  • arr = QuickSort(arr)
  • int rep = INT_MAX
  • rep_list = [ ]
  • FOR i from 1 to length(arr)-1:
  • if arr[ i ] == arr[ i+1 ] and arr[ i ] != rep:
  • rep_list.append ( arr[i] )
  • rep = arr[ i ]
  • print rep_list


b)

Hashing based algorithm:

  • INPUT arr = array of elements
  • SET max_element = maximum ( arr )
  • SET hash[ max_element ] = [0,0,....0]
  • rep_list = [ ]
  • FOR i from 1 to length( arr ) :
  • if hash[ arr[ i ] ] == 1:
  • rep_list.append( arr[ i ] )
  • else:
  • hash[ arr[ i ] ] = 1
  • print rep_list
Add a comment
Know the answer?
Add Answer to:
Consider the function FINDDUPLICATES(A[0. 1) that returns a list of all duplicate items in the unsorted integer array A...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider an array A[1...n] which is originally unsorted. One method to sort the array is as...

    Consider an array A[1...n] which is originally unsorted. One method to sort the array is as follows: First find the largest key in the unsorted portion of the array (initially the entire array s unsorted) and swap this with the last position in the unsorted section. This last position is now considered part of the sorted portion. The procedure is repeated until the entire array is sorted. (a) Write an algorithm to sort A as outlined above (in pseudo-code, no...

  • Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion...

    Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion, Quick, Merge). Take help from lecture slides and welb . You will then create arrays of different sizes as instructed below, test each algorithm on each array and record the execution times of each individual algorithm on each array. . You will report these execution times in a table and then write a report explaining the execution times of the sorting algorithms according to...

  • Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8...

    Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8 9 7 0 4 Pivot selection is defined to be the first element of each sub-list. Show the array before and after each quicksort round (when the array is partitioned after placing the pivot at its correct position). Also, clearly highlight the pivot in each partition b. Consider an unsorted array of integers of size n. Write a Java program to arrange the array...

  • I need help In the lecture you got acquainted with the median algorithm, which calculates the median of an unsorted array with n∈N elements in O (n). But the algorithm can actually do much more: it is...

    I need help In the lecture you got acquainted with the median algorithm, which calculates the median of an unsorted array with n∈N elements in O (n). But the algorithm can actually do much more: it is not limited to finding only the median, but can generally find the ith element with 0≤i <n. Implement this generic version of the median algorithm by creating a class selector in the ads.set2.select package and implementing the following method: /** * Returns the...

  • Please answer this in python pseudocode. It's an algorithm question. 1. [10 marks] Consider the function...

    Please answer this in python pseudocode. It's an algorithm question. 1. [10 marks] Consider the function SumKSmallest(A[0..n – 1), k) that returns the sum of the k smallest elements in an unsorted integer array A of size n. For example, given the array A=[6,-6,3,2,1,2,0,4,3,5] and k=3, the function should return -5. a. [3 marks) Write an algorithm in pseudocode for SumKSmallest using the brute force paradigm. Indicate and justify (within a few sentences) the time complexity of your algorithm. b....

  • In python Programming Exercise 1 Write a function that will have a list as an input,...

    In python Programming Exercise 1 Write a function that will have a list as an input, the task of the function is to check if all the elements in the list are unique,( i.e. no repetition of any value has occurred in the list), then the function returns true otherwise it returns false. Your program should include a main method that call the method to test it. Algorithm Analysis For the function you implemented in part 1, please calculate T(n),...

  • Problem 1 1. Consider the following function (K is the size of array A and L...

    Problem 1 1. Consider the following function (K is the size of array A and L is the size of array B) bool func (int A[], int K, int B[], int L, int start) { if (L > K-start) return false; for (int i =0; i < L; i++) { if(A[start+i] != B[i]) return false } return true; } What are the input and output variables to this function. Trace it for the following call: int F[] = {1, 3,...

  • Python program - Write a Python program, in a file called sortList.py, which, given a list...

    Python program - Write a Python program, in a file called sortList.py, which, given a list of names, sorts the names into alphabetical order. Use a one dimensional array to hold the list of names. To do the sorting use a simple sorting algorithm that repeatedly takes an element from the unsorted list and puts it in alphabetical order within the same list. Initially the entire list is unsorted. As each element is placed in alphabetical order, the elements in...

  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

  • Program this in C There is a lot of sorting algorithms. So far, we have covered...

    Program this in C There is a lot of sorting algorithms. So far, we have covered selection and insertion sort. However, we only covered how selection sort is implemented using an array. Every sorting algorithm implemented using an array can also be implemented using a linked list. In this assignment, you will implement the selection sort algorithm using a linked list instead of an array. You will first create an interface for handling string items. Basically, you will need to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT