sortech 0.1.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/sortech.rb +203 -12
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b8a69ef2c9c0bc4a5ba3fc0c1e6370bf5117b0a7
4
- data.tar.gz: 016362b6decef9da9729e6d5fa6ecbb596b09c4a
3
+ metadata.gz: 84f353b9ca32d5c35de10c3a62ed8fc78f1e59dc
4
+ data.tar.gz: 7fb34164abd1a2b3ea61ef83cc79a21a48ffeaf4
5
5
  SHA512:
6
- metadata.gz: 52107a459920ed628254a2d894a5bfb92a04cc31de3ee24f5b887007816ba11af2a581c23977e943a2ef1cd18d933463335b478572d5e5382617e5379c347f74
7
- data.tar.gz: 31f2fe51f89eeda809229dfe734edfb3ecba5c8b589823e733c92db66c07bc1ccbd148bfdd431fa6c2d21889b910e5d63644f972959295fc680f67768c445283
6
+ metadata.gz: 884c0c43fffa371dfaa897cec669e99f008c0c135619c38a0178cd13c055b176c0020544ed3865c3dac8f1bbd63bc66a7470cbb39c8ad2aac569a31003b87c1e
7
+ data.tar.gz: 223440b4e3099053449e9860e63f3cb20fb3def3213b0c7cd8293600d45a17374fc80fb2f0b30b88942ad48f4ce402a134126ad173cf9ac849e09a2f809858d1
@@ -1,6 +1,16 @@
1
1
  module Sortech
2
2
  class Sort
3
3
  class << self
4
+ # Sort an array using Bubble sort technique
5
+ # Advantages:
6
+ # 1. Straightforward, simple and slow.
7
+ # 2. Stable.
8
+ # 3. Inefficient on large tables.
9
+ #
10
+ # @param [Array<Integer>]
11
+ #
12
+ # @return Array
13
+ #
4
14
  def bubble(arr)
5
15
  arr if arr.length == 0 || is_sorted?(arr)
6
16
  n = arr.length
@@ -8,9 +18,7 @@ module Sortech
8
18
  flag = false
9
19
  for i in 0..n - 2
10
20
  if arr[i] > arr[i+1]
11
- arr[i] += arr[i+1]
12
- arr[i+1] = arr[i] - arr[i+1]
13
- arr[i] = arr[i] - arr[i+1]
21
+ arr[i], arr[i+1] = arr[i+1], arr[i]
14
22
  flag = true
15
23
  end
16
24
  end
@@ -20,15 +28,198 @@ module Sortech
20
28
  arr
21
29
  end
22
30
 
23
- private
24
- def is_sorted? arr
25
- for i in 0..arr.length - 2
26
- if arr[i] > arr[i+1]
27
- return false
31
+ # Sort an array using Selection sort technique
32
+ # Advantages:
33
+ # 1. Improves the performance of bubble sort and also slow.
34
+ # 2. Unstable but can be implemented as a stable sort.
35
+ # 3. Quite slow for large amount of data.
36
+ #
37
+ # @param [Array<Integer>]
38
+ #
39
+ # @return Array
40
+ #
41
+ def selection(arr)
42
+ arr if arr.length == 0 || is_sorted?(arr)
43
+ n = arr.length
44
+ for i in 0..n - 1
45
+ s = arr[i]
46
+ p = i
47
+ for j in i+1..n - 1
48
+ if s > arr[j]
49
+ s = arr[j]
50
+ p = j
28
51
  end
29
52
  end
