File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments