unit 4
unit 4
FUNDAMENTALS OF PROGRAMMING
Content
• Pointers:
• Pointer and address arithmetic,
• Pointer operations and declarations,
• Pointer and arrays,
• Pointer to structure.
• Call by value,
• Call by reference.
• Dynamic memory allocation.
• Sorting and searching algorithms:
• Selection sort,
• Bubble sort,
• Insertion sort,
• Linear and binary search.
Pointers
Pointer is a special variable that can store some address.
Syntax for declaring pointer variable
Examples
Need of address of operator (&)
Like
Important points
Value of operator • Value of operator/indirection operator/dereference
operator is an operator that is used to access the
(*) value stored at the location pointed by the pointer.
We can also change the value of the object by the pointer.
Important point
One more point
Example: Incrementing and decrementing pointers
• Structures and unions are user-defined data types that allow storing different types of
data under a single name.
• Structures allocate memory for each member individually, while unions allocate
memory that is the size of the largest member.
Structure is the best solution for such problems
• Structure is user defined data type that can be used to group elements of different types into a
single type.
Example: Using a pointer to access structure
members
struct Point {
int x;
int y;
};
printf("Coordinates: (%d, %d)\n", ptr->x, ptr->y); // Accessing structure members using pointer
Searching algorithms
Linear search
list one by one until it finds the element it is looking for. Linear search is
not very efficient for large lists, but it can be effective for small lists.
Example
#include <stdio.h>
// Function to perform linear search
int linearSearch(int arr[], int n, int key) {
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
return i; // Return the index if key is found
}
}
return -1; // Return -1 if key is not found
}
int main() {
int arr[] = {12, 45, 23, 6, 78, 54, 39};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 78;
// Perform linear search
int index = linearSearch(arr, n, key);
if (index != -1) {
printf("Element found at index: %d\n", index);
} else {
printf("Element not found\n");
}
return 0;
}
Binary search
dividing the list in half and then searching the half that contains the
sorting each half, and then merging the two sorted halves
list and then swapping it with the first element in the list. Selection
sort is a simple sorting algorithm, but it is not very efficient for large
lists.
1 2
5
3 4
Example
#include <stdio.h> int main() {
int arr[] = {64, 25, 12, 22, 11};
// Function to perform selection sort int n = sizeof(arr) / sizeof(arr[0]);
void selectionSort(int arr[], int n) { printf("Original array: ");
for (int i = 0; i < n - 1; i++) { for (int i = 0; i < n; i++) {
int minIndex = i; printf("%d ", arr[i]);
// Find the index of the smallest element in the unsorted }
part of the array printf("\n");
for (int j = i + 1; j < n; j++) { // Perform selection sort
if (arr[j] < arr[minIndex]) { selectionSort(arr, n);
minIndex = j;
} printf("Sorted array: ");
} for (int i = 0; i < n; i++) {
// Swap the smallest element with the first element of printf("%d ", arr[i]);
the unsorted part }
int temp = arr[i]; printf("\n");
arr[i] = arr[minIndex];
arr[minIndex] = temp; return 0;
} }
}
Insertion sort
into its correct position in the sorted list. Insertion sort is a simple
This algorithm is not suitable for large data sets as its average
given array around the picked pivot by placing the pivot in its
• The best sorting and searching algorithm to use will depend on the
linear search may be the best option. If the list is large and sorted,
binary search may be the best option. If the list is large and not