structu
This commit is contained in:
1 parent
d410e6cd5d
commit
5968efc884
6 files changed
+365
-68
No files matched your search
@@ -0,0 +1,51 @@
|
||||
// feed in a pointer to a function that takes 2 and returns (just like the )
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <math.h>
|
||||
|
||||
int cmpfunc(const void *a, const void *b)
|
||||
{
|
||||
const int *A = a, *B = b;
|
||||
return (*A > *B) - (*A < *B);
|
||||
}
|
||||
int _centeredCube(int num) {
|
||||
int low = -num, high = num, mid, result = 0;
|
||||
while (low <= high) {
|
||||
mid = low + (high - low) / 2;
|
||||
|
||||
long long thisSum = (long long) pow(mid, 3) + (long long) pow(mid + 1, 3); // Avoid overflow
|
||||
|
||||
if (thisSum == (long long) num)
|
||||
return mid;
|
||||
else if (thisSum < num) {
|
||||
result = mid; // Store the best approximation so far
|
||||
low = mid + 1;
|
||||
} else {
|
||||
high = mid - 1;
|
||||
}
|
||||
}
|
||||
return result; // Return the closest guess when exact match is not found
|
||||
};
|
||||
bool centerCube(int num) {
|
||||
int n = _centeredCube(num);
|
||||
long long thisSum = (long long) pow(n, 3) + (long long) pow(n + 1, 3);
|
||||
printf("n = %d, thisSum = %lld, num = %d\n", n, thisSum, num);
|
||||
return thisSum == (long long) num;
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
int a[13] = {1, 2, 3, 7, 3, 9, 11, 13, 42, -1, 4, 52, 10};
|
||||
qsort(a, 13, sizeof(int), cmpfunc);
|
||||
for (int i = 0; i < 13; i++)
|
||||
printf("%d ", a[i]);
|
||||
printf("\n");
|
||||
int at = 5;
|
||||
int *here = (int *)bsearch(&at, a, 13, sizeof(int), cmpfunc);
|
||||
if (here == NULL)
|
||||
printf("no %d ", at);
|
||||
else
|
||||
printf("is %d ", *here);
|
||||
return 0;
|
||||
}
|
||||
+235
@@ -0,0 +1,235 @@
|
||||
#include <stdio.h>
|
||||
|
||||
// Function to perform Cocktail Shaker Sort (Bidirectional Bubble Sort)
|
||||
void cocktailShakerSort(int arr[], int n)
|
||||
{
|
||||
int swapped = 1; // To track if any swaps were made
|
||||
int start = 0; // Starting index
|
||||
int end = n - 1; // Ending index
|
||||
|
||||
while (swapped)
|
||||
{
|
||||
swapped = 0;
|
||||
|
||||
// Forward pass: left to right
|
||||
for (int i = start; i < end; ++i)
|
||||
{
|
||||
if (arr[i] > arr[i + 1])
|
||||
{
|
||||
// Swap if the current element is greater than the next
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[i + 1];
|
||||
arr[i + 1] = temp;
|
||||
swapped = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// If no swaps occurred, the array is sorted
|
||||
if (!swapped)
|
||||
break;
|
||||
|
||||
// Reduce the end point since the last element is sorted
|
||||
--end;
|
||||
|
||||
// Backward pass: right to left
|
||||
for (int i = end - 1; i >= start; --i)
|
||||
{
|
||||
if (arr[i] > arr[i + 1])
|
||||
{
|
||||
// Swap if the current element is greater than the next
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[i + 1];
|
||||
arr[i + 1] = temp;
|
||||
swapped = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Increment the start point since the first element is sorted
|
||||
++start;
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to print the array
|
||||
void printArray(int arr[], int n)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
printf("%d ", arr[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
// Swap function to swap two elements
|
||||
void swap(int *a, int *b) {
|
||||
int temp = *a;
|
||||
*a = *b;
|
||||
*b = temp;
|
||||
}
|
||||
|
||||
// Selection Sort function
|
||||
void selectionSort(int arr[], int n) {
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
int minIndex = i; // Assume the current element is the minimum
|
||||
|
||||
// Find the index of the minimum element in the unsorted portion
|
||||
for (int j = i + 1; j < n; j++) {
|
||||
if (arr[j] < arr[minIndex]) {
|
||||
minIndex = j;
|
||||
}
|
||||
}
|
||||
|
||||
// Swap the minimum element with the first element of the unsorted portion
|
||||
if (minIndex != i) {
|
||||
swap(&arr[i], &arr[minIndex]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int findMinIndex(int arr[], int start, int n) {
|
||||
int minIndex = start;
|
||||
for (int i = start + 1; i < n; i++) {
|
||||
if (arr[i] < arr[minIndex]) {
|
||||
minIndex = i;
|
||||
}
|
||||
}
|
||||
return minIndex;
|
||||
}
|
||||
|
||||
// Recursive Selection Sort function
|
||||
void recursiveSelectionSort(int arr[], int start, int n) {
|
||||
if (start >= n - 1) {
|
||||
return; // Base case: when there's only one element left, array is sorted
|
||||
}
|
||||
|
||||
// Find the minimum element in the remaining unsorted array
|
||||
int minIndex = findMinIndex(arr, start, n);
|
||||
|
||||
// Swap it with the first element of the unsorted portion
|
||||
if (minIndex != start) {
|
||||
swap(&arr[start], &arr[minIndex]);
|
||||
}
|
||||
|
||||
// Recur for the remaining unsorted portion
|
||||
recursiveSelectionSort(arr, start + 1, n);
|
||||
}
|
||||
|
||||
|
||||
void insertionSort(int arr[], int n) {
|
||||
for (int i = 1; i < n; i++) {
|
||||
for (int j = i; j > 0 && arr[j] < arr[j - 1]; j--) {
|
||||
// Swap if the current element is smaller than the previous one
|
||||
swap(&arr[j], &arr[j - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to insert element in the sorted portion recursively
|
||||
void insertElement(int arr[], int n) {
|
||||
if (n <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
insertElement(arr, n - 1); // Sort the first n-1 elements
|
||||
|
||||
// Now insert the nth element into the sorted portion
|
||||
for (int j = n; j > 0 && arr[j] < arr[j - 1]; j--) {
|
||||
swap(&arr[j], &arr[j - 1]);
|
||||
}
|
||||
}
|
||||
|
||||
// Recursive Insertion Sort function
|
||||
void recursiveInsertionSort(int arr[], int n) {
|
||||
if (n <= 1) {
|
||||
return; // Base case: when the array has 1 or fewer elements, it's sorted
|
||||
}
|
||||
|
||||
recursiveInsertionSort(arr, n - 1); // Sort the first n-1 elements
|
||||
|
||||
insertElement(arr, n - 1); // Insert the nth element in the correct position
|
||||
}
|
||||
|
||||
// Merge two sorted subarrays arr[l..m] and arr[m+1..r]
|
||||
void merge(int arr[], int l, int m, int r)
|
||||
{
|
||||
int n1 = m - l + 1; // Size of the left subarray
|
||||
int n2 = r - m; // Size of the right subarray
|
||||
|
||||
// Temporary arrays to hold the values
|
||||
int *left = (int *)malloc(n1 * sizeof(int));
|
||||
int *right = (int *)malloc(n2 * sizeof(int));
|
||||
|
||||
// Copy data to temp arrays
|
||||
for (int i = 0; i < n1; i++)
|
||||
left[i] = arr[l + i];
|
||||
for (int i = 0; i < n2; i++)
|
||||
right[i] = arr[m + 1 + i];
|
||||
|
||||
// Merge the temp arrays back into arr[l..r]
|
||||
int i = 0, j = 0, k = l;
|
||||
while (i < n1 && j < n2)
|
||||
{
|
||||
if (left[i] <= right[j])
|
||||
{
|
||||
arr[k] = left[i];
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
arr[k] = right[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of left[], if any
|
||||
while (i < n1)
|
||||
{
|
||||
arr[k] = left[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of right[], if any
|
||||
while (j < n2)
|
||||
{
|
||||
arr[k] = right[j];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
free(left);
|
||||
free(right);
|
||||
}
|
||||
|
||||
// Function to split the array and call merge on the halves
|
||||
void mergeSort(int arr[], int l, int r)
|
||||
{
|
||||
if (l < r)
|
||||
{
|
||||
int m = l + (r - l) / 2; // Find the middle point
|
||||
|
||||
// Recursively sort the first and second halves
|
||||
mergeSort(arr, l, m);
|
||||
mergeSort(arr, m + 1, r);
|
||||
|
||||
// Merge the sorted halves
|
||||
merge(arr, l, m, r);
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int arr[] = {5, 1, 4, 2, 8, 0, 3, 12, 11, 64, 25, 12, 22, 11, 13, 5, 6, 7};
|
||||
int n = sizeof(arr) / sizeof(arr[0]);
|
||||
|
||||
printf("Original array: ");
|
||||
printArray(arr, n);
|
||||
// cocktailShakerSort(arr, n);
|
||||
// mergeSort(arr, 0, n - 1);
|
||||
|
||||
printf("Sorted array: ");
|
||||
printArray(arr, n);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <stdio.h>
|
||||
int arrPrinter(int *l, int sz)
|
||||
{
|
||||
printf("array (size %d): [", sz);
|
||||
for (
|
||||
int i = 0;
|
||||
(printf("%d", l[i]) != 0) &&
|
||||
(++i < sz);
|
||||
printf(", "))
|
||||
;
|
||||
printf("]\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
int l[8] = {1, 2, 4, 6, 8, 11, 42, 76};
|
||||
printf("array l = [");
|
||||
for (
|
||||
int i = 0;
|
||||
(printf("%d", l[i]) != 0) &&
|
||||
(++i < 8);
|
||||
printf(", "))
|
||||
;
|
||||
printf("]\n");
|
||||
|
||||
arrPrinter(l, 8);
|
||||
return 0;
|
||||
}
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
int *array; // Dynamic array to store heap elements
|
||||
int size; // Current size of the heap
|
||||
int capacity; // Maximum capacity of the heap
|
||||
} MaxHeap;
|
||||
|
||||
// Function to create a max heap
|
||||
MaxHeap* createMaxHeap(int capacity) {
|
||||
MaxHeap *heap = (MaxHeap *)malloc(sizeof(MaxHeap));
|
||||
heap->capacity = capacity;
|
||||
heap->size = 0;
|
||||
heap->array = (int *)malloc(capacity * sizeof(int));
|
||||
return heap;
|
||||
}
|
||||
|
||||
// Function to swap two elements
|
||||
void swap(int *x, int *y) {
|
||||
int temp = *x;
|
||||
*x = *y;
|
||||
*y = temp;
|
||||
}
|
||||
|
||||
// Function to heapify a subtree rooted with node i
|
||||
void heapify(MaxHeap *heap, int i) {
|
||||
int largest = i; // Initialize largest as root
|
||||
int left = 2 * i + 1; // left child index
|
||||
int right = 2 * i + 2; // right child index
|
||||
|
||||
// If left child is larger than root
|
||||
if (left < heap->size && heap->array[left] > heap->array[largest]) {
|
||||
largest = left;
|
||||
}
|
||||
|
||||
// If right child is larger than largest so far
|
||||
if (right < heap->size && heap->array[right] > heap->array[largest]) {
|
||||
largest = right;
|
||||
}
|
||||
|
||||
// If largest is not root, swap and continue heapifying
|
||||
if (largest != i) {
|
||||
swap(&heap->array[i], &heap->array[largest]);
|
||||
heapify(heap, largest);
|
||||
}
|
||||
}
|
||||
|
||||
// Function to insert a new element to the heap
|
||||
void insert(MaxHeap *heap, int key) {
|
||||
if (heap->size == heap->capacity) {
|
||||
printf("Heap is full, cannot insert %d\n", key);
|
||||
return;
|
||||
}
|
||||
|
||||
// First insert the new key at the end
|
||||
heap->array[heap->size] = key;
|
||||
heap->size++;
|
||||
|
||||
// Fix the max heap property if it is violated
|
||||
for (int i = heap->size / 2 - 1; i >= 0; i--) {
|
||||
heapify(heap, i);
|
||||
}
|
||||
}
|
||||
|
||||
// Function to display the max heap
|
||||
void displayHeap(MaxHeap *heap) {
|
||||
printf("Max Heap: ");
|
||||
for (int i = 0; i < heap->size; i++) {
|
||||
printf("%d ", heap->array[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// Main function
|
||||
int main() {
|
||||
int capacity;
|
||||
printf("Enter the maximum capacity of the heap: ");
|
||||
scanf("%d", &capacity);
|
||||
|
||||
MaxHeap *heap = createMaxHeap(capacity);
|
||||
|
||||
int numElements;
|
||||
printf("Enter the number of elements to insert into the heap: ");
|
||||
scanf("%d", &numElements);
|
||||
|
||||
for (int i = 0; i < numElements; i++) {
|
||||
int value;
|
||||
printf("Enter value %d: ", i + 1);
|
||||
scanf("%d", &value);
|
||||
insert(heap, value);
|
||||
}
|
||||
|
||||
displayHeap(heap);
|
||||
|
||||
// Free allocated memory
|
||||
free(heap->array);
|
||||
free(heap);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user