0
Smallest element of an array
Can someone explain this to me, I`m new so it is a bit confusing. Thank you Algorithm STEP 1: START STEP 2: Initially set the first element as minimum element min = arr[0] STEP 3: Set i = 0 STEP 4: Check if min > arr[i] STEP 5: If step 4 is true set min = arr[i] and i = i STEP 6: If step 4 is false set i = i + 1 STEP 7: If i < n, go to step 4 else display min as the smallest element. STEP 8: End Program
2 Answers
0
Let's make that with an example you have an Array with {3, 4, 2, 5} at start you set Array [0] also 3 as min. but then you go to the next value and when than value is lower than the former Minimum set it as the new min.
Note: i's only meant to get the index of the next value of the array.
min = Array [0] // 3
Array [i] < min ? // Currently it's arr [0] so it's the same. i increase by 1
...
That goes until:
Array [i] < min ? //Now we are by i=2 skipped 1 and we have the value 2 which's lower than 3.
min = Array[i] // 2
And then that until we reach the Array end.