structu
This commit is contained in:
1 parent
d410e6cd5d
commit
5968efc884
6 files changed
+365
-68
No files matched your search
@@ -1,51 +0,0 @@
|
||||
// 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;
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
#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");
|
||||
}
|
||||
|
||||
int main() {
|
||||
int arr[] = {5, 1, 4, 2, 8, 0, 3};
|
||||
int n = sizeof(arr) / sizeof(arr[0]);
|
||||
|
||||
printf("Original array: ");
|
||||
printArray(arr, n);
|
||||
|
||||
cocktailShakerSort(arr, n);
|
||||
|
||||
printf("Sorted array: ");
|
||||
printArray(arr, n);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user