Quantcast
Channel: Finding smallest value in an array most efficiently - Stack Overflow
Browsing latest articles
Browse All 15 View Live

Answer by Harshit Dalal for Finding smallest value in an array most efficiently

C++ code#include <iostream>using namespace std;int main() { int n = 5; int arr[n] = {12,4,15,6,2}; int min = arr[0]; for (int i=1;i<n;i++){ if (min>arr[i]){ min = arr[i]; } } cout <<...

View Article



Answer by Taohidul Islam for Finding smallest value in an array most efficiently

If the array is sorted in ascending or descending order then you can find it with complexity O(1).For an array of ascending order the first element is the smallest element, you can get it by arr[0] (0...

View Article

Answer by rashedcs for Finding smallest value in an array most efficiently

Procedure:We can use min_element(array, array+size) function . But it iteratorthat return the address of minimum element . If we use *min_element(array, array+size) then it will return the minimum...

View Article

Answer by abiodun for Finding smallest value in an array most efficiently

int small=a[0];for (int x: a.length){ if(a[x]<small) small=a[x];}

View Article

Answer by Tomas Kubes for Finding smallest value in an array most efficiently

If you want to be really efficient and you have enough time to spent, use SIMD instruction.You can compare several pairs in one instruction:r0 := min(a0, b0)r1 := min(a1, b1)r2 := min(a2, b2)r3 :=...

View Article


Answer by Rampedi Tshepo for Finding smallest value in an array most efficiently

//smalest number in the array// double small = x[0]; for(t=0;t<x[t];t++) { if(x[t]<small) { small=x[t]; } } printf("\nThe smallest number is %0.2lf \n",small);

View Article

Answer by vknyvz for Finding smallest value in an array most efficiently

//find the min in an array list of #s$array = array(45,545,134,6735,545,23,434);$smallest = $array[0];for($i=1; $i<count($array); $i++){ if($array[$i] < $smallest){ echo $array[$i]; }}

View Article

Answer by Paul Chernoch for Finding smallest value in an array most efficiently

Richie's answer is close. It depends upon the language. Here is a good solution for java:int smallest = Integer.MAX_VALUE;int array[]; // Assume it is filled.int array_length = array.length;for (int i...

View Article


Answer by Michael for Finding smallest value in an array most efficiently

If you're developing some kind of your own array abstraction, you can get O(1) if you store smallest added value in additional attribute and compare it every time a new item is put into array.It should...

View Article


Answer by clemahieu for Finding smallest value in an array most efficiently

If finding the minimum is a one time thing, just iterate through the list and find the minimum.If finding the minimum is a very common thing and you only need to operate on the minimum, use a Heap data...

View Article

Answer by Daren Thomas for Finding smallest value in an array most efficiently

An O(1) sollution might be to just guess: The smallest number in your array will often be 0. 0 crops up everywhere. Given that you are only looking at unsigned numbers. But even then: 0 is good enough....

View Article

Answer by Totonga for Finding smallest value in an array most efficiently

The stl contains a bunch of methods that should be used dependent to the problem.std::findstd::find_ifstd::countstd::findstd::binary_searchstd::equal_rangestd::lower_boundstd::upper_boundNow it...

View Article

Answer by GManNickG for Finding smallest value in an array most efficiently

If they are unsorted, you can't do much but look at each one, which is O(N), and when you're done you'll know the minimum.Pseudo-code:small = <biggest value> // such as...

View Article


Answer by RichieHindle for Finding smallest value in an array most efficiently

You need too loop through the array, remembering the smallest value you've seen so far. Like this:int smallest = INT_MAX;for (int i = 0; i < array_length; i++) { if (array[i] < smallest) {...

View Article

Finding smallest value in an array most efficiently

There are N values in the array, and one of them is the smallest value. How can I find the smallest value most efficiently?

View Article

Browsing latest articles
Browse All 15 View Live




Latest Images