Added the code for Selection Sort in Java Language (#23465)

* Added the code for Selection Sort in  Java Language

* fix: code block formatting
This commit is contained in:
Achint Srivastava
2018-12-05 04:58:38 +05:30
committed by Tom
parent c97a834cc9
commit b939a3f915

View File

@ -76,6 +76,30 @@ def seletion_sort(arr):
min_i = j
arr[i], arr[min_i] = arr[min_i], arr[i]
```
### Implementation in Java
```java
public void selectionsort(int array[])
{
int n = array.length; //method to find length of array
for (int i = 0; i < n-1; i++)
{
int index = i;
int min = array[i]; // taking the min element as ith element of array
for (int j = i+1; j < n; j++)
{
if (array[j] < array[index])
{
index = j;
min = array[j];
}
}
int t = array[index]; //Interchange the places of the elements
array[index] = array[i];
array[i] = t;
}
}
```
### Implementation in MATLAB
```MATLAB