-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection_sort.java
More file actions
37 lines (32 loc) · 799 Bytes
/
Copy pathselection_sort.java
File metadata and controls
37 lines (32 loc) · 799 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
// Simple Java code for Selection Sort
// Function to sort array (Selection sort)
public class selection_sort {
void sort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
{
int min = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min])
min = j;
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}
void printarray(int arr[]){
int n = arr.length;
for(int i=0;i<n;i++){
System.out.println(arr[i]+ " ");
System.out.println();
}
}
public static void main(String[] args) {
selection_sort ob = new selection_sort();
int arr[] = {64,25,12,11,22};
ob.sort(arr);
System.out.println("Sorted array");
ob.printarray(arr);
}
}