-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchOPerations.cs
74 lines (65 loc) · 1.93 KB
/
SearchOPerations.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DSA
{
class SearchOPerations
{
public static void LinearSearch(int item, int[] searchArray)
{
bool itemFound = false;
for (int i = 0; i < searchArray.Length; i++)
{
Console.WriteLine("\nThe array to look for in is :" + searchArray[i]);
if (searchArray[i] == item)
{
itemFound = true;
Console.WriteLine("\n Item found and is equals to:" + searchArray[i] + " at location:" + i);
}
}
if (!itemFound)
{
Console.WriteLine("\n Item does not exist in the array set");
}
}
public static void MergeSort()
{
int[] a = new int[8]{9,3,5,1,2,6,11,4};
SortAndMerge(a);
for(int i=0;i<a.Length;i++)
{
Console.WriteLine("\n The sorted array is :"+ a[i]);
}
}
static void SortAndMerge(int[] a)
{
double exp=0;
int j = 1;
while (j <a.Length)
{
for (int i = j-1; i < j; i = i + j)
{
for (int x = 0; x < i; x++)
{
if (a[x] > a[x + i])
{
Swap(a, x, x + i);
}
}
}
exp++;
j = j+(int)Math.Pow(2, exp);
}
}
static int[] Swap(int[] a, int x, int y)
{
int temp = 0;
temp = a[x];
a[x] = a[y];
a[y] = temp;
return a;
}
}
}