Pseudocode and Flowchart for finding the largest element in an Array[35188 views] Show Array is a data structure consisting of a collection of homogenous data type or element (values or variables), each identified by at least one array index or key. An array is stored in such a manner that the position of each element can be found out from its index tuple by using a mathematical formula. The simplest data structure is a linear array, also known as one-dimensional array. Arrays are among the most important data structures and are used in almost every program. They can be also used to implement many other data structures, such as linked-lists and stacks. The most important fact is that arrays are linear datatypes in which data is stored in sequence. In many internal and external storage devices, the memory is a one-dimensional array of words, whose indices are their addresses. We can access the elements if we know their indexes directly so, we can say that random access is possible in array. Flowchart for finding the largest element in an Array :Remove WaterMark from Above FlowchartAlgorithm to find the largest element in an Array :Step 1: Start Step 2: Declare array a[n] and variable i, large Step 3: Read n from User and read all the elements of a[n] Step 4: Initialize Variable i=1 and large=a[0] Step 5: Repeat Until i<=n-1 5.1 if(a[i]>large), set large=a[i] 5.2 increment i=i+1 Step 6: Print "The largest element is": large Step 7: Stop In the above algorithm,
Updated on 10 MARCH, 2021 by Shaddy Efficient Approach
Time complexity: O(N) where there are N elements in the array k largest(or smallest) elements in an arrayQuestion: Write an efficient program for printing k largest elements in an array. Elements in an array can be in any order. Recommended: Please solve it on “PRACTICE” first, before moving on to the solution. Method 1 (Use Bubble k times) Like Bubble sort, other sorting algorithms like Selection Sort can also be modified to get the k largest elements. Method 2 (Use temporary array) 1) Store the first k elements in a temporary array temp[0..k-1]. Time Complexity: O((n-k)*k). If we want the output sorted then O((n-k)*k + k*log(k)) Method 3(Use Sorting) Following is the implementation of the above. C++
Java
Python
C#
PHP
Javascript
Output
50 30 23
Time complexity: O(n*log(n)) Method 4 (Use Max Heap) Time complexity: O(n + k*log(n)) Method 5(Use Order Statistics) Time complexity: O(n) if we don’t need the sorted output, otherwise O(n+k*log(k)) Method 6 (Use Min Heap) All of the above methods can also be used to find the kth largest (or smallest) element. C++
Java
Python3
C#
Javascript
Output
50 88 96
Method 7(Using Quick Sort partitioning algorithm):
We can improve on the standard quicksort algorithm by using the random() function. Instead of using the pivot element as the last element, we can randomly choose the pivot element. The worst-case time complexity of this version is O(n2) and the average time complexity is O(n). Following is the implementation of the above algorithm: C++
Output
3 smallest elements are : 3 2 1
3 largest elements are : 96 50 88
Please write comments if you find any of the above explanations/algorithms incorrect, or find better ways to solve the same problem.
Article Tags :
Arrays Heap Searching Sorting
Amazon Microsoft Order-Statistics Samsung Walmart Practice Tags :
Amazon Microsoft Samsung Walmart Arrays Searching Sorting Heap Find the minimum and maximum value in an arrayDifficulty: Medium Asked in: Facebook Understanding the ProblemProblem Description: Given an array A[] of size n, you need to find the maximum and minimum element present in the array. Your algorithm should make the minimum number of comparisons. For Example: Input: A[] = { 4, 2, 0, 8, 20, 9, 2} Output: Maximum: 20, Minimum: 0 Input: A[] = {-8, -3, -10, -32, -1} Output: Maximum: -1, Minimum: -32 Possible follow-up questions to ask the interviewer :-
Problem Note:- The interviewer would not judge your algorithm for this question based on the time complexity as all solution has the time complexity of O(n). The bottleneck parameter in this problem is the number of comparisons that your algorithm requires to determine the maximum and the minimum element. You need to decrease the number of comparisons as much as you can. Solutions
1. Searching linearly: Increment the loop by 1We initialize both minimum and maximum element to the first element and then traverse the array, comparing each element and update minimum and maximum whenever necessary. Pseudo-Codeint[] getMinMax(int A[], int n) { int max = A[0] int min = A[0] for ( i = 1 to n-1 ) { if ( A[i] > max ) max = A[i] else if ( A[i] < min ) min = A[i] } // By convention, let ans[0] = maximum and ans[1] = minimum int ans[2] = {max, min} return ans } Complexity Analysis At every step of the loop, we are doing 2 comparisons in the worst case. Total no. of comparisons (in worst case) = 2*(n-1) = 2n - 2 Time complexity = O(n), Space complexity = O(1) In the best case, a total of n-1 comparisons have been made. (How?) Critical ideas to think!
2. Divide and Conquer : Tournament MethodAnother way to do this could be by following the divide and conquer strategy. Just like the merge sort, we could divide the array into two equal parts and recursively find the maximum and minimum of those parts. After this, compare the maximum and minimum of those parts to get the maximum and minimum of the whole array. Solution Steps
3. The recursive part is
4. Return max and min. Pseudo Codeint[] findMinMax(int A[], int start, int end) { int max; int min; if ( start == end ) { max = A[start] min = A[start] } else if ( start + 1 == end ) { if ( A[start] < A[end] ) { max = A[end] min = A[start] } else { max = A[start] min = A[end] } } else { int mid = start + (end - start)/2 int left[] = findMinMax(A, start, mid) int right[] = findMinMax(A, mid+1, end) if ( left[0] > right[0] ) max = left[0] else max = right[0] if ( left[1] < right[1] ) min = left[1] else min = right[1] } // By convention, we assume ans[0] as max and ans[1] as min int ans[2] = {max, min} return ans } Complexity Analysis For counting the number of comparisons, since this is a recursive function, let us define the recurrence relation : T(n) = 2 T(n/2) + 2 T(2) = 1 T(1) = 0 We can solve this recurrence relation by master method/recursion tree method. if n is a power of 2 T(n) = 3n/2 - 2Time complexity = O(n) and space complexity = O(logn) (For recursion call stack) If n is a power of 2, the algorithm needs exactly 3n/2–2 comparisons to find min and max. If it's not a power of 2, it will take a few more(not significant). Critical ideas to think!
3. Comparison in Pairs : Increment the loop by 2In this approach, we pick array elements in pairs and update the min and max. If the array size is odd, we initialize the first element as both min and max, and if it's even, we compare the first two elements and initialize min and max accordingly. Solution Steps
3. Traverse the array in pairs 4. For each pair, compare the two elements and then
5. Return max and min. Pseudo Codeint[] findMinMax(int A[], int n) { int max, min int i if ( n is odd ) { max = A[0] min = A[0] i = 1 } else { if ( A[0] < A[1] ) { max = A[1] min = A[0] } else { max = A[0] min = A[1] } i = 2 } while ( i < n ) { if ( A[i] < A[i+1] ) { if ( A[i] < min ) min = A[i] if ( A[i+1] > max ) max = A[i+1] } else { if ( A[i] > max ) max = A[i] if ( A[i+1] < min ) min = A[i+1] } i = i + 2 } // By convention, we assume ans[0] as max and ans[1] as min int ans[2] = {max, min} return ans } Complexity Analysis Time Complexity is O(n) and Space Complexity is O(1). For each pair, there are a total of three comparisons, first among the elements of the pair and the other two with min and max. Total number of comparisons:-
Critical ideas to think!
Comparison of different solutionsSuggested Problems to solve
Please write comments if you find any error or bug. Happy Coding! Enjoy Algorithms! AfterAcademy Data Structure And Algorithms Online Course - Admissions Open |