Skip to content

Commit 4a55006

Browse files
authored
Create selectionsort.java
1 parent 951b3d3 commit 4a55006

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

selectionsort.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.io.*;
2+
import java.util.ArrayList;
3+
import java.util.Scanner;
4+
5+
// Java program for implementation of Selection Sort
6+
class SelectionSort
7+
{
8+
void sorting(ArrayList<Integer> arr)
9+
{
10+
int n = arr.size();
11+
for (int i = 0; i < n-1; i++)
12+
{
13+
int min_idx = i;
14+
for (int j = i+1; j < n; j++)
15+
if (arr.get(j) < arr.get(min_idx))
16+
min_idx = j;
17+
18+
int temp = arr.get(min_idx);
19+
arr.set(min_idx, arr.get(i));
20+
arr.set(i,temp);
21+
}
22+
}
23+
24+
public static void main(String args[]) throws IOException
25+
{
26+
SelectionSort obj = new SelectionSort();
27+
String pathToFile = "./Sorting/unsorted.txt";
28+
File unsorted = new File(pathToFile);
29+
Scanner sc = new Scanner(unsorted);
30+
sc.useDelimiter(",");
31+
ArrayList<Integer> arr = new ArrayList<Integer>();
32+
while(sc.hasNext()){
33+
arr.add(sc.nextInt());
34+
}
35+
obj.sorting(arr);
36+
System.out.println("Sorted array");
37+
38+
System.out.println( arr);
39+
sc.close();
40+
}
41+
}

0 commit comments

Comments
 (0)