-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
157 lines (123 loc) · 3.22 KB
/
Program.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
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
// O(1) - Constant time complexity
int[] array = new int[] { 10, 20, 30, 40, 50 };
int element = array[2];
Console.WriteLine($"Element at index 2: {element}");
// O(log n) - Logarithmic time complexity
int BinarySearch(int[] arr, int target)
{
int left = 0;
int right = arr.Length - 1;
while (left <= right)
{
int mid = left + (right - left) / 2;
if (arr[mid] == target)
return mid;
if (arr[mid] > target)
right = mid - 1;
else
left = mid + 1;
}
return -1;
}
int[] arr = { 2, 5, 6, 12, 16, 23, 38, 56, 72, 91 };
int target = 23;
int result = BinarySearch(arr, target);
if (result is not -1)
Console.WriteLine($"Element found in position: {result}");
else
Console.WriteLine("Element not found in array");
// O(n) - Linear time complexity
int[] unorderedArray = { 3, 1, 6, 12, 16, 23, 38, 56, 72, 83 };
int targetElement = 72;
int LinearSearch(int[] unorderedArray, int targetElement)
{
for (int i = 0; i < unorderedArray.Length; i++)
{
if (unorderedArray[i] == targetElement)
{
return i;
}
}
return -1;
}
int resultIndex = LinearSearch(unorderedArray, targetElement);
if (resultIndex != -1)
Console.WriteLine($"Element found at index: {resultIndex}");
else
Console.WriteLine("Element not found in the array");
// O(n log n) - Log-linear time complexity
int[] originalArray = { 12, 4, 5, 6, 7, 3, 1, 15, 8, 9 };
Console.WriteLine("Original array:");
PrintArray(originalArray);
QuickSort(originalArray, 0, originalArray.Length - 1);
Console.WriteLine("\nOrdered array:");
PrintArray(originalArray);
void QuickSort(int[] originalArray, int low, int high)
{
if (low < high)
{
int pivot = Partition(originalArray, low, high);
QuickSort(originalArray, low, pivot - 1);
QuickSort(originalArray, pivot + 1, high);
}
}
int Partition(int[] originalArray, int low, int high)
{
int pivot = originalArray[high];
int i = low - 1;
for (int j = low; j < high; j++)
{
if (originalArray[j] < pivot)
{
i++;
Swap(originalArray, i, j);
}
}
Swap(originalArray, i + 1, high);
return i + 1;
}
void Swap(int[] originalArray, int i, int j)
{
int temp = originalArray[i];
originalArray[i] = originalArray[j];
originalArray[j] = temp;
}
void PrintArray(int[] arr)
{
foreach (int num in arr)
{
Console.Write(num + " ");
}
Console.WriteLine();
}
// O(n^2) - Quadratic time complexity
int[] originalArray = { 64, 34, 25, 12, 22, 11, 90 };
Console.WriteLine("Original array:");
PrintArray(originalArray);
BubbleSort(originalArray);
Console.WriteLine("\nOrdered array:");
PrintArray(originalArray);
static void BubbleSort(int[] arr)
{
int n = arr.Length;
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
static void PrintArray(int[] arr)
{
foreach (int num in arr)
{
Console.Write(num + " ");
}
Console.WriteLine();
}