Skip to content

Commit b4f4dc8

Browse files
committed
Problem 066 Running Time Of Algorithm :: Calculating the number of ReAsign Operands inside Sorting by Insertion Algorithm
1 parent 32ce2dd commit b4f4dc8

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package hackerRank_JavaProblemSolving;
2+
3+
import java.io.BufferedWriter;
4+
import java.io.FileWriter;
5+
import java.io.IOException;
6+
import java.util.Scanner;
7+
8+
public class Problem066_RunningTimeOfAlgorithm__CountTheNumberOfReAssigning {
9+
10+
// HINTS: Analyzing the requirement, I came to conclusion that I need to calculate the reassigning number of operation
11+
static int runningTime(int[] A) {
12+
int count = 0;
13+
for(int i = 1; i < A.length; i++) {
14+
int value = A[i];
15+
int j = i - 1;
16+
while(j >= 0 && A[j] > value) {
17+
A[j + 1] = A[j];
18+
j--;
19+
count++;
20+
}
21+
A[j + 1] = value;
22+
}
23+
return count;
24+
}
25+
26+
private static final Scanner scanner = new Scanner(System.in);
27+
28+
public static void main(String[] args) throws IOException {
29+
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
30+
31+
int n = scanner.nextInt();
32+
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
33+
34+
int[] arr = new int[n];
35+
36+
String[] arrItems = scanner.nextLine().split(" ");
37+
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
38+
39+
for (int i = 0; i < n; i++) {
40+
int arrItem = Integer.parseInt(arrItems[i]);
41+
arr[i] = arrItem;
42+
}
43+
44+
int result = runningTime(arr);
45+
46+
bufferedWriter.write(String.valueOf(result));
47+
bufferedWriter.newLine();
48+
49+
bufferedWriter.close();
50+
51+
scanner.close();
52+
}
53+
}

0 commit comments

Comments
 (0)