-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Raj Patil <rajp152k@gmail.com>
- Loading branch information
Showing
13 changed files
with
256 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
:PROPERTIES: | ||
:ID: 00d20a5b-be5e-44a4-a95f-44690883418d | ||
:END: | ||
#+title: Stable Sorting | ||
#+filetags: :algo:cs: | ||
|
||
* Resources | ||
- https://www.baeldung.com/cs/stable-sorting-algorithms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
:PROPERTIES: | ||
:ID: 7c0d5d3c-50a3-4ed1-ad97-7b3cde2462bc | ||
:END: | ||
#+title: Timsort | ||
#+filetags: :algo:cs: | ||
|
||
* Overview | ||
|
||
- *Definition*: Timsort is a hybrid sorting algorithm derived from [[id:fa43a1e8-2bee-47d3-98c6-6037a9b0f8ee][merge sort]] and [[id:c70dbfb7-1556-47d6-a590-c438e9662d91][insertion sort]]. | ||
- *Origin*: Developed by Tim Peters in 2002 for use in the [[id:985a470b-7184-4f9f-8b16-fe7b90bccebe][Python]] programming language. | ||
- *Stability*: Timsort is a [[id:00d20a5b-be5e-44a4-a95f-44690883418d][stable sort]], meaning it maintains the relative order of items that compare equal. | ||
- *Complexity*: | ||
- Best Case Time Complexity: O(n) when the input list is already sorted. | ||
- Average Case Time Complexity: O(n log n). | ||
- Worst Case Time Complexity: O(n log n). | ||
- *Space Complexity*: O(n) due to temporary arrays created during the merge process. | ||
- *Use Cases*: Suitable for real-world data which often contains ordered sequences (runs), such as sorting large datasets in Python or [[id:b056e747-dee4-4e6d-a7af-d644f842f0b8][Java]]. | ||
- *Adaptivity*: Timsort takes advantage of existing order in data by identifying runs (consecutive ordered sequences). | ||
|
||
*** Connections | ||
- *Hybrid Nature*: Combines strengths of insertion sort (efficient for small datasets or partially sorted data) and merge sort (efficient for larger datasets). | ||
- *Stability Significance*: Important in applications where the original order of equal elements is needed (e.g., in database records). | ||
- *Practical Performance*: Despite theoretical complexities, Timsort performs remarkably well on various datasets due to its adaptivity. | ||
|
||
* Mechanism of TimSort | ||
|
||
- Step 1: Identify Runs: | ||
- Timsort begins by partitioning the input array into small segments known as runs, which are either ordered ascending or descending sequences. | ||
- It uses a minimum run size, typically between 32 and 64 elements, to ensure efficient processing. | ||
|
||
- Step 2: Sort Each Run: | ||
- Each run is sorted using insertion sort, which is well-suited for small datasets due to its low overhead. | ||
|
||
- Step 3: Merge Runs: | ||
- Sorted runs are merged together using a modified merge sort algorithm. | ||
- The merging process takes care to maintain the stability of the sorting and uses a [[id:e20be945-5df7-4b35-aab5-a9a439b62de8][stack]] to keep track of runs. | ||
|
||
- Step 4: Manage Stack of Runs: | ||
- Runs are pushed onto a stack, and based on certain size constraints, Timsort merges those runs to ensure that the overall sorting remains efficient. | ||
- The merging strategy uses the principles of maintaining balanced merges, similar to a binary tree structure. | ||
|
||
- Step 5: Final Merge: | ||
- The process continues until all runs are merged into a final sorted array. | ||
|
||
* Resources | ||
- https://en.wikipedia.org/wiki/Timsort |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
:PROPERTIES: | ||
:ID: fa43a1e8-2bee-47d3-98c6-6037a9b0f8ee | ||
:END: | ||
#+title: Merge Sort | ||
#+filetags: :algo:cs: | ||
|
||
#+begin_src go :exports both | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func merge(a1 []int, a2 []int) []int { | ||
n, m := len(a1), len(a2) | ||
res := make([]int, n+m) | ||
i, j, k := 0, 0, 0 | ||
for i < n || j < m { | ||
if j == m || (i < n && a1[i] < a2[j]) { | ||
res[k] = a1[i] | ||
k++ | ||
i++ | ||
} else { | ||
res[k] = a2[j] | ||
k++ | ||
j++ | ||
} | ||
} | ||
return res | ||
} | ||
|
||
func mergeSort(arr []int) []int { | ||
if len(arr) < 2 { | ||
return arr | ||
} | ||
mid := len(arr) / 2 | ||
return merge(mergeSort(arr[:mid]), mergeSort(arr[mid:])) | ||
} | ||
|
||
func main() { | ||
arr := []int{4, 2, 5, 1, -3, -2} | ||
sortedArr := mergeSort(arr) | ||
fmt.Println(sortedArr) | ||
} | ||
#+end_src | ||
|
||
#+RESULTS: | ||
: [-3 -2 1 2 4 5] | ||
|
||
* Overview | ||
|
||
- *Definition*: Merge sort is a [[id:60121a6c-9dd8-4a17-8a87-15e8147ab228][divide-and-conquer]] algorithm that sorts an array by [[id:95edc4bc-c364-4b18-833a-ba476b3283e8][recursively]] dividing it into two halves, sorting each half, and then merging the sorted halves. | ||
|
||
*** Key Features: | ||
- *Time Complexity*: O(n log n) on average, best, and worst cases. | ||
- *Space Complexity*: O(n) due to the use of temporary arrays for merging. | ||
- *Stability*: It is a [[id:00d20a5b-be5e-44a4-a95f-44690883418d][stable sort]]; equal elements maintain their relative order. | ||
- *Adaptivity*: Not adaptive; it processes the same regardless of input order. | ||
|
||
*** Steps of the Algorithm: | ||
1. *Recursive Division*: | ||
- Split the input array into two halves. | ||
- Continue splitting until each sub-array has at most one element (base case). | ||
|
||
2. *Merging*: | ||
- Combine the sorted halves back together in a sorted manner by comparing the elements of both halves. | ||
|
||
3. *Base Case*: | ||
- An array of length 0 or 1 is already sorted. | ||
|
||
*** Misc | ||
- Merge sort is particularly effective for: | ||
- Sorting [[id:9a7e1b83-9160-40a7-821b-0f0ada44e350][linked lists]]. | ||
- Large datasets that do not fit into memory, as it can be adapted to external sorting. | ||
|
||
*** Questions for Clarification: | ||
- What specific aspects of merge sort do you wish to explore further (e.g., applications, comparisons, optimizations)? | ||
- Are you interested in examples of merge sort implementations in other programming languages? | ||
|
||
*** Pathways for Further Research: | ||
- How does merge sort compare with non-comparison-based sorting algorithms (e.g., radix sort, counting sort)? | ||
- What are the practical applications of merge sort in real-world systems and data processing? | ||
- Explore the theoretical limits of sorting algorithms: What are the proven lower bounds for comparison-based sorting? | ||
- Investigate optimizations of merge sort, particularly in adaptive contexts or with parallel processing techniques. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
:PROPERTIES: | ||
:ID: b056e747-dee4-4e6d-a7af-d644f842f0b8 | ||
:END: | ||
#+title: Java | ||
#+filetags: :java:programming: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
:PROPERTIES: | ||
:ID: e20be945-5df7-4b35-aab5-a9a439b62de8 | ||
:END: | ||
#+title: Stack | ||
#+filetags: :data:cs: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
:PROPERTIES: | ||
:ID: 4054ebe7-812f-4204-be63-bb7379d5b56b | ||
:END: | ||
#+title: Queue | ||
#+filetags: :data:cs: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
:PROPERTIES: | ||
:ID: 9a7e1b83-9160-40a7-821b-0f0ada44e350 | ||
:END: | ||
#+title: Linked lists | ||
#+filetags: :data:cs: |