Find minimum (Smallest) element in Array

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends today we are going to see how to find out the minimum element in an atom so let's see see this is the array and we want to find out the minimum value in this array this array contains all integer values okay so the logic is very simple you have to trace the array by using variable I means you have to visit every element in this array and the eighth element is the current element okay so see first you have to declare the minimum element as the first element in the end so minimum will become five in this case okay so the minimum element is the first element in the array and after we declare the minimum element to the first element of the array you have to update the minimum variable okay now let's go and see the function here how it is done so the function name is minimum and we are passing two variables that is the array and the size of the array in this case the size of the array is 7 now I is the variable declared to trace this array and the minimum variable is declared and as you know the first value in the array that is the 0th index of the array that element is assigned to the minimum variable now let's start the for loop from the first index to the last index that is n now see for I equal to 1 means I will be 1 now so if array of I is less than minimum means you have to check whether the current element is less than the minimum variable ok this variable minimum is holding the value 5 now so you have to check whether the current element 3 is less than 5 see here 3 is less than 5 yes it is so this if is true and we go inside this if condition okay and that current element becomes the new minimum inside the if condition so see here this is the rule if current is less than minimum then that current element becomes the minimum okay means that value of the current element is assigned to the minimum variable so see because 3 is less than 5 now 3 will be assigned to the minimum variable okay so this variable is updated and now minimum is 3 right and we go to the next iteration of for loop now I plus plus means I will become 2 now so see whether 8 here if array of I that is 8 is less than minimum now minimum is 3 okay so whether 8 is less than 3 no it is not so we don't go inside this if condition we just directly go to the next iteration so I plus plus means we go to the next element now check - so whether 2 that is area of I is less than 3 that is the minimum yes 2 is less than 3 so 2 will be assigned to the minimum variable now means the current element 2 will become the minimum element so this variable is updated to 2 and we go to the next iteration so I is incremented check whether 1 is less than 2 see whether one here is less than the minimum variable minimum variable value is 2 now so whether 1 is less than 2 yes it is so 1 is assigned to the minimum variable so minimum becomes 1 we go to the next iteration of our loop so I is incremented so check whether 9 is less than 1 no it is not so we directly increment I check whether 4 is less than 1 no it is not so we directly increment I now I becomes 7 ok now see I is less than n so currently I is 7 and n is 7 and 7 is not less than 7 so this condition fails and we come out of this or loop ok and after we come out of this for loop we return the minimum value this minimum variable value so 1 is the minimum element in this array so this is how we find out the minimum in n time complexity in the worst case hey friends please subscribe to my channel as I post algorithm videos every day and if you want a video on any particular topic then please mention in the comment below thank you
Info
Channel: Vivekanand Khyade - Algorithm Every Day
Views: 61,365
Rating: undefined out of 5
Keywords: find minimum element in array, code, algorithm, program, computer science, information technology, placement, interview, question
Id: fZywNeNP0Yk
Channel Id: undefined
Length: 5min 38sec (338 seconds)
Published: Sun Apr 15 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.