forked from MakeContributions/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(CPlusPlus): add index of smallest element in array (MakeContrib…
…utions#584) Co-authored-by: Ming Tsai <[email protected]>
- Loading branch information
1 parent
c152e76
commit 2dddebc
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
algorithms/CPlusPlus/Arrays/index-of-smallest-element-of-array.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//index of smallest element of array | ||
/* Example : | ||
size of array =5 | ||
Let array be ,arr[5]={9,4,1,5,8} | ||
here smallest element of array is 1 which is at index 2 of array | ||
output = 2 that is index of smallest element of array. | ||
Time complexity = O(n) | ||
*/ | ||
|
||
#include<iostream> | ||
using namespace std; | ||
int main() | ||
{ | ||
int size,j; | ||
cout<<"enter the size of array"; | ||
cin>>size; | ||
int arr[size]; | ||
for(int i=0;i<size;i++) | ||
{ | ||
cin>>arr[i]; | ||
} | ||
//considering the element at zero index to be smallest | ||
int min=arr[0]; | ||
int smallest_index=0; | ||
//comparing min to all other elements | ||
for(j=0;j<size;j++) | ||
{ | ||
if(min>arr[j]) | ||
{ | ||
min=arr[j]; | ||
smallest_index=j; | ||
} | ||
} | ||
cout<<"index of smallest element of array is"<<smallest_index; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters