-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortSubArrayToSortWholeArray.java
97 lines (65 loc) · 1.75 KB
/
SortSubArrayToSortWholeArray.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* Read inputSTDIN. Print your output to STDOUT*/
import java.util.*;
import java.*;
import java.io.*;
public class SortSubArrayToSortWholeArray {
public static void findSubarrayToSort(int array[], int size){
int left , right,max,min,i;
// left traverse
for(left=0;left<size-1;left++)
{if(array[left]>array[left+1])
break;
}
if(left==size-1)
{
System.out.print("Array is Sorted");
return;
}
// right traverse
for(right=size-1 ; right > 0; right--)
{ if(array[right] < array[right-1])
break;
}
// max & min
max=array[left];
min=array[right];
for(i=left+1;i<=right;i++)
{if(array[i] < min)
{ min=array[i]; }
if (array[i] > max){
max=array[i];
}
}
// traversing left to right to find no.smaller than max
for(i=size-1;i>= right+1;i--){
if(array[i]< max){
right=i;
break;
}
}
for(i=0;i<left;i++){
if(array[i]>min){
left=i;
break;
}
}
//System.out.println("left : "+ left +" right : " +right);
for(i=left;i<=right;i++){
System.out.print(array[i]);
if ( i>=0 && i < right)
System.out.print("||");
}
}
public static void main(String args[] ) throws Exception {
//Write code here
Scanner sc=new Scanner(System.in);
System.out.print("Enter Size : ");
int mSize=sc.nextInt();
int[] array=new int[mSize];
System.out.print("Values : ");
for(int i=0;i<array.length;i++){
array[i]=sc.nextInt();
}
findSubarrayToSort(array, mSize);
}
}