-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathselectionsort.java
41 lines (37 loc) · 998 Bytes
/
selectionsort.java
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
38
39
40
41
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
// Java program for implementation of Selection Sort
class SelectionSort
{
void sorting(ArrayList<Integer> arr)
{
int n = arr.size();
for (int i = 0; i < n-1; i++)
{
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr.get(j) < arr.get(min_idx))
min_idx = j;
int temp = arr.get(min_idx);
arr.set(min_idx, arr.get(i));
arr.set(i,temp);
}
}
public static void main(String args[]) throws IOException
{
SelectionSort obj = new SelectionSort();
String pathToFile = "./Sorting/unsorted.txt";
File unsorted = new File(pathToFile);
Scanner sc = new Scanner(unsorted);
sc.useDelimiter(",");
ArrayList<Integer> arr = new ArrayList<Integer>();
while(sc.hasNext()){
arr.add(sc.nextInt());
}
obj.sorting(arr);
System.out.println("Sorted array");
System.out.println( arr);
sc.close();
}
}