Skip to content

Commit 2d78cea

Browse files
add search and basic operations
1 parent 265871f commit 2d78cea

File tree

3 files changed

+233
-0
lines changed

3 files changed

+233
-0
lines changed

Array ADT/binary_search.cpp

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Array as Abstract Data Type
2+
// ADT has two properties
3+
// 1.) Declaration (most of the languages has array by default)
4+
// 2.) Operation - The operation must be defined
5+
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
struct Array
9+
{
10+
int A[100];
11+
int size;
12+
int length;
13+
};
14+
15+
void Display(struct Array arr)
16+
{
17+
printf("\nElement in array are : ");
18+
for (int i = 0; i < arr.length; i++)
19+
printf("%d ", arr.A[i]);
20+
}
21+
22+
// int BinarySearchIterative(struct Array arr, int key)
23+
// {
24+
// int low, mid, high;
25+
// low = 0;
26+
// high = arr.length - 1;
27+
// while (low <= high)
28+
// {
29+
// mid = (low + high) / 2;
30+
// if (key == arr.A[mid])
31+
// return mid;
32+
// else if (key > arr.A[mid])
33+
// low = mid + 1;
34+
// else
35+
// high = mid - 1;
36+
// }
37+
// return -1;
38+
// }
39+
40+
int BinarySearchRecursive(int a[], int low, int high, int key)
41+
{
42+
int mid;
43+
if (low <= high)
44+
{
45+
mid = (low + high) / 2;
46+
if (key == a[mid])
47+
return mid;
48+
else if (key < a[mid])
49+
return BinarySearchRecursive(a, low, mid - 1, key);
50+
else
51+
return BinarySearchRecursive(a, mid + 1, high, key);
52+
}
53+
return -1;
54+
}
55+
56+
int main()
57+
{
58+
struct Array arr = {{2, 3, 4, 5, 6, 7, 8}, 10, 7};
59+
60+
Display(arr);
61+
// printf("\nSearch Successful item found at index : %d", BinarySearchIterative(arr, 6));
62+
printf("\nSearch Successful item found at index : %d", BinarySearchRecursive(arr.A, 0, arr.length, 5));
63+
return 0;
64+
}
65+
66+
// Time Complexity Analysis
67+
// Iterative & Recursive
68+
// best : O[1]
69+
// average : O[logn]
70+
// worst : O[logn]

Array ADT/get_set_max_avg.c

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Array as Abstract Data Type
2+
// ADT has two properties
3+
// 1.) Declaration (most of the languages has array by default)
4+
// 2.) Operation - The operation must be defined
5+
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
struct Array
9+
{
10+
int A[100];
11+
int size;
12+
int length;
13+
};
14+
15+
void Display(struct Array arr)
16+
{
17+
printf("\nElement in array : ");
18+
for (int i = 0; i < arr.length; i++)
19+
printf("%d ", arr.A[i]);
20+
}
21+
22+
int get(struct Array arr, int index)
23+
{
24+
if (index >= 0 && index <= arr.length)
25+
return arr.A[index];
26+
return -1;
27+
}
28+
29+
int set(struct Array *arr, int index, int item)
30+
{
31+
if (index >= 0 && index <= arr->length)
32+
arr->A[index] = item;
33+
}
34+
35+
int avg(struct Array arr)
36+
{
37+
int sum = 0;
38+
for (int i = 0; i < arr.length; i++)
39+
sum += arr.A[i];
40+
return sum / arr.length;
41+
}
42+
43+
int max(struct Array arr)
44+
{
45+
int max = arr.A[0];
46+
for (int i = 0; i < arr.length; i++)
47+
{
48+
if (arr.A[i] > max)
49+
max = arr.A[i];
50+
}
51+
return max;
52+
}
53+
54+
int main()
55+
{
56+
struct Array arr = {{2, 4, 6, 8, 10}, 10, 5};
57+
58+
Display(arr);
59+
printf("\nElement at given index is : %d ", get(arr, 2));
60+
printf("\nafter Set operation ");
61+
set(&arr, 2, 9);
62+
Display(arr);
63+
printf("\nAverage in array : %d", avg(arr));
64+
printf("\nMax in array : %d", max(arr));
65+
66+
return 0;
67+
}
68+
69+
// output
70+
71+
// Element in array : 2 4 6 8 10
72+
// Element at given index is : 6
73+
// after Set operation
74+
// Element in array : 2 4 9 8 10
75+
// Average in array : 6
76+
// Max in array : 10
77+
78+
// Analysis
79+
80+
// get : O(1) constant for all cases
81+
// set : O(1) constant for all cases
82+
// max : O(n) all cases
83+
// avg : O(n) all cases

Array ADT/linear_search.c

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// improved Linear Search
2+
// 1 - transposition key is moved one position left ,
3+
// decrease one step from next search
4+
5+
// 2 - Move to Head/Tail, key is swapped with tail/head,
6+
// optimize search to O[1] for next searches
7+
8+
#include <stdio.h>
9+
#include <stdlib.h>
10+
11+
struct Array
12+
{
13+
int A[100];
14+
int size;
15+
int length;
16+
};
17+
18+
void Display(struct Array arr)
19+
{
20+
printf("\nElement in array are : ");
21+
for (int i = 0; i < arr.length; i++)
22+
printf("%d ", arr.A[i]);
23+
}
24+
25+
void swap(int *x, int *y)
26+
{
27+
int temp = *x;
28+
*x = *y;
29+
*y = temp;
30+
}
31+
32+
int LinearSearchMoveToHead(struct Array *arr, int key)
33+
{
34+
for (int i = 0; i < arr->length; i++)
35+
{
36+
if (key == arr->A[i])
37+
{
38+
swap(&arr->A[0], &arr->A[i]);
39+
return i;
40+
}
41+
}
42+
return -1;
43+
}
44+
45+
// int LinearSearchTransposition(struct Array *arr, int key)
46+
// {
47+
// for (int i = 0; i < arr->length; i++)
48+
// {
49+
// if (key == arr->A[i])
50+
// {
51+
// swap(&arr->A[i], &arr->A[i - 1]);
52+
// return i;
53+
// }
54+
// }
55+
// return -1;
56+
// }
57+
58+
int main()
59+
{
60+
struct Array arr = {{2, 3, 4, 5, 6}, 10, 5};
61+
62+
Display(arr);
63+
64+
// printf("\nElement found at index : %d", LinearSearchTransposition(&arr, 4));
65+
printf("\nElement found at index : %d", LinearSearchMoveToHead(&arr, 4));
66+
67+
Display(arr);
68+
69+
return 0;
70+
}
71+
72+
// Output :
73+
// Element in array are : 2 3 4 5 6
74+
// Element found at index : 2
75+
// Element in array are : 4 3 2 5 6
76+
77+
78+
// Analysis :
79+
// worst : O[n]
80+
// Best : O[1]

0 commit comments

Comments
 (0)