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

+1 -1
View File
@@ -16,7 +16,7 @@ ldaArrayList number_to_list(long long int e)
else
ldalistpush(&c, 1);
for (; e > 0; e /= 10)
ldalistadd(&c, 1, (int)(e % 10));
ldalistadd(&c, 1, (int)(e % 10)); // next time use e % int::MAX you bitch ass mf
// SHIT IN (array rotato (it means bad))
ldalistprint(&c);
return c;
+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};
+22 -27
View File
@@ -202,27 +202,7 @@ void mergeSort(int arr[], int l, int r) {
Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order.
### How It Works
1. Iterate through the array from the first element to the last.
2. For each element, compare it with the next element.
3. If the current element is greater than the next element, swap them.
4. Repeat the process for the entire array until no swaps are needed in a full pass, indicating the array is sorted.
### Diagram
```mermaid
graph TD
subgraph "Pass 1"
A[5, 1, 4, 2, 8] --> B[1, 5, 4, 2, 8] --> C[1, 4, 5, 2, 8] --> D[1, 4, 2, 5, 8]
end
subgraph "Pass 2"
D --> E[1, 2, 4, 5, 8]
end
E --> F[Sorted]
```
### Implementation
### Code
```c
void bubbleSort(int arr[], int n) {
@@ -255,11 +235,13 @@ int partition(int arr[], int low, int high) {
for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
// Swap arr[i] and arr[j]
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
// Swap arr[i+1] and arr[high]
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
@@ -283,7 +265,7 @@ void quickSort(int arr[], int low, int high) {
## 7. Heap Sort
Heap Sort is a comparison-based sorting technique based on a Binary Heap data structure. It is similar to selection sort where we first find the maximum element and place the maximum element at the end. We repeat the same process for the remaining elements.
Heap Sort is a comparison-based sorting technique based on a Binary Heap data structure. It is similar to selection sort where we first find the maximum element and place the maximum element at the end.
### Heapify Function
@@ -293,13 +275,16 @@ void heapify(int arr[], int n, int i) {
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest])
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
if (right < n && arr[right] > arr[largest])
if (right < n && arr[right] > arr[largest]) {
largest = right;
}
if (largest != i) {
// Swap arr[i] and arr[largest]
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
@@ -312,10 +297,12 @@ void heapify(int arr[], int n, int i) {
```c
void heapSort(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--)
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
for (int i = n - 1; i > 0; i--) {
// Swap arr[0] and arr[i]
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
@@ -326,6 +313,14 @@ void heapSort(int arr[], int n) {
---
## Summary
## Time Complexity Summary
This guide has covered several common sorting algorithms. Each has its own trade-offs in terms of performance and complexity.
| Algorithm | Best Case | Average Case | Worst Case |
| -------------------- | :-------- | :----------- | :--------- |
| Bubble Sort | O(n) | O(n^2) | O(n^2) |
| Cocktail Shaker Sort | O(n) | O(n^2) | O(n^2) |
| Selection Sort | O(n^2) | O(n^2) | O(n^2) |
| Insertion Sort | O(n) | O(n^2) | O(n^2) |
| Merge Sort | O(n log n)| O(n log n) | O(n log n) |
| Quick Sort | O(n log n)| O(n log n) | O(n^2) |
| Heap Sort | O(n log n)| O(n log n) | O(n log n) |