Skip to content
This repository has been archived by the owner on May 10, 2020. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsaffari committed Jan 19, 2019
2 parents ea0d514 + 4b29d5f commit c5d4ec8
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Exercises and Samples/Exercises and Samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SearchMethods.cs" />
<Compile Include="SortMethods.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
Expand Down
89 changes: 85 additions & 4 deletions Exercises and Samples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,98 @@
namespace Exercises_and_Samples {
class Program {
static void Main(string[] args) {
Console.Write("Please enter a number: ");
long N = Convert.ToInt64(Console.ReadLine());
PrintNumbers(N);
}
//Console.Write("Please enter a number: ");
//long N = Convert.ToInt64(Console.ReadLine());
//PrintNumbers(N);

//int[] List = BubbleSort(new int[] { 10, 41, 15, 132, 102, 23, 265, 176, 90 });
//int Number = Convert.ToInt32(Console.ReadLine());
//Console.WriteLine("Index of {0} in the List is {1}", Number, BinarySearch_Recursive(Number, List, 0, List.Length));

int[] List = new int[] { 10, 41, 15, 132, 102, 23, 265, 176, 90 ,10};
Console.WriteLine("UnSorted: [{0}]", string.Join(", ", List));
//List = SortMethods.InsertionSort(List);
//Console.WriteLine("Insertion Sorted: [{0}]", string.Join(", ", List));
List = SortMethods.MergeSort(List);
Console.WriteLine("Merge Sorted: [{0}]", string.Join(", ", List));


//for (int i = 0; i < List.Length; i++) {
// int item = List[i];
// Console.WriteLine("Index of {0} in the list is: {1}", item, SearchMethods.BinarySearch_Recursive(List, item, 0, List.Length));
// Console.WriteLine("Index of {0} in the list is: {1}", item, SearchMethods.BinarySearch(List, item));
//}

}

static void PrintNumbers(long number) {
while (number > 0) {
Console.WriteLine(number % 10);
number /= 10;
}
}

static int SequentialSearch(int n, int[] array) {
for (int i = 0; i < array.Length; i++) {
if (array[i] == n) {
return i;
}
}
return -1;
}

static int BinarySearch(int n, int[] array) {
int low = 0;
int high = array.Length;

while (low <= high) {
int mid = (low + high) / 2;
if (n == array[mid]) {
return mid;
} else if (n < array[mid]) {
high = mid;
} else {
low = mid;
}
}
return -1;
}

static int BinarySearch_Recursive(int n, int[] array, int low, int high) {
if (high - low <= 1) {
if (n == array[low]) {
return low;
}
return -1;
}
int mid = (low + high) / 2;
if (n == array[mid]) {
return mid;
} else if (n < array[mid]) {
return BinarySearch_Recursive(n, array, low, mid);
} else {
return BinarySearch_Recursive(n, array, mid, high);
}
}

static int[] BubbleSort(int[] array) {
for (int i = array.Length - 1; i >= 0; i--) {
bool swapped = false;
for (int j = 0; j < i; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
swapped = true;
}
}
if (!swapped) {
break;
}
}
return array;

}

}
}
37 changes: 37 additions & 0 deletions Exercises and Samples/SearchMethods.cs
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;
}
}
}
53 changes: 53 additions & 0 deletions Exercises and Samples/SortMethods.cs
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;
}
}


}

0 comments on commit c5d4ec8

Please sign in to comment.