├── .gitattributes ├── .gitignore ├── BenchRunner.ps1 ├── BubbleSort.ps1 ├── BucketSort.ps1 ├── CountingSort.ps1 ├── DoBench.ps1 ├── HeapSort.ps1 ├── InsertionSort.ps1 ├── MergeSort.ps1 ├── QuickSort.ps1 ├── README.md ├── SelectionSort.ps1 └── bench.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear on external disk 35 | .Spotlight-V100 36 | .Trashes 37 | 38 | # Directories potentially created on remote AFP share 39 | .AppleDB 40 | .AppleDesktop 41 | Network Trash Folder 42 | Temporary Items 43 | .apdisk 44 | -------------------------------------------------------------------------------- /BenchRunner.ps1: -------------------------------------------------------------------------------- 1 | $outFile = ".\bench.md" 2 | '' | Add-Content $outFile 3 | "## PowerShell Version: $(($PSVersionTable).PSVersion.ToString())" | Add-Content $outFile 4 | 5 | $r = .\DoBench.ps1 | Out-String 6 | 7 | '' | Add-Content $outFile 8 | '```' | Add-Content $outFile 9 | $r.Trim() | Add-Content $outFile 10 | '```' | Add-Content $outFile -------------------------------------------------------------------------------- /BubbleSort.ps1: -------------------------------------------------------------------------------- 1 | # https://github.com/ztgu/sorting_algorithms_py 2 | 3 | class BubbleSort { 4 | static Sort($targetList) { 5 | $n = $targetList.Count 6 | 7 | for ($i = 0; $i -lt $n; $i+=1) { 8 | for ($j = 0; $j -lt $n-1; $j+=1) { 9 | if($targetList[$j] -gt $targetList[$j+1]) { 10 | $temp = $targetList[$j+1] 11 | $targetList[$j+1]=$targetList[$j] 12 | $targetList[$j]=$temp 13 | } 14 | } 15 | } 16 | } 17 | } 18 | 19 | # $list = (1..1000 | %{Get-Random -Minimum 1 -Maximum 1000}) 20 | # [BubbleSort]::Sort($list) 21 | -------------------------------------------------------------------------------- /BucketSort.ps1: -------------------------------------------------------------------------------- 1 | class BucketSort { 2 | static Sort($targetList) { 3 | 4 | $max = $targetList[0] 5 | $min = $targetList[0] 6 | 7 | for ($i = 1; $i -lt $targetList.Count; $i++) { 8 | if ($targetList[$i] -gt $max) { $max = $targetList[$i] } 9 | if ($targetList[$i] -lt $min) { $min = $targetList[$i]} 10 | } 11 | 12 | $holder = New-Object object[][] ($max - $min + 1) 13 | 14 | for ($i = 0; $i -lt $holder.Count; $i++) { 15 | $holder[$i] = @() 16 | } 17 | 18 | for ($i = 0; $i -lt $targetList.Count; $i++) { 19 | $holder[$targetList[$i] - $min]+=$targetList[$i] 20 | } 21 | 22 | $k = 0 23 | 24 | for ($i = 0; $i -lt $holder.Count; $i++) { 25 | if ($holder[$i].Count -gt 0) { 26 | for ($j = 0; $j -lt $holder[$i].Count; $j++) { 27 | $targetList[$k] = $holder[$i][$j] 28 | $k++ 29 | } 30 | } 31 | } 32 | 33 | } 34 | } 35 | 36 | # $list = (1..1000 | %{Get-Random -Minimum 1 -Maximum 1000}) 37 | # [BucketSort]::Sort($list) 38 | -------------------------------------------------------------------------------- /CountingSort.ps1: -------------------------------------------------------------------------------- 1 | class CountingSort { 2 | static Sort($targetList) { 3 | $min = 0 4 | $max = 0 5 | 6 | for ($counter = 0; $counter -lt $targetList.Count; $counter++) { 7 | if ($targetList[$counter] -lt $min) { $min = $targetList[$counter] } 8 | if ($targetList[$counter] -gt $max) { $max = $targetList[$counter] } 9 | } 10 | 11 | $arrayBucket = New-Object int[] ($max - $min + 1) 12 | 13 | for ($counter = 0; $counter -lt $targetList.Count; $counter++) { 14 | $arrayBucket[$targetList[$counter]]++; 15 | } 16 | 17 | $lastPosition = 0 18 | for ($counter = 0; $counter -lt $arrayBucket.Count ; $counter++) { 19 | for ($innerCounter = 0; $innerCounter -lt $arrayBucket[$counter]; $innerCounter++) { 20 | $targetList[$lastPosition++] = $counter 21 | } 22 | } 23 | } 24 | } 25 | 26 | # $list = (1..1000 | %{Get-Random -Minimum 1 -Maximum 1000}) 27 | # [CountingSort]::Sort($list) 28 | 29 | -------------------------------------------------------------------------------- /DoBench.ps1: -------------------------------------------------------------------------------- 1 | #Requires -Modules Benchpress 2 | 3 | . .\BubbleSort.ps1 4 | . .\BucketSort.ps1 5 | . .\CountingSort.ps1 6 | . .\HeapSort.ps1 7 | . .\InsertionSort.ps1 8 | . .\MergeSort.ps1 9 | . .\QuickSort.ps1 10 | . .\SelectionSort.ps1 11 | 12 | function GenList { 13 | for ($i = 1; $i -lt 1001; $i++) { 14 | Get-Random -Minimum 1 -Maximum 1000 15 | } 16 | } 17 | 18 | Measure-Benchmark -RepeatCount 10 -Technique @{ 19 | BubbleSort = { 20 | [BubbleSort]::Sort((GenList)) 21 | } 22 | 23 | BucketSort = { 24 | GenList 25 | [BucketSort]::Sort((GenList)) 26 | } 27 | 28 | CountingSort = { 29 | GenList 30 | [CountingSort]::Sort((GenList)) 31 | } 32 | 33 | HeapSort = { 34 | GenList 35 | [HeapSort]::Sort((GenList)) 36 | } 37 | 38 | InsertionSort = { 39 | GenList 40 | [InsertionSort]::Sort((GenList)) 41 | } 42 | 43 | MergeSort = { 44 | GenList 45 | [MergeSort]::Sort((GenList)) 46 | } 47 | 48 | QuickSort = { 49 | $list=GenList 50 | [QuickSort]::Sort($list, 0, $list.Count-1) 51 | } 52 | 53 | SelectionSort = { 54 | 55 | [SelectionSort]::Sort((GenList)) 56 | } 57 | 58 | PowerShellSortObjectInObject = { 59 | $list=GenList 60 | Sort-Object -InputObject $list 61 | } 62 | 63 | PowerShellSortObjectPipe= { 64 | $list=GenList 65 | $list | Sort-Object 66 | } 67 | } -------------------------------------------------------------------------------- /HeapSort.ps1: -------------------------------------------------------------------------------- 1 | class HeapSort { 2 | static Sort($targetList) { 3 | $heapSize = $targetList.Count 4 | 5 | for ([int]$p = ($heapSize - 1) / 2; $p -ge 0; $p--) { 6 | [HeapSort]::MaxHeapify($targetList, $heapSize, $p) 7 | } 8 | 9 | for ($i = $targetList.Count - 1; $i -gt 0; $i--) { 10 | $temp = $targetList[$i] 11 | $targetList[$i] = $targetList[0] 12 | $targetList[0] = $temp 13 | 14 | $heapSize-- 15 | [HeapSort]::MaxHeapify($targetList, $heapSize, 0) 16 | } 17 | } 18 | 19 | static MaxHeapify($targetList, $heapSize, $index) { 20 | $left = ($index + 1) * 2 - 1 21 | $right = ($index + 1) * 2 22 | $largest = 0 23 | 24 | if ($left -lt $heapSize -and $targetList[$left] -gt $targetList[$index]) { 25 | $largest = $left 26 | } 27 | else { 28 | $largest = $index 29 | } 30 | 31 | if ($right -lt $heapSize -and $targetList[$right] -gt $targetList[$largest]) { 32 | $largest = $right 33 | } 34 | 35 | if ($largest -ne $index) { 36 | $temp = $targetList[$index] 37 | $targetList[$index] = $targetList[$largest] 38 | $targetList[$largest] = $temp 39 | 40 | [HeapSort]::MaxHeapify($targetList, $heapSize, $largest) 41 | } 42 | } 43 | } 44 | 45 | # $list = (1..1000 | %{Get-Random -Minimum 1 -Maximum 1000}) 46 | # [HeapSort]::Sort($list) 47 | -------------------------------------------------------------------------------- /InsertionSort.ps1: -------------------------------------------------------------------------------- 1 | class InsertionSort { 2 | static Sort($targetList) { 3 | $n = $targetList.count 4 | 5 | for ($i = 0; $i -lt $n - 1; $i++) { 6 | $j = $i + 1 7 | 8 | while ($j -gt 0) { 9 | 10 | if ($targetList[$j - 1] -gt $targetList[$j]) { 11 | $temp = $targetList[$j - 1] 12 | $targetList[$j - 1] = $targetList[$j] 13 | $targetList[$j] = $temp 14 | } 15 | 16 | $j-- 17 | } 18 | } 19 | } 20 | } 21 | 22 | # $list = (1..1000 | %{Get-Random -Minimum 1 -Maximum 1000}) 23 | # [InsertionSort]::Sort($list) 24 | -------------------------------------------------------------------------------- /MergeSort.ps1: -------------------------------------------------------------------------------- 1 | # https://gallery.technet.microsoft.com/scriptcenter/Merge-Sort-for-PowerShell-e5fdf3ab 2 | 3 | class MergeSort { 4 | 5 | static Merge($theArray, $tempArray, $leftPos, $rightPos, $rightEnd) { 6 | $leftEnd = $rightPos - 1 7 | $tmpPos = $leftPos 8 | $numElements = $rightEnd - $leftPos + 1 9 | 10 | while (($leftPos -le $leftEnd) -and ($rightPos -le $rightEnd)) { 11 | if ($theArray[$leftPos].CompareTo($theArray[$rightPos]) -le 0) { 12 | $tempArray[$tmpPos++] = $theArray[$leftPos++] 13 | } 14 | else { 15 | $tempArray[$tmpPos++] = $theArray[$rightPos++] 16 | } 17 | } 18 | 19 | while ($leftPos -le $leftEnd) { $tempArray[$tmpPos++] = $theArray[$leftPos++] } 20 | while ($rightPos -le $rightEnd) { $tempArray[$tmpPos++] = $theArray[$rightPos++] } 21 | 22 | for ($i = 0; $i -lt $numElements; $i++, $rightEnd--) { 23 | $theArray[$rightEnd] = $tempArray[$rightEnd] 24 | } 25 | } 26 | 27 | static Sort($theArray) { 28 | $tempArray = New-Object Object[] $theArray.Count 29 | [MergeSort]::Sort($theArray, $tempArray, 0, ($theArray.Count - 1)) 30 | } 31 | 32 | static Sort($theArray, $tempArray, $left, $right) { 33 | if ($left -lt $right) { 34 | 35 | $center = [Math]::Floor(($left + $right) / 2) 36 | 37 | [MergeSort]::Sort($theArray, $tempArray, $left, $center) 38 | [MergeSort]::Sort($theArray, $tempArray, ($center + 1), $right) 39 | 40 | [MergeSort]::Merge($theArray, $tempArray, $left, ($center + 1), $right) 41 | } 42 | } 43 | } 44 | 45 | # $list = (1..1000 | %{Get-Random -Minimum 1 -Maximum 1000}) 46 | # [MergeSort]::Sort($list) 47 | -------------------------------------------------------------------------------- /QuickSort.ps1: -------------------------------------------------------------------------------- 1 | class QuickSort { 2 | static Sort($targetList, $left, $right) { 3 | $i=$left 4 | $j=$right 5 | $pivot = $targetList[($left+$right)/2] 6 | 7 | while($i -le $j) { 8 | while($targetList[$i] -lt $pivot -and $i -lt $right) {$i++} 9 | while($targetList[$j] -gt $pivot -and $j -gt $left) {$j--} 10 | 11 | if($i -le $j) { 12 | $tmp = $targetList[$i] 13 | $targetList[$i]=$targetList[$j] 14 | $targetList[$j]=$tmp 15 | 16 | $i++ 17 | $j-- 18 | } 19 | } 20 | 21 | if($left -lt $j) {[QuickSort]::Sort($targetList, $left, $j)} 22 | if($i -lt $right) {[QuickSort]::Sort($targetList, $i, $right)} 23 | } 24 | } 25 | 26 | # $list = (1..1000 | ForEach{Get-Random -Minimum 1 -Maximum 1000}) 27 | # [QuickSort]::Sort($list, 0, $list.Count-1) 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sorting 2 | 3 | * Bubble Sort: `O(n^2)` 4 | * Bucket Sort: 5 | * Counting Sort: 6 | * Heap Sort: `O(n log(n))` 7 | * Insertion Sort: `O(n^2)` 8 | * Merge Sort: `O(n log(n))` 9 | * Quick Sort: `O(n log(n))` 10 | * Selection Sort: `O(n^2)` 11 | 12 | |Type|Description| 13 | |---|---| 14 | |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. The pass through the list is repeated until the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller or larger elements "bubble" to the top of the list.| 15 | |Bucket Sort|Is a sorting algorithm that works by distributing the elements of an array into a number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sorting algorithm. It is a distribution sort, a generalization of pigeonhole sort that allows multiple keys per bucket, and is a cousin of radix sort in the most-to-least significant digit flavor. Bucket sort can be implemented with comparisons and therefore can also be considered a comparison sort algorithm. The computational complexity depends on the algorithm used to sort each bucket, the number of buckets to use, and whether the input is uniformly distributed.| 16 | |Counting Sort|Is an algorithm for sorting a collection of objects according to keys that are small positive integers; that is, it is an integer sorting algorithm. It operates by counting the number of objects that possess distinct key values, and applying prefix sum on those counts to determine the positions of each key value in the output sequence. Its running time is linear in the number of items and the difference between the maximum key value and the minimum key value, so it is only suitable for direct use in situations where the variation in keys is not significantly greater than the number of items. It is often used as a subroutine in radix sort, another sorting algorithm, which can handle larger keys more efficiently.| 17 | |Heap Sort|Is a comparison-based sorting algorithm. Heapsort can be thought of as an improved selection sort: like selection sort, heapsort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element from it and inserting it into the sorted region. Unlike selection sort, heapsort does not waste time with a linear-time scan of the unsorted region; rather, heap sort maintains the unsorted region in a heap data structure to more quickly find the largest element in each step.| 18 | |Insertion Sort|Is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.| 19 | |Merge Sort|Is an efficient, general-purpose, and comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the order of equal elements is the same in the input and output. Merge sort is a divide-and-conquer algorithm that was invented by John von Neumann in 1945. A detailed description and analysis of bottom-up merge sort appeared in a report by Goldstine and von Neumann as early as 1948.| 20 | |Quick Sort|Is an in-place sorting algorithm. Developed by British computer scientist Tony Hoare in 1959 and published in 1961, it is still a commonly used algorithm for sorting. When implemented well, it can be somewhat faster than merge sort and about two or three times faster than heapsort.| 21 | |Selection Sort|Is an in-place comparison sorting algorithm. It has an O(n2) time complexity, which makes it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity and has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited.| 22 | 23 | # Benchmark 24 | 25 | ## PowerShell Version: 5.1.18362.145 26 | 27 | ``` 28 | Benchmark: DoBench 29 | 30 | Technique Time RelativeSpeed Throughput 31 | --------- ---- ------------- ---------- 32 | PowerShellSortObjectInObject 00:00:00.302804 1x 33.02/s 33 | PowerShellSortObjectPipe 00:00:00.405994 1.34x 24.63/s 34 | QuickSort 00:00:00.640130 2.11x 15.62/s 35 | CountingSort 00:00:00.641724 2.12x 15.58/s 36 | BucketSort 00:00:00.724440 2.39x 13.8/s 37 | SelectionSort 00:00:02.220003 7.33x 4.5/s 38 | MergeSort 00:00:02.255250 7.45x 4.43/s 39 | InsertionSort 00:00:02.901976 9.58x 3.45/s 40 | HeapSort 00:00:03.597627 11.88x 2.78/s 41 | BubbleSort 00:00:04.207017 13.89x 2.38/s 42 | ``` 43 | 44 | ## PowerShell Version: 7.0.0-preview.3 45 | 46 | ``` 47 | Benchmark: DoBench 48 | Technique Time RelativeSpeed Throughput 49 | --------- ---- ------------- ---------- 50 | PowerShellSortObjectInObject 00:00:00.246744 1x 40.53/s 51 | CountingSort 00:00:00.518265 2.1x 19.3/s 52 | QuickSort 00:00:00.519477 2.11x 19.25/s 53 | BucketSort 00:00:00.540518 2.19x 18.5/s 54 | PowerShellSortObjectPipe 00:00:00.825575 3.35x 12.11/s 55 | SelectionSort 00:00:01.285905 5.21x 7.78/s 56 | InsertionSort 00:00:01.652678 6.7x 6.05/s 57 | MergeSort 00:00:01.940894 7.87x 5.15/s 58 | BubbleSort 00:00:02.345025 9.5x 4.26/s 59 | HeapSort 00:00:03.111185 12.61x 3.21/s 60 | ``` 61 | 62 | ## PowerShell Version: 7.0.0-preview.5 63 | 64 | ``` 65 | Benchmark: DoBench 66 | 67 | Technique Time RelativeSpeed Throughput 68 | --------- ---- ------------- ---------- 69 | PowerShellSortObjectInObject 00:00:00.311540 1x 32.1/s 70 | PowerShellSortObjectPipe 00:00:00.351608 1.13x 28.44/s 71 | QuickSort 00:00:00.585106 1.88x 17.09/s 72 | CountingSort 00:00:00.627661 2.01x 15.93/s 73 | BucketSort 00:00:00.635744 2.04x 15.73/s 74 | SelectionSort 00:00:01.211441 3.89x 8.25/s 75 | InsertionSort 00:00:01.744976 5.6x 5.73/s 76 | MergeSort 00:00:02.047297 6.57x 4.88/s 77 | BubbleSort 00:00:02.317352 7.44x 4.32/s 78 | HeapSort 00:00:03.418777 10.97x 2.93/s 79 | ``` 80 | -------------------------------------------------------------------------------- /SelectionSort.ps1: -------------------------------------------------------------------------------- 1 | class SelectionSort { 2 | static Sort($targetList) { 3 | $n = $targetList.count 4 | 5 | for ($i = 0; $i -lt $n; $i++) { 6 | for ($j = $i + 1; $j -lt $n; $j++) { 7 | if ($targetList[$j] -lt $targetList[$i]) { 8 | $tmp = $targetList[$i] 9 | $targetList[$i] = $targetList[$j] 10 | $targetList[$j] = $tmp 11 | } 12 | } 13 | } 14 | } 15 | } 16 | 17 | # $list = (1..1000 | %{Get-Random -Minimum 1 -Maximum 1000}) 18 | # [SelectionSort]::Sort($list) 19 | -------------------------------------------------------------------------------- /bench.md: -------------------------------------------------------------------------------- 1 | 2 | ## PowerShell Version: 7.0.0-preview.5 3 | 4 | ``` 5 | Benchmark: DoBench 6 | 7 | Technique Time RelativeSpeed Throughput 8 | --------- ---- ------------- ---------- 9 | PowerShellSortObjectInObject 00:00:00.311540 1x 32.1/s 10 | PowerShellSortObjectPipe 00:00:00.351608 1.13x 28.44/s 11 | QuickSort 00:00:00.585106 1.88x 17.09/s 12 | CountingSort 00:00:00.627661 2.01x 15.93/s 13 | BucketSort 00:00:00.635744 2.04x 15.73/s 14 | SelectionSort 00:00:01.211441 3.89x 8.25/s 15 | InsertionSort 00:00:01.744976 5.6x 5.73/s 16 | MergeSort 00:00:02.047297 6.57x 4.88/s 17 | BubbleSort 00:00:02.317352 7.44x 4.32/s 18 | HeapSort 00:00:03.418777 10.97x 2.93/s 19 | ``` 20 | 21 | ## PowerShell Version: 7.3.3 22 | 23 | ``` 24 | Technique Time RelativeSpeed Throughput 25 | --------- ---- ------------- ---------- 26 | PowerShellSortObjectInObject 00:00:00.283801 1x 35.24/s 27 | PowerShellSortObjectPipe 00:00:00.354330 1.25x 28.22/s 28 | BucketSort 00:00:00.573679 2.02x 17.43/s 29 | CountingSort 00:00:00.579028 2.04x 17.27/s 30 | QuickSort 00:00:00.643806 2.27x 15.53/s 31 | SelectionSort 00:00:01.084849 3.82x 9.22/s 32 | InsertionSort 00:00:01.529397 5.39x 6.54/s 33 | BubbleSort 00:00:02.100335 7.4x 4.76/s 34 | MergeSort 00:00:02.417965 8.52x 4.14/s 35 | HeapSort 00:00:03.855357 13.58x 2.59/s 36 | ``` 37 | --------------------------------------------------------------------------------