C Programming
Sorting with C Code; C Standard Library Functions
C-CAT
Sorting with C Code
Complete Sorting Program
#include <stdio.h>
#include <stdlib.h>
// Selection Sort
void selection_sort(int arr[], int n) {
for (int i = 0; i < n - 1;
i++) {
int min_idx = i;
for (int j = i + 1; j < n; j++) {
if
(arr[j] < arr[min_idx]) min_idx = j;
}
if (min_idx != i) {
int
temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
}
// Bubble Sort
void bubble_sort(int arr[], int n) {
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;
}
}
}
}
// Merge Sort
void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left
+ 1, n2 = right - mid;
int L[n1], R[n2];
for (int i=0; i<n1; i++) L[i] =
arr[left+i];
for (int j=0; j<n2; j++) R[j] = arr[mid+1+j];
int i=0, j=0, k=left;
while (i<n1 && j<n2) arr[k++] = (L[i]<=R[j]) ? L[i++] : R[j++];
while (i<n1) arr[k++] =
L[i++];
while (j<n2) arr[k++] = R[j++];
}
void merge_sort(int arr[], int l, int r) {
if (l < r) {
int m = l + (r-l)/2;
merge_sort(arr, l, m);
merge_sort(arr, m+1, r);
merge(arr, l, m, r);
}
}
// Quick Sort
int partition(int arr[], int low, int high) {
int pivot = arr[high], i =
low - 1;
for (int j=low; j<high; j++) {
if (arr[j] <= pivot) {
i++;
int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
}
}
int temp = arr[i+1];
arr[i+1] = arr[high]; arr[high] = temp;
return i+1;
}
void quick_sort(int arr[], int
low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quick_sort(arr, low, pi-1);
quick_sort(arr, pi+1, high);
}
}
// Linear Search
int linear_search(int arr[], int n, int target) {
for (int i = 0; i <
n; i++) {
if (arr[i] == target) return i;
}
return -1;
}
// Binary Search
int binary_search(int arr[], int n, int target) {
int low = 0, high = n
- 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if
(arr[mid] == target) return mid;
else if (arr[mid] < target) low = mid + 1;
else high = mid - 1;
}
return -1;
}
void print_array(int arr[], int n) {
for (int i=0; i<n; i++) printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {56, 4, 43, 33, 2};
int n = 5;
int temp[5];
for (int i=0;i<n;i++) temp[i]=arr[i];
selection_sort(temp, n);
printf("Selection Sort: "); print_array(temp, n);
for (int i=0;i<n;i++) temp[i]=arr[i];
merge_sort(temp, 0, n-1);
printf("Merge
Sort: "); print_array(temp, n);
for (int i=0;i<n;i++) temp[i]=arr[i];
quick_sort(temp, 0, n-1);
printf("Quick
Sort: "); print_array(temp, n);
printf("Linear search 43: index %d\n", linear_search(arr, n, 43)); // 2
// Sort
first for binary search
binary_search(temp, n, 43);
printf("Binary search 43: index
%d\n", binary_search(temp, n, 43)); // index in sorted array
return 0;
}
C Standard Library Functions
Mathematical Functions (<math.h>)
#include <math.h>
// NOTE: Compile with -lm: gcc prog.c -o prog -lm
printf("sqrt(16) = %.2f\n", sqrt(16.0)); // 4.00
printf("pow(2, 10) = %.0f\n", pow(2, 10)); // 1024
printf("abs(-5) = %d\n", abs(-5)); // 5
printf("fabs(-3.14) = %.2f\n", fabs(-3.14)); // 3.14
printf("ceil(3.2) = %.0f\n", ceil(3.2)); // 4
printf("floor(3.8) = %.0f\n", floor(3.8)); // 3
printf("round(3.5) = %.0f\n", round(3.5)); // 4
printf("log(2.718) = %.4f\n", log(2.718)); // ~1.0000 (natural log)
printf("log10(100) = %.1f\n", log10(100)); // 2.0
printf("sin(3.14159/2) = %.4f\n", sin(M_PI/2)); // 1.0000
stdlib.h Functions
#include <stdlib.h>
// Random numbers
srand(time(NULL)); // seed with current time
int r = rand();
// random int 0 to RAND_MAX
int dice = rand() % 6 + 1; // 1-6
// Exit
exit(0); // exit with success code
exit(EXIT_FAILURE); // exit with
failure
// Search and sort
int arr[] = {1, 3, 5, 7, 9};
int target = 7;
int *result =
bsearch(&target, arr, 5, sizeof(int),
[](const void *a, const void
*b) {
return *(int*)a - *(int*)b;
});
qsort(arr, 5, sizeof(int), [](const void *a, const void *b) {
return *(int*)a - *(int*)b;
});
Continue learning
Related notes
Definition of AI; Need of AI
Artificial Intelligence
Introduction to Data Engineering; Big Data — The 5 V's; Types of Data
Big Data and Data Engineering
Introduction to C Programming; C Program Structure; Data Types and Variables
C Programming
What Is a Computer?; Machine Cycle: Fetch–Decode–Execute; CPU Organization
Computer Architecture
Put this topic into timed practice
Open mock tests when you want full-exam pacing, or keep drilling in practice mode.