This commit is contained in:
lda committed 2024-12-05 17:41:00 +07:00
1 parent d410e6cd5d
commit 5968efc884
6 files changed
+365 -68

No files matched your search

-1
View File
@@ -1,4 +1,3 @@
.vscode .vscode
exe exe
*.exe *.exe
drafts
File renamed without changes.
+235
View File
@@ -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;
}
+29
View File
@@ -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
View File
@@ -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;
}
-67
View File
@@ -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;
}