C Programming Fundamentals and Sorting Algorithms Reference
C Program: Find and Count Array Element Occurrences
Objective: Write a C program to find all positions where a specific value (9) is present in a user-defined list and count its total occurrences.
Given Sample List (entered at runtime): 3, 2, 10, 9, 7, 1, 5, 21, 8, 5. This should print: “9 is present at 4th position.”
C Implementation (1-Based Indexing)
#include <stdio.h>
int main() {
int n, i, count = 0, x = 9;
printf("Enter number of elements: ");
scanf("%d", &n);
int a[n];
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) scanf("%d", &a[i]);
printf("Value %d found at positions: ", x);
for (i = 0; i < n; i++) {
if (a[i] == x) {
count++;
printf("%d ", i + 1); /* print 1-based position */
}
}
if (count == 0) printf("None");
printf("\nTotal occurrences: %d\n", count);
return 0;
}For the sample input, the output is: Value 9 found at positions: 4 and Total occurrences: 1.
Selection Sort: Array State After Each Pass
Initial Array: 67, 41, 11, 22, 40, 85 (n = 6).
Selection sort works by repeatedly finding the minimum element from the unsorted suffix of the array and swapping it with the element at the current position.
Pass-by-Pass Demonstration
Pass 1 (i=0): Find minimum in [0..5] = 11. Swap 11 with a[0] (67).
Result:11, 41, 67, 22, 40, 85Pass 2 (i=1): Find minimum in [1..5] = 22. Swap 22 with a[1] (41).
Result:11, 22, 67, 41, 40, 85Pass 3 (i=2): Find minimum in [2..5] = 40. Swap 40 with a[2] (67).
Result:11, 22, 40, 41, 67, 85Pass 4 (i=3): Find minimum in [3..5] = 41. Swap 41 with a[3] (41). (No change)
Result:11, 22, 40, 41, 67, 85Pass 5 (i=4): Find minimum in [4..5] = 67. Swap 67 with a[4] (67). (No change)
Result:11, 22, 40, 41, 67, 85
After n-1 = 5 passes, the array is sorted.
C Program: Inserting an Element at the End of an Array
Initial Array: 57, 14, 26, 80, 47 (n = 5). We insert 90 at the end.
Expected Result: 57, 14, 26, 80, 47, 90.
C Implementation
#include <stdio.h>
int main() {
int arr[100] = {57, 14, 26, 80, 47};
int n = 5, x = 90, i;
/* insert x at end (using the current size 'n' as the next index) */
arr[n] = x;
n++;
printf("Array after inserting %d at end:\n", x);
for (i = 0; i < n; i++) printf("%d ", arr[i]);
printf("\n");
return 0;
}Output: 57 14 26 80 47 90
Pointers and Structures (Struct) in C
Definition of a Pointer
A pointer is a variable that stores the memory address of another variable. It allows indirect access and manipulation of the value stored at that address.
C Program Using the student Structure
This program defines a student structure and initializes data for two students (Aman and Riya), then prints their details.
#include <stdio.h>
#include <string.h>
struct student {
char name[50];
int rollno;
float marks;
};
int main() {
struct student s1, s2;
/* Initialize s1 */
strcpy(s1.name, "Aman");
s1.rollno = 101;
s1.marks = 78.5;
/* Initialize s2 */
strcpy(s2.name, "Riya");
s2.rollno = 102;
s2.marks = 85.0;
/* Print results */
printf("Name\tRoll\tMarks\n");
printf("%s\t%d\t%.2f\n", s1.name, s1.rollno, s1.marks);
printf("%s\t%d\t%.2f\n", s2.name, s2.rollno, s2.marks);
return 0;
}Search and Sorting Algorithms in C
Iterative Binary Search Function
Assumption: The input array a[] is sorted ascending. The function returns the 0-based index of the key or -1 if the key is not found.
int binary_search(int a[], int n, int key) {
int low = 0, high = n - 1, mid;
while (low <= high) {
mid = low + (high - low) / 2;
if (a[mid] == key) return mid;
else if (a[mid] < key) low = mid + 1;
else high = mid - 1;
}
return -1;
}Insertion Sort Logic (Ascending)
Idea: Take the next element (the key) and insert it into the correct place within the already sorted left portion of the array.
Pseudocode for Insertion Sort
for i = 1 to n-1:
key = a[i]
j = i - 1
while j >= 0 and a[j] > key:
a[j+1] = a[j] // Shift element right
j = j - 1
a[j+1] = key // Insert keyBest-Case Performance: If the array is already sorted ascending, the inner while condition (a[j] > key) is immediately false for every iteration. This results in no element shifts, and Insertion Sort runs in O(n) time complexity.
C Program: Reading and Printing 1D Array Elements
This program demonstrates how to read the size and elements of a 1D array from the user at runtime and then print the contents.
#include <stdio.h>
int main() {
int n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
int a[n]; // Variable Length Array (VLA)
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) scanf("%d", &a[i]);
printf("Array elements are:\n");
for (i = 0; i < n; i++) printf("%d ", a[i]);
printf("\n");
return 0;
}Merge Sort Execution Example (16 Integers)
Input Array:22, 36, 6, 79, 26, 45, 75, 13, 31, 62, 27, 76, 33, 16, 62, 47
Merge Sort recursively splits the array until individual elements remain, then merges the sorted sub-arrays back together.
High-Level Splits and Merges
Initial Split:
- L =
22, 36, 6, 79, 26, 45, 75, 13 - R =
31, 62, 27, 76, 33, 16, 62, 47
- L =
Sorting L (Left Half):
- Split L:
22, 36, 6, 79and26, 45, 75, 13 - Sub-splits and Merges:
22, 36and6, 79→ Merge →6, 22, 36, 7926, 45and75, 13→ Sorted26, 45and13, 75→ Merge →13, 26, 45, 75
- Merge L parts:
6, 13, 22, 26, 36, 45, 75, 79
- Split L:
Sorting R (Right Half):
- Split R:
31, 62, 27, 76and33, 16, 62, 47 - Sub-splits and Merges:
31, 62and27, 76→ Merge →27, 31, 62, 7633, 16and62, 47→ Sorted16, 33and47, 62→ Merge →16, 33, 47, 62
- Merge R parts:
16, 27, 31, 33, 47, 62, 62, 76
- Split R:
Final Merge (L & R):
Merge
6, 13, 22, 26, 36, 45, 75, 79and16, 27, 31, 33, 47, 62, 62, 76
Final Sorted Array (Ascending)
6, 13, 16, 22, 26, 27, 31, 33, 36, 45, 47, 62, 62, 75, 76, 79
