what is the algorithm for finding the largest element in an unsorted integer array?
?
2009-11-21 04:54:36 UTC
what is the algorithm for finding the largest element in an unsorted integer array?
Six answers:
SPB
2009-11-21 04:58:55 UTC
int max
max = 0
for the size of the array
if array[i] > max
max = array[i]
end for
2009-11-21 05:09:13 UTC
SPB's answer won't work except in the special case where you have a non-empty array and non of the elements are -ve. Close though
Modifying SPB's algorithm ...
precondition: array not empty
generate exception
initialize max to the first element in the array
harsha deep
2009-11-21 06:10:22 UTC
Function MAX( n, x[] )
best := x1
for j := 2 to n do
if xj > best then best := xj
return best
If u just want the largest element without bothering about how algo is implemented , use C++ STL library which has a header called algorithm.h
In it ther is a function called max_element which gives max element out of a range.
jaffar
2016-12-14 21:26:21 UTC
#incorporate employing namespace std; int significant(){ //it extremely is an int via fact i like to return 0. int alphabet[]={12, 2, 7, 6,39}; //hi, i wanted an array. int SizeOf=5; //it extremely is often extra effective to assign length of array to a variable // or macro int Least; // to hold the smallest huge style. Least=alphabet[0]; //No braces via fact i'm purely repeating one //attempt/action. for (int i=a million;i<=SizeOf;i++) if (alphabet[i]
2016-12-09 00:22:26 UTC
#contain utilising namespace std; int considerable(){ //that's an int as a results of fact i like to return 0. int alphabet[]={12, 2, 7, 6,39}; //whats up, i needed an array. int SizeOf=5; //it is regularly greater useful to assign length of array to a variable // or macro int Least; // to hold the smallest extensive style. Least=alphabet[0]; //No braces as a results of fact i'm only repeating one //try/action. for (int i=a million;i<=SizeOf;i++) if (alphabet[i]