-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearMoveToHead.java
More file actions
31 lines (30 loc) · 866 Bytes
/
Copy pathLinearMoveToHead.java
File metadata and controls
31 lines (30 loc) · 866 Bytes
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
import java.util.*;
public class LinearMoveToHead{
public static int MoveToHead(int[] num , int key ){
int n=num.length;
for(int i=0;i<n;i++){
if(num[i]==key){
int temp = num [ i ];
num [ i] =num[0];
num[0]=temp;
return i;
}
}
return -1;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int[] num = new int[n];
for(int i=0;i<n;i++){
num[i]=sc.nextInt();
}
System.out.println("Enter the key to search");
int k =sc.nextInt();
System.out.println("\nMove to head array");
System.out.println(MoveToHead(num, k));
for(int i=0;i<n;i++){
System.out.print(num[i]+" ");
}
}
}