This repository has been archived by the owner on May 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/mmsaffari/Foundations-of-…
- Loading branch information
Showing
4 changed files
with
177 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
|
||
namespace Exercises_and_Samples { | ||
public class SearchMethods { | ||
public static int BinarySearch_Recursive<T>(T[] input, T item, int lowerBound, int upperBound) where T : IComparable { | ||
if (lowerBound <= upperBound) { | ||
int mid = (lowerBound + upperBound) / 2; | ||
if (input[mid].CompareTo(item) == 0) { | ||
return mid; | ||
} else if (input[mid].CompareTo(item) > 0) { | ||
return BinarySearch_Recursive(input, item, lowerBound, mid - 1); | ||
} else { | ||
return BinarySearch_Recursive(input, item, mid + 1, upperBound); | ||
} | ||
} else { | ||
return -1; | ||
} | ||
} | ||
|
||
public static int BinarySearch<T>(T[] input, T item) where T : IComparable { | ||
int low = 0; | ||
int high = input.Length; | ||
|
||
while (low <= high) { | ||
int mid = (low + high) / 2; | ||
if (input[mid].CompareTo(item) == 0) { | ||
return mid; | ||
} else if (input[mid].CompareTo(item) > 0) { | ||
high = mid - 1; | ||
} else { | ||
low = mid + 1; | ||
} | ||
} | ||
return -1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace Exercises_and_Samples { | ||
public class SortMethods { | ||
public static T[] InsertionSort<T>(T[] input) where T : IComparable { | ||
T[] result = input.Clone() as T[]; | ||
for (int i = 1; i < result.Length; i++) { | ||
T key = result[i]; | ||
int j = i - 1; | ||
while (j > 0 && result[j].CompareTo(key) > 0) { | ||
result[j + 1] = result[j--]; | ||
} | ||
result[j + 1] = key; | ||
} | ||
return result; | ||
} | ||
|
||
|
||
public static T[] MergeSort<T>(T[] input) where T : IComparable { | ||
int n = input.Length; | ||
|
||
if (n > 1) { | ||
int h = n / 2; | ||
T[] U = MergeSort(input.Take(h).ToArray()); | ||
T[] V = MergeSort(input.Skip(h).ToArray()); | ||
|
||
T[] result = new T[U.Length + V.Length]; | ||
int uIdx = 0; | ||
int vIdx = 0; | ||
int rIdx = 0; | ||
while (uIdx < U.Length || vIdx < V.Length) { | ||
result[rIdx++] = U[uIdx].CompareTo(V[vIdx]) < 0 ? U[uIdx++] : V[vIdx++]; | ||
if (uIdx == U.Length) { | ||
for (int i = vIdx; i < V.Length; i++) { | ||
result[rIdx++] = V[vIdx++]; | ||
} | ||
} | ||
if (vIdx == V.Length) { | ||
for (int i = uIdx; i < U.Length; i++) { | ||
result[rIdx++] = U[uIdx++]; | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
return input; | ||
} | ||
} | ||
|
||
|
||
} |