MCPcopy Index your code
hub / github.com/careercup/ctci / findUnsortedSequence

Method findUnsortedSequence

java/Chapter 17/Question17_6/Question.java:43–77  ·  view source on GitHub ↗
(int[] array)

Source from the content-addressed store, hash-verified

41 }
42
43 public static void findUnsortedSequence(int[] array) {
44 // find left subsequence
45 int end_left = findEndOfLeftSubsequence(array);
46
47 if (end_left >= array.length - 1) {
48 //System.out.println("The array is already sorted.");
49 return; // Already sorted
50 }
51
52 // find right subsequence
53 int start_right = findStartOfRightSubsequence(array);
54
55 int max_index = end_left; // max of left side
56 int min_index = start_right; // min of right side
57 for (int i = end_left + 1; i < start_right; i++) {
58 if (array[i] < array[min_index]) {
59 min_index = i;
60 }
61 if (array[i] > array[max_index]) {
62 max_index = i;
63 }
64 }
65
66 // slide left until less than array[min_index]
67 int left_index = shrinkLeft(array, min_index, end_left);
68
69 // slide right until greater than array[max_index]
70 int right_index = shrinkRight(array, max_index, start_right);
71
72 if (validate(array, left_index, right_index)) {
73 System.out.println("TRUE: " + left_index + " " + right_index);
74 } else {
75 System.out.println("FALSE: " + left_index + " " + right_index);
76 }
77 }
78
79 /* Validate that sorting between these indices will sort the array. Note that this is not a complete
80 * validation, as it does not check if these are the best possible indices.

Callers 1

mainMethod · 0.95

Calls 5

shrinkLeftMethod · 0.95
shrinkRightMethod · 0.95
validateMethod · 0.95

Tested by

no test coverage detected