Finding smallest value in an array most efficiently

后端 未结 13 1638
慢半拍i
慢半拍i 2020-11-29 07:02

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

相关标签:
13条回答
  • 2020-11-29 07:48

    Procedure:

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


    C++ implementation

      #include<bits/stdc++.h>
      using namespace std;
    
      int main()
      {
         int num;
         cin>>num;
         int arr[10];
    
         for(int i=0; i<num; i++)
         {
           cin>>arr[i];
         }
    
    
        cout<<*min_element(arr,arr+num)<<endl;
    
        return 0;
      }
    
    0 讨论(0)
  • 2020-11-29 07:50
                    //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);
    
    0 讨论(0)
  • 2020-11-29 07:54

    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) {
            smallest = array[i];
        }
    }
    
    0 讨论(0)
  • 2020-11-29 07:54
    int small=a[0];
    for (int x: a.length)
    {
        if(a[x]<small)
            small=a[x];
    }
    
    0 讨论(0)
  • 2020-11-29 07:58

    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 structure.

    A heap will be faster than doing a sort on the list but the tradeoff is you can only find the minimum.

    0 讨论(0)
  • 2020-11-29 07:59

    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 based indexing). If the array is sorted in descending order then the last element is the smallest element,you can get it by arr[sizeOfArray-1].

    If the array is not sorted then you have to iterate over the array to get the smallest element.In this case time complexity is O(n), here n is the size of array.

    int arr[] = {5,7,9,0,-3,2,3,4,56,-7};
    int smallest_element=arr[0] //let, first element is the smallest one
    
    for(int i =1;i<sizeOfArray;i++)  
    {
        if(arr[i]<smallest_element)
        {
         smallest_element=arr[i];
        }
    }
    

    You can calculate it in input section (when you have to find smallest element from a given array)

    int smallest_element;
    int arr[100],n;
    cin>>n;
    for(int i = 0;i<n;i++)
    {
    cin>>arr[i];
    if(i==0)
    {
        smallest_element=arr[i]; //smallest_element=arr[0];
    }
    else if(arr[i]<smallest_element)
    {
    smallest_element = arr[i];
    }
    }
    

    Also you can get smallest element by built in function

    #inclue<algorithm>
    int smallest_element = *min_element(arr,arr+n); //here n is the size of array
    

    You can get smallest element of any range by using this function such as,

    int arr[] = {3,2,1,-1,-2,-3};
    cout<<*min_element(arr,arr+3); //this will print 1,smallest element of first three element
    cout<<*min_element(arr+2,arr+5); // -2, smallest element between third and fifth element (inclusive) 
    

    I have used asterisk (*), before min_element() function. Because it returns pointer of smallest element. All codes are in c++. You can find the maximum element in opposite way.

    0 讨论(0)
提交回复
热议问题