30
- true
53
+ arr[i], arr[p] = arr[p], arr[i]
54
+ end
55
+ arr
56
+ end
57
+
58
+ # Sort an array using Insertion sort technique
59
+ #
60
+ # Advantages:
61
+ # 1. Efficient for small list and mostly sorted list.
62
+ # 2. Sort big array slowly.
63
+ # 3. Save memory
64
+ #
65
+ # @param [Array<Integer>]
66
+ #
67
+ # @return Array
68
+ #
69
+ def insertion(arr)
70
+ for i in 1..arr.length - 1
71
+ t = arr[i]
72
+ j = i - 1
73
+ while j >= 0 && arr[j] > t
74
+ arr[j+1] = arr[j]
75
+ j -= 1
76
+ end
77
+ arr[j+1] = t
78
+ end
79
+ arr
80
+ end
81
+
82
+ # Sort an array using Quick sort technique
83
+ # Advantages:
84
+ # 1. Fastest sorting algorithm in practice.
85
+ # 2. Available in many standard libraries.
86
+ # 3. O (log n) space usage.
87
+ # 4. Unstable sort and complex for choosing a good pivot element. # @param [Array<Integer>]
88
+ #
89
+ # @param [Array<Integer>]
90
+ #
91
+ # @return Array
92
+ #
93
+ def quicksort(arr, low=0, high=arr.length-1)
94
+
95
+ if low < high
96
+ pi = partition(arr, low, high)
97
+ quicksort(arr, low, pi-1) # called before pi
98
+ quicksort(arr, pi+1, high) # called after pi
99
+ end
100
+ arr
101
+ end
102
+
103
+ # Sort an array using Merge sort technique
104
+ # Advantages:
105
+ # 1. Well for very large list, stable sort.
106
+ # 2. A fast recursive sorting.
107
+ # 3. Both useful for internal and external sorting.
108
+ # 4. It requires an auxiliary array that is as large as the original array to be sorted.
109
+ #
110
+ # @param [Array<Integer>]
111
+ #
112
+ # @return Array
113
+ #
114
+ def mergesort(arr, left=0,right=arr.length-1)
115
+
116
+ if left < right
117
+ middle = (left + (right - 1))/2
118
+
119
+ mergesort(arr,left,middle) # sort first half
120
+ mergesort(arr, middle+1,right) # sort second half
121
+ merge(arr, left, middle, right) # merge the two arrays
31
122
  end
32
- end
33
- end
34
- end
123
+ arr
124
+ end
125
+
126
+ # Sort an array using Radix sort technique
127
+ # Advantages:
128
+ # 1. Stable, fast.
129
+ # 2. Used in special cases when the key can be
130
+ #
131
+ # @param [Array<Integer>]
132
+ #
133
+ # @return Array
134
+ # TODO: Implement
135
+ def radix(arr)
136
+ end
137
+
138
+ private
139
+ # Returns true or false depending on whether array is sorted or not
140
+ #
141
+ # @param [Array<Integer>]
142
+ #
143
+ # @return Boolean
144
+ #
145
+ def is_sorted? arr
146
+ return false if arr.nil?
147
+ for i in 0..(arr.length - 2)
148
+ if arr[i] > arr[i+1]
149
+ return false
150
+ end
151
+ end
152
+ true
153
+ end
154
+
155
+ def partition arr, low, high
156
+ pivot = arr[high]
157
+ i = low - 1
158
+ for j in low..high-1
159
+ if arr[j] <= pivot
160
+ i += 1
161
+ arr[i], arr[j] = arr[j], arr[i]
162
+ end
163
+ end
164
+ arr[i+1], arr[high] = arr[high], arr[i+1]
165
+ i+1
166
+ end # partition
167
+
168
+ # Splits the array into two halves and merge them in sorted order
169
+ # back to the original array
170
+ #
171
+ # @param [Array<Integer>, Integer, Integer, Integer]
172
+ #
173
+ # @return Boolean
174
+ #
175
+
176
+ def merge arr, left, middle, right
177
+ n1 = middle - left + 1
178
+ n2 = right - middle
179
+
180
+ # Create empty arrays of size n1 and n2
181
+ left_array = Array.new(n1){0}
182
+ right_array = Array.new(n1){0}
183
+
184
+ # copy data to empty arrays
185
+ for i in 0..n1
186
+ left_array[i] = arr[left+i]
187
+ end
188
+ for i in 0..n2
189
+ right_array[i] = arr[middle + i + 1]
190
+ end
191
+
192
+ # Merge two arrays back into arr
193
+ i = 0
194
+ j = 0
195
+ k = left
196
+
197
+ while i < n1 && j < n2
198
+ if left_array[i] <= right_array[j]
199
+ arr[k] = left_array[i]
200
+ i += 1
201
+ else
202
+ arr[k] = right_array[j]
203
+ j += 1
204
+ end
205
+ k += 1
206
+ end
207
+
208
+ # copy the remaining elements of left_array, if any
209
+ while i < n1
210
+ arr[k] = left_array[i]
211
+ i += 1
212
+ k += 1
213
+ end
214
+
215
+ # copy the remaining elements of right_array, if any
216
+ while j < n2
217
+ arr[k] = right_array[j]
218
+ j += 1
219
+ k += 1
220
+ end
221
+ end # merge
222
+
223
+ end # Class self
224
+ end # Sort
225
+ end # Sortech
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sortech
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mansoor Khan