-
Notifications
You must be signed in to change notification settings - Fork 0
/
Insertion_Sort_Unique.java
63 lines (51 loc) · 1.55 KB
/
Insertion_Sort_Unique.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class Insertion_Sort_Unique {
public Insertion_Sort_Unique() throws FileNotFoundException {
}
public static void main(String[] args) throws Exception {
long start2 = System.currentTimeMillis();
File fis = new File("G:\\Unique.txt");
File f = new File("G:\\Unique_Insertion_Sort_new.txt");
ArrayList<Integer> s = new ArrayList<Integer>();
Scanner sc = new Scanner(fis);
while(sc.hasNextLine()){
int num = Integer.parseInt(sc.nextLine());
s.add(num);
}
System.out.println(s);
int[] arr = new int[s.size()];
for(int i=0;i<s.size();i++){
arr[i] = s.get(i);
}
for (int i = 0; i < arr.length; i++) {
int key = arr[i];
int j = i-1;
while(j>=0 && arr[j]>key){
arr[j+1] = arr[j];
j--;
}
arr[j+1]=key;
}
for (int k : arr) {
System.out.println(k);
}
if (f.createNewFile()){
System.out.println("File Created");
}
else{
System.out.println("File already exists");
}
FileWriter fw = new FileWriter(f.getAbsolutePath());
for (int j : arr) {
fw.write((j) + "\n ");
}
fw.close();
sc.close();
long end2 = System.currentTimeMillis();
System.out.println("Time taken to sort file in Insertion Sort: "+(end2-start2));
}
}