├── README.md ├── searching ├── .gitignore ├── Binary.java ├── README.md ├── binary.js ├── binary.pl ├── binary.py └── binary.rb └── sorting ├── README.md ├── java ├── Bubble.java ├── Heap.java ├── Insertion.java ├── Merge.java ├── Quick.java ├── Selection.java └── Shell.java ├── js └── bubble.js ├── perl ├── bubbble.pl ├── headp.pl ├── heap.pl ├── insertion.pl ├── merge.pl ├── quick.pl ├── selection.pl └── shell.pl ├── python ├── bubble.py ├── heap.py ├── insertion.py ├── merge.py ├── qucik.py ├── selection.py └── shell.py └── ruby ├── .gitignore ├── bubble.rb ├── heap.rb ├── insertion.rb ├── merge.rb ├── quick.rb ├── selection.rb └── shell.rb /README.md: -------------------------------------------------------------------------------- 1 | >Implementation of commonly used algorithms. 2 | 3 | Searching and sorting alogrithms implemented in: 4 | 5 | * Python 6 | 7 | * Ruby 8 | 9 | * Perl 10 | 11 | * Java 12 | 13 | * JS 14 | 15 | === 16 | 17 | ##Pesudocodes of the Alogrithms : 18 | 19 | #Binary search O(log n) 20 | 21 | ##Recursive 22 | BinarySearch(A[0..N-1], value, low, high) { 23 | if (high < low) 24 | return not_found 25 | mid = (low + high) / 2 26 | if (A[mid] > value) 27 | return BinarySearch(A, value, low, mid-1) 28 | else if (A[mid] < value) 29 | return BinarySearch(A, value, mid+1, high) 30 | else 31 | return mid 32 | } 33 | 34 | 35 | #I List of sorting algorithms with complexity O( n^2 ) 36 | 37 | #0. Bubble Sort : 38 | 39 | repeat 40 | hasChanged := false 41 | decrement itemCount 42 | repeat with index from 1 to itemCount 43 | if (item at index) > (item at (index + 1)) 44 | swap (item at index) with (item at (index + 1)) 45 | hasChanged := true 46 | until hasChanged = false 47 | 48 | #1. Insertion Sort : 49 | 50 | function insertionSort(array A) 51 | for i from 1 to length[A]-1 do 52 | value := A[i] 53 | j := i-1 54 | while j >= 0 and A[j] > value do 55 | A[j+1] := A[j] 56 | j := j-1 57 | done 58 | A[j+1] = value 59 | done 60 | 61 | #2. Selection Sort : 62 | 63 | function selectionSort(array A) 64 | max = length(A) - 1 65 | 66 | for i from 0 to max 67 | key = A[i] 68 | keyj = i 69 | 70 | for j from i+1 to max 71 | if A[j] < key 72 | key = A[j] 73 | keyj = j 74 | 75 | lst[keyj] = A[i] 76 | lst[i] = key 77 | done 78 | 79 | 80 | ## II Algorithms with complexity as O(n log n) 81 | 82 | #0. Heap Sort : 83 | 84 | function heapSort(a, count) is 85 | input: an unordered array a of length count 86 | 87 | (first place a in max-heap order) 88 | heapify(a, count) 89 | 90 | end := count - 1 91 | while end > 0 do 92 | (swap the root(maximum value) of the heap with the 93 | last element of the heap) 94 | swap(a[end], a[0]) 95 | (put the heap back in max-heap order) 96 | siftDown(a, 0, end-1) 97 | (decrement the size of the heap so that the previous 98 | max value will stay in its proper place) 99 | end := end - 1 100 | 101 | function heapify(a,count) is 102 | (start is assigned the index in a of the last parent node) 103 | start := (count - 2) / 2 104 | 105 | while start ≥ 0 do 106 | (sift down the node at index start to the proper place 107 | such that all nodes below the start index are in heap 108 | order) 109 | siftDown(a, start, count-1) 110 | start := start - 1 111 | (after sifting down the root all nodes/elements are in heap order) 112 | 113 | function siftDown(a, start, end) is 114 | (end represents the limit of how far down the heap to sift) 115 | root := start 116 | 117 | while root * 2 + 1 ≤ end do (While the root has at least one child) 118 | child := root * 2 + 1 (root*2+1 points to the left child) 119 | (If the child has a sibling and the child's value is less than its sibling's...) 120 | if child + 1 ≤ end and a[child] < a[child + 1] then 121 | child := child + 1 (... then point to the right child instead) 122 | if a[root] < a[child] then (out of max-heap order) 123 | swap(a[root], a[child]) 124 | root := child (repeat to continue sifting down the child now) 125 | else 126 | return 127 | 128 | # 1. Merge Sort: 129 | 130 | function mergesort(m) 131 | var list left, right, result 132 | if length(m) ≤ 1 133 | return m 134 | else 135 | var middle = length(m) / 2 136 | for each x in m up to middle - 1 137 | add x to left 138 | for each x in m at and after middle 139 | add x to right 140 | left = mergesort(left) 141 | right = mergesort(right) 142 | if last(left) ≤ first(right) 143 | append right to left 144 | return left 145 | result = merge(left, right) 146 | return result 147 | 148 | function merge(left,right) 149 | var list result 150 | while length(left) > 0 and length(right) > 0 151 | if first(left) ≤ first(right) 152 | append first(left) to result 153 | left = rest(left) 154 | else 155 | append first(right) to result 156 | right = rest(right) 157 | if length(left) > 0 158 | append rest(left) to result 159 | if length(right) > 0 160 | append rest(right) to result 161 | return result 162 | 163 | # 2. Quick Sort: 164 | 165 | function quicksort(array) 166 | var list lessOrEqual, greater 167 | if length(array) ≤ 1 168 | return array 169 | pivot := select a pivot value 170 | for each x in array 171 | if x ≤ pivot then add x to lessOrEqual 172 | if x > pivot then add x to greater 173 | return concatenate(quicksort(lessOrEqual), pivot, quicksort(greater)) 174 | 175 | 176 | ##III Alogrithm with O(n log^2 n) 177 | 178 | #0. Shell Sort 179 | 180 | input: an array a of length n with array elements numbered 0 to n − 1 181 | 182 | inc ← round(n/2) 183 | while inc > 0 do: 184 | for i = inc .. n − 1 do: 185 | temp ← a[i] 186 | j ← i 187 | while j ≥ inc and a[j − inc] > temp do: 188 | a[j] ← a[j − inc] 189 | j ← j − inc 190 | a[j] ← temp 191 | inc ← round(inc / 2.2) 192 | 193 | 194 | -------------------------------------------------------------------------------- /searching/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hemanth/algorithms/3312b595d7790424f3137f5081c763618a2bdb23/searching/.gitignore -------------------------------------------------------------------------------- /searching/Binary.java: -------------------------------------------------------------------------------- 1 | // Arrays.binarySearch or Collections.binarySearch can be used 2 | int binarySearch(int[] tosearch, int key, int lb, int ub) { 3 | 4 | if (lb > ub) 5 | 6 | return -1; 7 | 8 | int middle = (lb + ub) / 2; 9 | 10 | if (array[middle] == key) 11 | 12 | return middle; 13 | 14 | else if (array[middle] > key) 15 | 16 | return binarySearch(array, key, lb, middle - 1); 17 | 18 | else 19 | 20 | return binarySearch(array, key, middle + 1, ub); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /searching/README.md: -------------------------------------------------------------------------------- 1 | ##Pesudocodes of the Alogrithms : 2 | 3 | #Binary search O(log n) 4 | 5 | ##Recursive 6 | BinarySearch(A[0..N-1], value, low, high) { 7 | if (high < low) 8 | return not_found 9 | mid = (low + high) / 2 10 | if (A[mid] > value) 11 | return BinarySearch(A, value, low, mid-1) 12 | else if (A[mid] < value) 13 | return BinarySearch(A, value, mid+1, high) 14 | else 15 | return mid 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /searching/binary.js: -------------------------------------------------------------------------------- 1 | function binary_search(hay, value) { 2 | low = 0; 3 | high = hay.length - 1; 4 | while (low <= high) { 5 | var mid = Math.floor((lo+hi)/2); 6 | if (a[mid] > value) 7 | high = mid - 1; 8 | else if (a[mid] < value) 9 | low = mid + 1; 10 | else 11 | return mid; 12 | } 13 | return null; 14 | } 15 | -------------------------------------------------------------------------------- /searching/binary.pl: -------------------------------------------------------------------------------- 1 | #!/bin/perl 2 | sub binary_search { 3 | ($array_ref, $value, $left, $right) = @_; 4 | if ($right < $left) { 5 | return 0; 6 | } 7 | $middle = ($right + $left) / 2; 8 | if ($array_ref->[$middle] == $value) { 9 | return 1; 10 | } 11 | if ($value < $array_ref->[$middle]) { 12 | binary_search($array_ref, $value, $left, $middle - 1); 13 | } else { 14 | binary_search($array_ref, $value, $middle + 1, $right); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /searching/binary.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | def binary_search(searchMe, key, low = 0, high = -1): 3 | if(high == -1): high = len(l)-1 4 | if low == high: 5 | if searchMe[low] == key: return low 6 | else: return -1 7 | mid = (low+high)//2 8 | if searchMe[mid] > key: return binary_search(searchMe, value, low, mid-1) 9 | elif searchMe[mid] < key: return binary_search(searchMe, value, mid+1, high) 10 | else: return mid 11 | -------------------------------------------------------------------------------- /searching/binary.rb: -------------------------------------------------------------------------------- 1 | #!/bin/ruby 2 | class Array 3 | def binary_search(val, low=0, high=(length - 1)) 4 | return nil if high < low 5 | mid = (low + high) / 2 6 | case 7 | when self[mid] > val then binary_search(val, low, mid-1) 8 | when self[mid] < val then binary_search(val, mid+1, high) 9 | else mid 10 | end 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /sorting/README.md: -------------------------------------------------------------------------------- 1 | ##Pesudocodes of the Alogrithms : 2 | 3 | #I List of sorting algorithms with complexity O( n^2 ) 4 | 5 | #0. Bubble Sort : 6 | 7 | repeat 8 | hasChanged := false 9 | decrement itemCount 10 | repeat with index from 1 to itemCount 11 | if (item at index) > (item at (index + 1)) 12 | swap (item at index) with (item at (index + 1)) 13 | hasChanged := true 14 | until hasChanged = false 15 | 16 | #1. Insertion Sort : 17 | 18 | function insertionSort(array A) 19 | for i from 1 to length[A]-1 do 20 | value := A[i] 21 | j := i-1 22 | while j >= 0 and A[j] > value do 23 | A[j+1] := A[j] 24 | j := j-1 25 | done 26 | A[j+1] = value 27 | done 28 | 29 | #2. Selection Sort : 30 | 31 | function selectionSort(array A) 32 | max = length(A) - 1 33 | 34 | for i from 0 to max 35 | key = A[i] 36 | keyj = i 37 | 38 | for j from i+1 to max 39 | if A[j] < key 40 | key = A[j] 41 | keyj = j 42 | 43 | lst[keyj] = A[i] 44 | lst[i] = key 45 | done 46 | 47 | 48 | ## II Algorithms with complexity as O(n log n) 49 | 50 | #0. Heap Sort : 51 | 52 | function heapSort(a, count) is 53 | input: an unordered array a of length count 54 | 55 | (first place a in max-heap order) 56 | heapify(a, count) 57 | 58 | end := count - 1 59 | while end > 0 do 60 | (swap the root(maximum value) of the heap with the 61 | last element of the heap) 62 | swap(a[end], a[0]) 63 | (put the heap back in max-heap order) 64 | siftDown(a, 0, end-1) 65 | (decrement the size of the heap so that the previous 66 | max value will stay in its proper place) 67 | end := end - 1 68 | 69 | function heapify(a,count) is 70 | (start is assigned the index in a of the last parent node) 71 | start := (count - 2) / 2 72 | 73 | while start ≥ 0 do 74 | (sift down the node at index start to the proper place 75 | such that all nodes below the start index are in heap 76 | order) 77 | siftDown(a, start, count-1) 78 | start := start - 1 79 | (after sifting down the root all nodes/elements are in heap order) 80 | 81 | function siftDown(a, start, end) is 82 | (end represents the limit of how far down the heap to sift) 83 | root := start 84 | 85 | while root * 2 + 1 ≤ end do (While the root has at least one child) 86 | child := root * 2 + 1 (root*2+1 points to the left child) 87 | (If the child has a sibling and the child's value is less than its sibling's...) 88 | if child + 1 ≤ end and a[child] < a[child + 1] then 89 | child := child + 1 (... then point to the right child instead) 90 | if a[root] < a[child] then (out of max-heap order) 91 | swap(a[root], a[child]) 92 | root := child (repeat to continue sifting down the child now) 93 | else 94 | return 95 | 96 | # 1. Merge Sort: 97 | 98 | function mergesort(m) 99 | var list left, right, result 100 | if length(m) ≤ 1 101 | return m 102 | else 103 | var middle = length(m) / 2 104 | for each x in m up to middle - 1 105 | add x to left 106 | for each x in m at and after middle 107 | add x to right 108 | left = mergesort(left) 109 | right = mergesort(right) 110 | if last(left) ≤ first(right) 111 | append right to left 112 | return left 113 | result = merge(left, right) 114 | return result 115 | 116 | function merge(left,right) 117 | var list result 118 | while length(left) > 0 and length(right) > 0 119 | if first(left) ≤ first(right) 120 | append first(left) to result 121 | left = rest(left) 122 | else 123 | append first(right) to result 124 | right = rest(right) 125 | if length(left) > 0 126 | append rest(left) to result 127 | if length(right) > 0 128 | append rest(right) to result 129 | return result 130 | 131 | # 2. Quick Sort: 132 | 133 | function quicksort(array) 134 | var list lessOrEqual, greater 135 | if length(array) ≤ 1 136 | return array 137 | pivot := select a pivot value 138 | for each x in array 139 | if x ≤ pivot then add x to lessOrEqual 140 | if x > pivot then add x to greater 141 | return concatenate(quicksort(lessOrEqual), pivot, quicksort(greater)) 142 | 143 | 144 | ##III Alogrithm with O(n log^2 n) 145 | 146 | #0. Shell Sort 147 | 148 | input: an array a of length n with array elements numbered 0 to n − 1 149 | 150 | inc ← round(n/2) 151 | while inc > 0 do: 152 | for i = inc .. n − 1 do: 153 | temp ← a[i] 154 | j ← i 155 | while j ≥ inc and a[j − inc] > temp do: 156 | a[j] ← a[j − inc] 157 | j ← j − inc 158 | a[j] ← temp 159 | inc ← round(inc / 2.2) 160 | 161 | 162 | -------------------------------------------------------------------------------- /sorting/java/Bubble.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author hemanth.hm 3 | * Site : www.h3manth.com 4 | * 5 | */ 6 | public class BubbleSort { 7 | 8 | /** 9 | * @param args 10 | */ 11 | public static > void bubbleSort(E[] comparable) { 12 | boolean changed = false; 13 | do { 14 | changed = false; 15 | for (int a = 0; a < comparable.length - 1; a++) { 16 | if (comparable[a].compareTo(comparable[a + 1]) > 0) { 17 | E tmp = comparable[a]; 18 | comparable[a] = comparable[a + 1]; 19 | comparable[a + 1] = tmp; 20 | changed = true; 21 | } 22 | } 23 | } while (changed); 24 | } 25 | 26 | @SuppressWarnings("unchecked") 27 | public static void main(String[] args) { 28 | Comparable tosort [] = {0,1,-2,-4,6,8,9,99}; 29 | 30 | BubbleSort.bubbleSort(tosort); 31 | 32 | for (Comparable sorted : tosort) { 33 | System.out.println(sorted); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /sorting/java/Heap.java: -------------------------------------------------------------------------------- 1 | public static void heapSort(int[] a){ 2 | int count = a.length; 3 | 4 | //first place a in max-heap order 5 | heapify(a, count); 6 | 7 | int end = count - 1; 8 | while(end > 0){ 9 | //swap the root(maximum value) of the heap with the 10 | //last element of the heap 11 | int tmp = a[end]; 12 | a[end] = a[0]; 13 | a[0] = tmp; 14 | //put the heap back in max-heap order 15 | siftDown(a, 0, end - 1); 16 | //decrement the size of the heap so that the previous 17 | //max value will stay in its proper place 18 | end--; 19 | } 20 | } 21 | 22 | public static void heapify(int[] a, int count){ 23 | //start is assigned the index in a of the last parent node 24 | int start = (count - 2) / 2; //binary heap 25 | 26 | while(start >= 0){ 27 | //sift down the node at index start to the proper place 28 | //such that all nodes below the start index are in heap 29 | //order 30 | siftDown(a, start, count - 1); 31 | start--; 32 | } 33 | //after sifting down the root all nodes/elements are in heap order 34 | } 35 | 36 | public static void siftDown(int[] a, int start, int end){ 37 | //end represents the limit of how far down the heap to sift 38 | int root = start; 39 | 40 | while((root * 2 + 1) <= end){ //While the root has at least one child 41 | int child = root * 2 + 1; //root*2+1 points to the left child 42 | //if the child has a sibling and the child's value is less than its sibling's... 43 | if(child + 1 <= end && a[child] < a[child + 1]) 44 | child = child + 1; //... then point to the right child instead 45 | if(a[root] < a[child]){ //out of max-heap order 46 | int tmp = a[root]; 47 | a[root] = a[child]; 48 | a[child] = tmp; 49 | root = child; //repeat to continue sifting down the child now 50 | }else 51 | return; 52 | } 53 | } 54 | 55 | 56 | -------------------------------------------------------------------------------- /sorting/java/Insertion.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedList; 2 | public class Merge> { 3 | public LinkedList mergeSort(LinkedList m){ 4 | if(m.size() <= 1) return m; 5 | 6 | int middle= m.size() / 2; 7 | LinkedList left= new LinkedList(); 8 | for(int i= 0;i < middle;i++) left.add(m.get(i)); 9 | LinkedList right= new LinkedList(); 10 | for(int i= middle;i < m.size();i++) right.add(m.get(i)); 11 | 12 | right= mergeSort(right); 13 | left= mergeSort(left); 14 | LinkedList result= merge(left, right); 15 | 16 | return result; 17 | } 18 | 19 | public LinkedList merge(LinkedList left, LinkedList right){ 20 | LinkedList result= new LinkedList(); 21 | 22 | while(!left.isEmpty() && !right.isEmpty()){ 23 | //change the direction of this comparison to change the direction of the sort 24 | if(left.peek().compareTo(right.peek()) <= 0) result.add(left.remove()); 25 | else result.add(right.remove()); 26 | } 27 | 28 | result.addAll(left); 29 | result.addAll(right); 30 | return result; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /sorting/java/Merge.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedList; 2 | public class Merge> { 3 | public LinkedList mergeSort(LinkedList m){ 4 | if(m.size() <= 1) return m; 5 | 6 | int middle= m.size() / 2; 7 | LinkedList left= new LinkedList(); 8 | for(int i= 0;i < middle;i++) left.add(m.get(i)); 9 | LinkedList right= new LinkedList(); 10 | for(int i= middle;i < m.size();i++) right.add(m.get(i)); 11 | 12 | right= mergeSort(right); 13 | left= mergeSort(left); 14 | LinkedList result= merge(left, right); 15 | 16 | return result; 17 | } 18 | 19 | public LinkedList merge(LinkedList left, LinkedList right){ 20 | LinkedList result= new LinkedList(); 21 | 22 | while(!left.isEmpty() && !right.isEmpty()){ 23 | //change the direction of this comparison to change the direction of the sort 24 | if(left.peek().compareTo(right.peek()) <= 0) result.add(left.remove()); 25 | else result.add(right.remove()); 26 | } 27 | 28 | result.addAll(left); 29 | result.addAll(right); 30 | return result; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /sorting/java/Quick.java: -------------------------------------------------------------------------------- 1 | public static > List quickSort(List arr) { 2 | if (arr.size() <= 1) 3 | return arr; 4 | E pivot = arr.getFirst(); //This pivot can change to get faster results 5 | 6 | List less = new LinkedList(); 7 | List pivotList = new LinkedList(); 8 | List more = new LinkedList(); 9 | 10 | // Partition 11 | for (E i: arr) { 12 | if (i.compareTo(pivot) < 0) 13 | less.add(i); 14 | else if (i.compareTo(pivot) > 0) 15 | more.add(i); 16 | else 17 | pivotList.add(i); 18 | } 19 | 20 | // Recursively sort sublists 21 | less = quickSort(less); 22 | more = quickSort(more); 23 | 24 | // Concatenate results 25 | less.addAll(pivotList); 26 | less.addAll(more); 27 | return less; 28 | } 29 | -------------------------------------------------------------------------------- /sorting/java/Selection.java: -------------------------------------------------------------------------------- 1 | public static void sort(int[] nums){ 2 | for(int currentPlace = 0;currentPlace 0) { 4 | for (int i = increment; i < a.length; i++) { 5 | int j = i; 6 | int temp = a[i]; 7 | while (j >= increment && a[j - increment] > temp) { 8 | a[j] = a[j - increment]; 9 | j -= increment; 10 | } 11 | a[j] = temp; 12 | } 13 | if (increment == 2) { 14 | increment = 1; 15 | } else { 16 | increment *= (5.0 / 11); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /sorting/js/bubble.js: -------------------------------------------------------------------------------- 1 | Array.prototype.bubblesort = function() { 2 | var done = false; 3 | while (!done) { 4 | done = true; 5 | for (var i = 1; i this[i]) { 7 | done = false; 8 | [this[i-1], this[i]] = [this[i], this[i-1]] 9 | } 10 | } 11 | } 12 | return this; 13 | } 14 | -------------------------------------------------------------------------------- /sorting/perl/bubbble.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | # In line sorting 3 | sub bubble_sort { 4 | for my $i (0 .. $#_){ 5 | for my $j ($i + 1 .. $#_){ 6 | # swap 7 | $_[$j] < $_[$i] and @_[$i, $j] = @_[$j, $i]; 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /sorting/perl/headp.pl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hemanth/algorithms/3312b595d7790424f3137f5081c763618a2bdb23/sorting/perl/headp.pl -------------------------------------------------------------------------------- /sorting/perl/heap.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | 3 | sub heap_sort 4 | { 5 | my($list) = @_; 6 | my $count = scalar @$list; 7 | heapify($count,$list); 8 | 9 | my $end = $count - 1; 10 | while($end > 0) 11 | { 12 | @$list[0,$end] = @$list[$end,0]; 13 | sift_down(0,$end-1,$list); 14 | $end--; 15 | } 16 | } 17 | sub heapify 18 | { 19 | my ($count,$list) = @_; 20 | my $start = ($count - 2) / 2; 21 | while($start >= 0) 22 | { 23 | sift_down($start,$count-1,$list); 24 | $start--; 25 | } 26 | } 27 | sub sift_down 28 | { 29 | my($start,$end,$list) = @_; 30 | 31 | my $root = $start; 32 | while($root * 2 + 1 <= $end) 33 | { 34 | my $child = $root * 2 + 1; 35 | $child++ if($child + 1 <= $end && $list->[$child] < $list->[$child+1]); 36 | if($list->[$root] < $list->[$child]) 37 | { 38 | @$list[$root,$child] = @$list[$child,$root]; 39 | $root = $child; 40 | }else{ return } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /sorting/perl/insertion.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | sub insertion_sort { 3 | $arr = shift; 4 | for ($i = 0; $i <= $#{$arr}; $i++) { 5 | $j = $i - 1; 6 | $key = $arr->[$i]; 7 | while ($arr->[$j] > $key && $j >= 0) { 8 | $arr->[$j + 1] = $arr->[$j]; 9 | $j -= 1; 10 | } 11 | $arr->[$j + 1] = $key; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /sorting/perl/merge.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | sub mergesort 3 | { 4 | my @i = @_; 5 | return @i unless scalar @i > 1; 6 | 7 | my $m = int $#i / 2; 8 | my @left = &mergesort(@i[0..$m]); 9 | my @right = &mergesort(@i[$m+1..$#i]); 10 | 11 | return &merge(\@left,\@right); 12 | } 13 | 14 | sub merge 15 | { 16 | my($left,$right) = @_; 17 | my @r = (); 18 | 19 | push @r, shift @{(@$left[0] < @$right[0]?$left:$right)} while(@$left and @$right); 20 | push @r, shift @$left while(@$left); 21 | push @r, shift @$right while(@$right); 22 | 23 | return @r; 24 | } 25 | -------------------------------------------------------------------------------- /sorting/perl/quick.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | 3 | sub quicksort { 4 | @_ <= 1 ? @_ : do { 5 | my $pivot = pop; 6 | quicksort( grep {$_ <= $pivot} @_ ), 7 | $pivot, 8 | quicksort( grep {$_ > $pivot} @_ ) 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /sorting/perl/selection.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | sub selection_sort 3 | { my @a = @_; 4 | foreach my $i (0 .. $#a - 1) 5 | {my $min = $i + 1; 6 | $a[$_] < $a[$min] and $min = $_ foreach $min .. $#a; 7 | $a[$i] > $a[$min] and @a[$i, $min] = @a[$min, $i];} 8 | return @a; 9 | } 10 | -------------------------------------------------------------------------------- /sorting/perl/shell.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | 3 | sub shell 4 | { 5 | my @tosort = @_; 6 | my $inc = int(($#tosort+1) / 2); 7 | while ($inc > 0) { 8 | foreach my $i (0 .. $#tosort) { 9 | my $temp = $tosort[$i]; 10 | while ($i >= $inc && $tosort[$i-$inc] > $temp) { 11 | $tosort[$i] = $iary[$i-$inc]; 12 | $i -= $inc; 13 | } 14 | $tosort[$i] = $temp; 15 | } 16 | if ($inc == 2) { 17 | $inc = 1; 18 | } else { 19 | $inc *= (5.0 / 11); 20 | $inc = int($inc); 21 | } 22 | } 23 | return @tosort; 24 | } 25 | -------------------------------------------------------------------------------- /sorting/python/bubble.py: -------------------------------------------------------------------------------- 1 | def bubble_sort(seq): 2 | 3 | changed = True 4 | while changed: 5 | changed = False 6 | for i in xrange(len(seq) - 1): 7 | if seq[i] > seq[i+1]: 8 | seq[i], seq[i+1] = seq[i+1], seq[i] 9 | changed = True 10 | return None 11 | -------------------------------------------------------------------------------- /sorting/python/heap.py: -------------------------------------------------------------------------------- 1 | def heapsort(lst): 2 | 3 | for start in range((len(lst)-2)/2, -1, -1): 4 | siftdown(lst, start, len(lst)-1) 5 | 6 | for end in range(len(lst)-1, 0, -1): 7 | lst[end], lst[0] = lst[0], lst[end] 8 | siftdown(lst, 0, end - 1) 9 | return lst 10 | 11 | def siftdown(lst, start, end): 12 | root = start 13 | while True: 14 | child = root * 2 + 1 15 | if child > end: break 16 | if child + 1 <= end and lst[child] < lst[child + 1]: 17 | child += 1 18 | if lst[root] < lst[child]: 19 | lst[root], lst[child] = lst[child], lst[root] 20 | root = child 21 | else: 22 | break 23 | -------------------------------------------------------------------------------- /sorting/python/insertion.py: -------------------------------------------------------------------------------- 1 | def insertion_sort(l): 2 | for i in xrange(1, len(l)): 3 | j = i-1 4 | key = l[i] 5 | while (l[j] > key) and (j >= 0): 6 | l[j+1] = l[j] 7 | j -= 1 8 | l[j+1] = key 9 | -------------------------------------------------------------------------------- /sorting/python/merge.py: -------------------------------------------------------------------------------- 1 | from heapq import merge 2 | 3 | def merge_sort(m): 4 | if len(m) <= 1: 5 | return m 6 | 7 | middle = len(m) / 2 8 | left = m[:middle] 9 | right = m[middle:] 10 | 11 | left = merge_sort(left) 12 | right = merge_sort(right) 13 | return list(merge(left, right)) 14 | 15 | def merge(left, right): 16 | result = [] 17 | 18 | while left and right: 19 | if left[0] <= right[0]: 20 | result.append(left.pop(0)) 21 | else: 22 | result.append(right.pop(0)) 23 | 24 | if left: 25 | result.extend(left) 26 | if right: 27 | result.extend(right) 28 | return result 29 | -------------------------------------------------------------------------------- /sorting/python/qucik.py: -------------------------------------------------------------------------------- 1 | def qsort(list): 2 | if not list: 3 | return [] 4 | else: 5 | pivot = list[0] 6 | less = [x for x in list if x < pivot] 7 | more = [x for x in list[1:] if x >= pivot] 8 | return qsort(less) + [pivot] + qsort(more) 9 | -------------------------------------------------------------------------------- /sorting/python/selection.py: -------------------------------------------------------------------------------- 1 | def selectionSort(lst): 2 | for i in range(0,len(lst)-1): 3 | mn = min(range(i,len(lst)), key=lst.__getitem__) 4 | lst[i],lst[mn] = lst[mn],lst[i] 5 | return lst 6 | -------------------------------------------------------------------------------- /sorting/python/shell.py: -------------------------------------------------------------------------------- 1 | def shell(seq): 2 | inc = len(seq) // 2 3 | while inc: 4 | for i, el in enumerate(seq): 5 | while i >= inc and seq[i - inc] > el: 6 | seq[i] = seq[i - inc] 7 | i -= inc 8 | seq[i] = el 9 | inc = 1 if inc == 2 else int(inc * 5.0 / 11) 10 | -------------------------------------------------------------------------------- /sorting/ruby/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hemanth/algorithms/3312b595d7790424f3137f5081c763618a2bdb23/sorting/ruby/.gitignore -------------------------------------------------------------------------------- /sorting/ruby/bubble.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/ruby 2 | # Works on a array 3 | # tosort.bubblesort! 4 | class Array 5 | def bubblesort! 6 | length.times do |j| 7 | for i in 1...(length - j) 8 | if self[i] < self[i - 1] 9 | self[i], self[i - 1] = self[i - 1], self[i] 10 | end 11 | end 12 | end 13 | end 14 | end 15 | 16 | -------------------------------------------------------------------------------- /sorting/ruby/heap.rb: -------------------------------------------------------------------------------- 1 | #!/bin/usr/ruby 2 | # Works on an Array 3 | class Array 4 | def heapsort! 5 | # Inline heapify 6 | ((length - 2) / 2).downto(0) {|start| siftdown(start, length - 1)} 7 | 8 | (length - 1).downto(1) do |tillend| 9 | self[end_], self[0] = self[0], self[end_] 10 | siftdown(0, end_ - 1) 11 | end 12 | self 13 | end 14 | 15 | def siftdown(start, tillend) 16 | root = start 17 | loop do 18 | child = root * 2 + 1 19 | break if child > tillend 20 | if child + 1 <= tillend and self[child] < self[child + 1] 21 | child += 1 22 | end 23 | if self[root] < self[child] 24 | self[root], self[child] = self[child], self[root] 25 | root = child 26 | else 27 | break 28 | end 29 | end 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /sorting/ruby/insertion.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/ruby 2 | class Array 3 | def insertionsort! 4 | 1.upto(length - 1) do |i| 5 | value = self[i] 6 | j = i - 1 7 | while j >= 0 and self[j] > value 8 | self[j+1] = self[j] 9 | j -= 1 10 | end 11 | self[j+1] = value 12 | end 13 | self 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /sorting/ruby/merge.rb: -------------------------------------------------------------------------------- 1 | #!/bin/usr/ruby 2 | # Works with Array 3 | # ary.mergesort {|a, b| b <=> a} 4 | 5 | class Array 6 | def mergesort(&comparitor) 7 | if length <= 1 8 | self 9 | else 10 | unless comparitor 11 | comparitor = lambda {|a, b| a <=> b} 12 | end 13 | middle = length / 2 14 | left = self[0, middle].mergesort(&comparitor) 15 | right = self[middle..-1].mergesort(&comparitor) 16 | merge(left, right, comparitor) 17 | end 18 | end 19 | 20 | protected 21 | def merge(left, right, comparitor) 22 | if left.empty? 23 | right 24 | elsif right.empty? 25 | left 26 | elsif comparitor.call(left.first, right.first) <= 0 27 | [left.first] + merge(left[1..-1], right, comparitor) 28 | else 29 | [right.first] + merge(left, right[1..-1], comparitor) 30 | end 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /sorting/ruby/quick.rb: -------------------------------------------------------------------------------- 1 | #/usr/bin/ruby 2 | # Works with array 3 | class Array 4 | def quick_sort 5 | return self if length <= 1 6 | pivot = self[length / 2] 7 | find_all { |i| i < pivot }.quick_sort + 8 | find_all { |i| i == pivot } + 9 | find_all { |i| i > pivot }.quick_sort 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /sorting/ruby/selection.rb: -------------------------------------------------------------------------------- 1 | #!/usr.bin/ruby 2 | 3 | class Array 4 | def selectionsort! 5 | 0.upto(length - 2) do |i| 6 | (mindx = i + 1).upto(length - 1) do |j| 7 | if self[j] < self[mindx] 8 | mindx = j 9 | end 10 | end 11 | if self[i] > self[mindx] 12 | self[i], self[mindx] = self[mindx], self[i] 13 | end 14 | end 15 | self 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /sorting/ruby/shell.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | 3 | class Array 4 | def shellsort! 5 | inc = length / 2 6 | while inc != 0 7 | each_with_index do |el, i| 8 | while i >= inc and self[i - inc] > el 9 | self[i] = self[i - inc] 10 | i -= inc 11 | end 12 | self[i] = el 13 | end 14 | inc = (inc == 2 ? 1 : (inc * 5.0 / 11).to_i) 15 | end 16 | end 17 | end 18 | --------------------------------------------------------------------------------