This commit is contained in:
lda committed 2025-06-30 10:46:13 +07:00
1 parent b9b768fe45
commit 57c8ec1136
4 files changed
+113 -35

No files matched your search

+4 -1
View File
@@ -258,7 +258,10 @@ int main()
push_front(list, 10);
push_front(list, 20);
push_back(list, 30);
push_front(list, 30);
push_back(list, 40);
push_back(list, 50);
push_back(list, 60);
print_list(list);
printf("Popped from front: %lu\n", pop_front(list));
+86 -6
View File
@@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdlib.h> // For malloc and free
// Function to perform Cocktail Shaker Sort (Bidirectional Bubble Sort)
void cocktailShakerSort(int arr[], int n)
@@ -202,22 +203,101 @@ void merge(int arr[], int l, int m, int r)
free(right);
}
// Function to split the array and call merge on the halves
// Main function for Merge Sort
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
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
// Bubble Sort function
void bubbleSort(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])
{
swap(&arr[j], &arr[j + 1]);
}
}
}
}
// Partition function for Quick Sort
int partition(int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++)
{
if (arr[j] < pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
// Main function for Quick Sort
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
// Heapify function for Heap Sort
void heapify(int arr[], int n, int i)
{
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest])
{
largest = left;
}
if (right < n && arr[right] > arr[largest])
{
largest = right;
}
if (largest != i)
{
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}
// Main function for Heap Sort
void heapSort(int arr[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
{
heapify(arr, n, i);
}
for (int i = n - 1; i > 0; i--)
{
swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
}
}
int main()
{
int arr[] = {5, 1, 4, 2, 8, 0, 3, 12, 11, 64, 25, 12, 22, 11, 13, 5, 6, 7};