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

+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) |