-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSelectionSortPedro.java
More file actions
37 lines (32 loc) · 974 Bytes
/
SelectionSortPedro.java
File metadata and controls
37 lines (32 loc) · 974 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class SelectionSortPedro
{
public static void selectionSort(int[] arr)
{
for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < arr.length; j++)
{
if (arr[j] < arr[index])
{
index = j;//searching for lowest index
}
}
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
}
public static void main(String a[])
{
int[] arr1 = {9,14,3,2,43,11,58,22};
System.out.println("Before Selection Sort");
for(int i:arr1)
System.out.print(i+" ");
System.out.println();
selectionSort(arr1);
System.out.println("After Selection Sort");
for(int i:arr1)
System.out.print(i+" ");
}
}