-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchInArrays.cs
More file actions
178 lines (150 loc) · 5.56 KB
/
SearchInArrays.cs
File metadata and controls
178 lines (150 loc) · 5.56 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
namespace LeetCode.Arrays
{
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
[TestClass]
public class SearchInArrays
{
public int SearchInRotatedSortedArray_Improved(int[] array, int target)
{
return -1;
}
/// <summary>
/// Rotated sorted array.
/// In Rotated sorted array, there is a point where a value is less than previous value.
/// For intance: 1 2 3 4 5 6 7 8 is rotated at 4 means 4 5 6 7 8 1 2 3
/// It is increasing order from 4 and stops at 8 and then starts at 1 and goes in increasing order.
/// Now, finding the pivot where it stops increasing is the first step (i.e, the value is less than previous value)
/// If the target is less than the arr[pivot] and greater than array[n-1] then search in first set.
/// Otherwise search in second set of array.
/// If it's first set then the array range will be 0-pivot
/// If it's second set then the array range will be pivot+1 - (n-1)
/// Both the ranges are in increasing order, so we can apply the binary search.
/// </summary>
public int SearchInRotatedSortedArray(int[] array, int target)
{
// first step find the pivot value.
int pivotValue = this.FindPivot(array);
int index =0;
if (target < array[pivotValue] && target > array[array.Length -1])
{
// Binary search from 0 to mid-1
int[] newArray = new int[pivotValue + 1];
Array.Copy(array, newArray, newArray.Length);
index = this.BinarySearch(newArray, target);
}
else
{
// Binary search from pivot to n
int len = array.Length -1;
int[] newArray = new int[len - pivotValue];
Array.Copy(array, pivotValue + 1, newArray, 0, newArray.Length);
index = this.BinarySearch(newArray, target);
index += pivotValue + 1;
}
return index;
}
private int BinarySearch(int[] array, int target)
{
int low = 0;
int high = array.Length - 1;
while(low<= high)
{
int mid = (low + high) / 2;
if (array[mid] < target)
{
low = mid + 1;
}
else if(array[mid] > target)
{
high = mid - 1;
}
else
{
return mid;
}
}
return -1;
}
private int FindPivot(int[] array)
{
// we can find the pivot in o(log(n)).
int low = 0;
int high = array.Length - 1;
while (low < high)
{
int mid = (low + high) / 2;
if (array[mid] < array[mid-1])
{
return mid -1;
}
else
{
low = mid + 1;
}
}
return 0;
}
public void SearchForRangeInArray()
{
}
/// <summary>
/// Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array.
/// [1,3,5,6], 5 -> 2
/// [1,3,5,6], 2 -> 1
/// </summary>
public int SearchInsertPositionInSortedArray_BinarySearch(int[] array, int target)
{
int low = 0;
int high = array.Length - 1;
int mid = 0;
while (low <= high)
{
mid = (low + high) / 2;
if (array[mid] < target)
{
low = mid + 1;
}
else if (array[mid] > target)
{
high = mid - 1;
}
else
{
return mid;
}
}
return low;
}
[TestMethod]
public void TestSearchInRotatedSortedArray()
{
int index = this.SearchInRotatedSortedArray(new[] { 4, 5, 6, 7, 8, 1, 2, 3 }, 5);
Assert.AreEqual(index, 1);
index = this.SearchInRotatedSortedArray(new[] { 3, 4, 5, 6, 7, 8, 1, 2 }, 2);
Assert.AreEqual(index, 7);
index = this.SearchInRotatedSortedArray(new[] { 7, 8, 1, 2, 3, 4, 5, 6 }, 4);
Assert.AreEqual(index, 5);
}
[TestMethod]
public void TestSearchInsertPositionInArray()
{
int[] array = { 1, 3, 5, 6 };
int resultIndex = this.SearchInsertPositionInSortedArray_BinarySearch(array, 5);
Assert.AreEqual(resultIndex, 2);
array = new [] { 1, 3, 5, 6 };
resultIndex = this.SearchInsertPositionInSortedArray_BinarySearch(array, 2);
Assert.AreEqual(resultIndex, 1);
array = new[] { 1, 3, 5, 6 };
resultIndex = this.SearchInsertPositionInSortedArray_BinarySearch(array, 7);
Assert.AreEqual(resultIndex, 4);
array = new[] { 1, 3, 5, 6 };
resultIndex = this.SearchInsertPositionInSortedArray_BinarySearch(array, 0);
Assert.AreEqual(resultIndex, 0);
}
}
}