Skip to content

Commit 0580a78

Browse files
committed
GOD bless universes
1 parent 6eae4b3 commit 0580a78

File tree

12 files changed

+276
-49
lines changed

12 files changed

+276
-49
lines changed

Algorithms/Searching algorithms/BinarySearch/BinarySearchAlgorithm.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
namespace BinarySearch
66
{
7-
public static class BinarySearchAlgorithm
7+
public static class BinarySearchAlgorithm<T> where T : IItem
88
{
9-
public static int Search(List<Item> items, int x)
9+
public static async Task <int> Search(List<T> items, int x)
1010
{
11-
return BinarySearchRecursive(items, x, 0, items.Count - 1);
11+
return await BinarySearchRecursive(items, x, 0, items.Count - 1);
1212
}
1313

14-
private static int BinarySearchRecursive(List<Item> items, int target, int left, int right)
14+
private static async Task <int> BinarySearchRecursive(List<T> items, int target, int left, int right)
1515
{
1616

1717
if (left <= right)
@@ -24,11 +24,11 @@ private static int BinarySearchRecursive(List<Item> items, int target, int left,
2424
}
2525
else if (items[middle].Id < target)
2626
{
27-
return BinarySearchRecursive(items, target, middle + 1, right);
27+
return await BinarySearchRecursive(items, target, middle + 1, right);
2828
}
2929
else
3030
{
31-
return BinarySearchRecursive(items, target, left, middle - 1);
31+
return await BinarySearchRecursive(items, target, left, middle - 1);
3232
}
3333
}
3434

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\..\..\Shares\Data\Data.csproj" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Data;
2+
3+
namespace JumpSearch
4+
{
5+
public static class JumpSearchAlgorithm<T> where T : IItem
6+
{
7+
public static async Task<int> Search(List<T> items, int targetId)
8+
{
9+
int n = items.Count;
10+
int step = (int)Math.Sqrt(n);
11+
int prev = 0;
12+
13+
while (items[Math.Min(step, n) - 1].Id < targetId)
14+
{
15+
prev = step;
16+
step += (int)Math.Sqrt(n);
17+
18+
if (prev >= n)
19+
{
20+
return -1;
21+
}
22+
}
23+
24+
while (items[prev].Id < targetId)
25+
{
26+
prev++;
27+
28+
if (prev == Math.Min(step, n))
29+
{
30+
return -1;
31+
}
32+
}
33+
34+
if (items[prev].Id == targetId)
35+
{
36+
return prev;
37+
}
38+
39+
return -1;
40+
}
41+
}
42+
}

Algorithms/Searching algorithms/LinearSearch/LinearSearchAlgorithm.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
namespace LinearSearch
99
{
10-
public static class LinearSearchAlgorithm
10+
public static class LinearSearchAlgorithm<T> where T : IItem
1111
{
1212

13-
public static int Search(List<Item> items, int x)
13+
public static async Task<int> Search(List<T> items, int x)
1414
{
1515
for (int i = 0; i < items.Count; i++)
1616
{
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\..\..\Shares\Data\Data.csproj" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Data;
2+
3+
namespace TernarySearch
4+
{
5+
public class TernarySearchAlgorithm<T> where T : IItem
6+
{
7+
public static async Task<int> Search(List<T> items, int left, int right, int targetId)
8+
{
9+
if (left <= right)
10+
{
11+
int mid1 = left + (right - left) / 3;
12+
int mid2 = right - (right - left) / 3;
13+
14+
if (items[mid1].Id == targetId)
15+
{
16+
return mid1;
17+
}
18+
if (items[mid2].Id == targetId)
19+
{
20+
return mid2;
21+
}
22+
23+
if (targetId < items[mid1].Id)
24+
{
25+
return await Search(items, left, mid1 - 1, targetId);
26+
}
27+
else if (targetId > items[mid2].Id)
28+
{
29+
return await Search(items, mid2 + 1, right, targetId);
30+
}
31+
else
32+
{
33+
return await Search(items, mid1 + 1, mid2 - 1, targetId);
34+
}
35+
}
36+
37+
return -1; // Not found
38+
}
39+
}
40+
}

Algorithms/Sorting algorithms/MergeSort/MergeSortAlgorithm.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ await Task.Run(() =>
1717
return items;
1818
}
1919

20-
static void MergeSort(List<T> items)
20+
static async Task MergeSort(List<T> items)
2121
{
2222
int n = items.Count;
2323

@@ -28,10 +28,14 @@ static void MergeSort(List<T> items)
2828
List<T> left = items.GetRange(0, mid);
2929
List<T> right = items.GetRange(mid, n - mid);
3030

31-
MergeSort(left);
32-
MergeSort(right);
31+
await Task.WhenAll(
32+
MergeSort(left),
33+
MergeSort(right)
34+
);
3335

3436
Merge(items, left, right);
37+
38+
3539
}
3640
}
3741

@@ -43,7 +47,6 @@ static void Merge(List<T> items, List<T> left, List<T> right)
4347

4448
while (i < leftCount && j < rightCount)
4549
{
46-
4750
if (left[i].Id < right[j].Id)
4851
{
4952
items[k] = left[i];
@@ -57,15 +60,13 @@ static void Merge(List<T> items, List<T> left, List<T> right)
5760
k++;
5861
}
5962

60-
6163
while (i < leftCount)
6264
{
6365
items[k] = left[i];
6466
i++;
6567
k++;
6668
}
6769

68-
6970
while (j < rightCount)
7071
{
7172
items[k] = right[j];

Algorithms/Sorting algorithms/QuickSort/QuickSortAlgorithm.cs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,28 @@ public static class QuickSortAlgorithm<T> where T : IItem
99
{
1010
public static async Task<List<T>> Sorting(List<T> items)
1111
{
12-
await Task.Run(() =>
12+
await Task.Run( async() =>
1313
{
14-
QuickSort(items, 0, items.Count - 1);
14+
await QuickSort(items, 0, items.Count - 1); // Wait for the asynchronous sorting to complete.
1515
});
1616

1717
return items;
1818
}
1919

20-
static void QuickSort(List<T> items, int low, int high)
20+
static async Task QuickSort(List<T> items, int low, int high)
2121
{
2222
if (low < high)
2323
{
24-
int partitionIndex = Partition(items, low, high);
24+
int partitionIndex = Partition(items, low, high).Result;
2525

26-
QuickSort(items, low, partitionIndex - 1);
27-
QuickSort(items, partitionIndex + 1, high);
26+
Task.WaitAll(
27+
Task.Run(() => QuickSort(items, low, partitionIndex - 1)),
28+
Task.Run(() => QuickSort(items, partitionIndex + 1, high))
29+
);
2830
}
2931
}
3032

31-
static int Partition(List<T> items, int low, int high)
33+
static async Task<int> Partition(List<T> items, int low, int high)
3234
{
3335
T pivot = items[high];
3436
int i = low - 1;
@@ -38,19 +40,23 @@ static int Partition(List<T> items, int low, int high)
3840
if (items[j].Id < pivot.Id)
3941
{
4042
i++;
41-
Swap(items, i, j);
43+
await Swap(items, i, j);
4244
}
4345
}
4446

45-
Swap(items, i + 1, high);
47+
await Swap(items, i + 1, high);
4648
return i + 1;
4749
}
4850

49-
static void Swap(List<T> items, int i, int j)
51+
static async Task Swap(List<T> items, int i, int j)
5052
{
51-
T temp = items[i];
52-
items[i] = items[j];
53-
items[j] = temp;
53+
await Task.Run(() =>
54+
{
55+
T temp = items[i];
56+
items[i] = items[j];
57+
items[j] = temp;
58+
});
5459
}
60+
5561
}
5662
}

DataStructuresAndAlgorithms.sln

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SelectionSort", "Algorithms
4141
EndProject
4242
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stack", "Algorithms\Custom Abstract data types algorithms\Stack\Stack.csproj", "{9BE7A02A-F512-442B-8830-E105E611B087}"
4343
EndProject
44-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Queue", "Algorithms\Custom Abstract data types algorithms\Queue\Queue.csproj", "{45E2DA40-FC03-43A2-AF7B-F3E8F94CE735}"
44+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Queue", "Algorithms\Custom Abstract data types algorithms\Queue\Queue.csproj", "{45E2DA40-FC03-43A2-AF7B-F3E8F94CE735}"
45+
EndProject
46+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TernarySearch", "Algorithms\Searching algorithms\TernarySearch\TernarySearch.csproj", "{DABF98E7-54A7-4A1C-8679-F2804A23FC97}"
47+
EndProject
48+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JumpSearch", "Algorithms\Searching algorithms\JumpSearch\JumpSearch.csproj", "{9B2B98BB-90C5-42BE-BDAA-EAC35F9DF41B}"
4549
EndProject
4650
Global
4751
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -101,6 +105,14 @@ Global
101105
{45E2DA40-FC03-43A2-AF7B-F3E8F94CE735}.Debug|Any CPU.Build.0 = Debug|Any CPU
102106
{45E2DA40-FC03-43A2-AF7B-F3E8F94CE735}.Release|Any CPU.ActiveCfg = Release|Any CPU
103107
{45E2DA40-FC03-43A2-AF7B-F3E8F94CE735}.Release|Any CPU.Build.0 = Release|Any CPU
108+
{DABF98E7-54A7-4A1C-8679-F2804A23FC97}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
109+
{DABF98E7-54A7-4A1C-8679-F2804A23FC97}.Debug|Any CPU.Build.0 = Debug|Any CPU
110+
{DABF98E7-54A7-4A1C-8679-F2804A23FC97}.Release|Any CPU.ActiveCfg = Release|Any CPU
111+
{DABF98E7-54A7-4A1C-8679-F2804A23FC97}.Release|Any CPU.Build.0 = Release|Any CPU
112+
{9B2B98BB-90C5-42BE-BDAA-EAC35F9DF41B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
113+
{9B2B98BB-90C5-42BE-BDAA-EAC35F9DF41B}.Debug|Any CPU.Build.0 = Debug|Any CPU
114+
{9B2B98BB-90C5-42BE-BDAA-EAC35F9DF41B}.Release|Any CPU.ActiveCfg = Release|Any CPU
115+
{9B2B98BB-90C5-42BE-BDAA-EAC35F9DF41B}.Release|Any CPU.Build.0 = Release|Any CPU
104116
EndGlobalSection
105117
GlobalSection(SolutionProperties) = preSolution
106118
HideSolutionNode = FALSE
@@ -123,6 +135,8 @@ Global
123135
{E93874F0-B0C5-4A04-9852-0D293BB86A0B} = {90AAC62F-6373-40D6-B54E-B5BF7AC25166}
124136
{9BE7A02A-F512-442B-8830-E105E611B087} = {97F18839-2773-43C8-A7C7-1E05888E8312}
125137
{45E2DA40-FC03-43A2-AF7B-F3E8F94CE735} = {97F18839-2773-43C8-A7C7-1E05888E8312}
138+
{DABF98E7-54A7-4A1C-8679-F2804A23FC97} = {EDA88687-37E5-4CB0-B165-99C234663DCC}
139+
{9B2B98BB-90C5-42BE-BDAA-EAC35F9DF41B} = {EDA88687-37E5-4CB0-B165-99C234663DCC}
126140
EndGlobalSection
127141
GlobalSection(ExtensibilityGlobals) = postSolution
128142
SolutionGuid = {4CCF8022-6EFC-48B8-91FE-D4EA5C7496E0}

Presentations/DataStructuresAndAlgorithms/DataStructuresAndAlgorithms.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
<ProjectReference Include="..\..\Algorithms\Custom Abstract data types algorithms\Queue\Queue.csproj" />
1313
<ProjectReference Include="..\..\Algorithms\Custom Abstract data types algorithms\Stack\Stack.csproj" />
1414
<ProjectReference Include="..\..\Algorithms\Searching algorithms\BinarySearch\BinarySearch.csproj" />
15+
<ProjectReference Include="..\..\Algorithms\Searching algorithms\JumpSearch\JumpSearch.csproj" />
1516
<ProjectReference Include="..\..\Algorithms\Searching algorithms\LinearSearch\LinearSearch.csproj" />
17+
<ProjectReference Include="..\..\Algorithms\Searching algorithms\TernarySearch\TernarySearch.csproj" />
1618
<ProjectReference Include="..\..\Algorithms\Sorting algorithms\BubbleSort\BubbleSort.csproj" />
1719
<ProjectReference Include="..\..\Algorithms\Sorting algorithms\InsertionSort\InsertionSort.csproj" />
1820
<ProjectReference Include="..\..\Algorithms\Sorting algorithms\MergeSort\MergeSort.csproj" />

0 commit comments

Comments
 (0)