-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathK_Sorted_Array.java
More file actions
55 lines (44 loc) · 1.12 KB
/
K_Sorted_Array.java
File metadata and controls
55 lines (44 loc) · 1.12 KB
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
/**
* @author SAGAR
*
*/
import java.util.Scanner;
public class K_Sorted_Array {
public static void main(String[] args) {
// TODO Auto-generated method stub
//System.out.println("Hello world");
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length of the array");
int n = sc.nextInt();
int array[] = new int[n];
for (int i = 0; i < n; i++) {
array[i] = sc.nextInt();
}
//System.out.println("Enter the Size of K-Sorted Array");
//int size = sc.nextInt();
k_Sorted_Array(array, n);
sc.close();
}
static void k_Sorted_Array(int A[], int size) {
int i, key, j;
for (i = 1; i < size; i++) {
key = A[i];
j = i - 1;
/*
* Move elements of A[0..i-1], that are greater than key, to one position ahead
* of their current position. This loop will run at most k times
*/
while (j >= 0 && A[j] > key) {
A[j + 1] = A[j];
j = j - 1;
}
A[j + 1] = key;
}
printArray(A);
}
static void printArray(int A[]) {
for (int i = 0; i < A.length; i++) {
System.out.print(A[i] + " ");
}
}
}