class 2
class 2
https://www.cs.usfca.edu/~galles/visualization/Search.html
// Binary Search in C
#include <stdio.h>
int binarySearch(int array[], int x, int low, int high) {
// Repeat until the pointers low and high meet each other
while (low <= high) {
int mid = low + (high - low) / 2;
if (x == array[mid])
return mid;
if (x > array[mid])
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main(void) {
int array[] = {3, 4, 5, 6, 7, 8, 9};
int n = sizeof(array) / sizeof(array[0]);
int x = 4;
int result = binarySearch(array, x, 0, n - 1);
if (result == -1)
printf("Not found");
else
printf("Element is found at index %d", result);
return 0;
}
2D Array
2D array can be defined as an array of arrays. The 2D array is organized as matrices which can be represented as the collection of rows and
columns.
Due to the fact that the elements of 2D arrays can be random accessed. Similar to one dimensional arrays, we can access the individual cells in
a 2D array by using the indices of the cells. There are two indices attached to a particular cell, one is its row number while the other is its
column number.
However, we can store the value stored in any particular cell of a 2D array to some variable x by using the following syntax.
int x = a[i][j];
where i and j is the row and column number of the cell respectively.
Initializing 2D Arrays
We know that, when we declare and initialize one dimensional array in C programming simultaneously, we don't need to specify the size of the
array. However this will not work with 2D arrays. We will have to define at least the second dimension of the array.
The syntax to declare and initialize the 2D array is given as follows: int arr[2][2] = {0,1,2,3};
The number of elements that can be present in a 2D array will always be equal to (number of rows * number of columns).
Example: Storing User's data into a 2D array and printing it.
1. #include <stdio.h>
2. void main ()
3. {
4. int arr[3][3],i,j;
5. for (i=0;i<3;i++)
6. {
7. for (j=0;j<3;j++)
8. {
9. printf("Enter a[%d][%d]: ",i,j);
10. scanf("%d",&arr[i][j]);
11. }
12. }
13. printf("\n printing the elements ....\n");
14. for(i=0;i<3;i++)
15. {
16. printf("\n");
17. for (j=0;j<3;j++)
18. {
19. printf("%d\t",arr[i][j]);
20. }
21. }
22. }
Mapping 2D array to 1D array
The size of a two-dimensional array is equal to the multiplication of number of rows and the number of columns present in the array. We do
need to map two-dimensional array to the one dimensional array in order to store them in the memory.
There are two main techniques of storing 2D array elements into memory
In row major ordering, all the rows of the 2D array are stored into the memory contiguously. Considering the array shown in the above image,
its memory allocation according to row major order is shown as follows.
First, the 1st row of the array is stored into the memory completely, then the 2nd row of the array is stored into the memory completely and so
on till the last row.
2. Column Major ordering
According to the column major ordering, all the columns of the 2D array are stored into the memory contiguously. The memory allocation of
the array which is shown in in the above image is given as follows.
First, the 1st column of the array is stored into the memory completely, then the 2nd row of the array is stored into the memory completely and
so on till the last column of the array.
Calculating the Address of the random element of a 2D array
Due to the fact that, there are two different techniques of storing the two dimensional array into the memory, there are two different formulas
to calculate the address of a random element of the 2D array.
If array is declared by a[m][n] where m is the number of rows while n is the number of columns, then address of an element a[i][j] of the array
stored in row major order is calculated as,
Address(a[i][j]) = B. A. + (i * n + j) * size
where, B. A. is the base address or the address of the first element of the array a[0][0] .
Example :
a[10...30][ 55...75], base address of the array (BA) = 0, size of an element = 4 bytes .
Find the location of a[15][68].
If array is declared by a[m][n] where m is the number of rows while n is the number of columns, then address of an element a[i][j] of the array
stored in row major order is calculated as,
Address(a[i][j]) = ((j*m)+i)*Size + BA
Example:
A [-5 ... +20][20 ... 70], BA = 1020, Size of element = 8 bytes. Find the location of a[0][30].
There are some basic operations that allow us to perform different actions on a stack.
A pointer called TOP is used to keep track of the top element in the stack.
When initializing the stack, we set its value to -1 so that we can check if the stack is empty by comparing TOP == -1.
On pushing an element, we increase the value of TOP and place the new element in the position pointed to by TOP.
On popping an element, we return the element pointed to by TOP and reduce its value.
Before pushing, we check if the stack is already full
Before popping, we check if the stack is already empty
#include <stdio.h> /* Main function */
int MAXSIZE = 8; int main(){
int stack[8]; int i;
int top = -1; push(44);
/* Check if the stack is full*/ push(10);
int isfull(){ push(62);
if(top == MAXSIZE) push(123);
return 1; push(15);
else printf("Stack Elements: \n");
return 0;
} // print stack data
/* Function to insert into the stack */ for(i = 0; i < 8; i++) {
int push(int data){ printf("%d ", stack[i]);
if(!isfull()) { }
top = top + 1; return 0;
stack[top] = data; }
} else {
printf("Could not insert data, Stack is
full.\n");
}
}
Applications of Stack Data Structure
Although stack is a simple data structure to implement, it is very powerful. The most common uses of a stack are:
To reverse a word - Put all the letters in a stack and pop them out. Because of the LIFO order of stack, you will get the letters in reverse
order.
In compilers - Compilers use the stack to calculate the value of expressions like by converting the expression to prefix or postfix form.
In browsers - The back button in a browser saves all the URLs you have visited previously in a stack. Each time you visit a new page, it
is added on top of the stack. When you press the back button, the current URL is removed from the stack, and the previous URL is
accessed.