quicksort_median_of_three 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +59 -0
- data/Rakefile +4 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/quicksort_median_of_three/version.rb +3 -0
- data/lib/quicksort_median_of_three.rb +69 -0
- data/quicksort_median_of_three.gemspec +32 -0
- data/rspec/spec_helper.rb +1 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: da464c8df3bece7ec170912dfba1eed64011b2ee
|
4
|
+
data.tar.gz: 29bfc7cf298fbc1a048e0afae6f7a3f6442997e7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9345ce6d01033ad5813e6f1d6e38b3c85abe5c5e8419c557d9c18c115c34be6dcda064ef981b8df731d153624b336a993994507016022fda6b4c911dd901b2cc
|
7
|
+
data.tar.gz: 406cdb58c407b70febc6f6c2c70312004b6604a261823a9b7cb596cf8d58b56e8a23136438ef14ffbd193b50ad29de108295d16230e141d701d352b856638b8e
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Iuliia Kotlenko
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# QuicksortMedianOfThree
|
2
|
+
|
3
|
+
## Description
|
4
|
+
|
5
|
+
Quicksort is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order.
|
6
|
+
Quicksort is a divide and conquer algorithm. Quicksort first divides a large array into two smaller sub-arrays: the low elements and the high elements. Quicksort can then recursively sort the sub-arrays.
|
7
|
+
|
8
|
+
The steps are:
|
9
|
+
|
10
|
+
1.Pick an element, called a pivot, from the array.
|
11
|
+
2.Reorder the array so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation.
|
12
|
+
3.Recursively apply the above steps to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values.
|
13
|
+
|
14
|
+
There are some ways how you can choose a pivot element: the first element, the last element or random element.
|
15
|
+
|
16
|
+
Bur Sedgewick suggested some optimizations:
|
17
|
+
|
18
|
+
1. Pivot element is median-of-three.
|
19
|
+
2.To make sure at most O(log n) space is used, recurse first into the smaller side of the partition, then use a tail call to recurse into the other.
|
20
|
+
3.Use insertion sort, which has a smaller constant factor and is thus faster on small arrays, for invocations on small arrays (i.e. where the length is less than a threshold k determined experimentally). This can be implemented by simply stopping the recursion when less than k elements are left, leaving the entire array k-sorted: each element will be at most k positions away from its final position. Then, a single insertion sort pass[13]:117 finishes the sort in O(kn) time. A separate insertion sort of each small segment as they are identified adds the overhead of starting and stopping many small sorts, but avoids wasting effort comparing keys across the many segment boundaries, where keys will be in order due to the workings of the quicksort process.
|
21
|
+
|
22
|
+
|
23
|
+
## Installation
|
24
|
+
|
25
|
+
Add this line to your application's Gemfile:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
gem 'quicksort_median_of_three'
|
29
|
+
```
|
30
|
+
|
31
|
+
And then execute:
|
32
|
+
|
33
|
+
$ bundle
|
34
|
+
|
35
|
+
Or install it yourself as:
|
36
|
+
|
37
|
+
$ gem install quicksort_median_of_three
|
38
|
+
|
39
|
+
## Usage
|
40
|
+
|
41
|
+
```
|
42
|
+
a = [9,34,8,0,1,23,56,87,45]
|
43
|
+
Sort.quicksort(a, 0, a.length_1)
|
44
|
+
method quicksort takes 3 arguments: array, index on the first element, index on the last element
|
45
|
+
```
|
46
|
+
|
47
|
+
## Development
|
48
|
+
|
49
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
50
|
+
|
51
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
52
|
+
|
53
|
+
## Contributing
|
54
|
+
|
55
|
+
1. Fork it ( https://github.com/[my-github-username]/quicksort_median_of_three/fork )
|
56
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
57
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
58
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
59
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "quicksort_median_of_three"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
require "quicksort_median_of_three/version"
|
2
|
+
|
3
|
+
class Sort
|
4
|
+
# Your code goes here...
|
5
|
+
|
6
|
+
def self.quicksort(ar, lo, hi)
|
7
|
+
|
8
|
+
if ((hi - lo) < 20)
|
9
|
+
insertionSort(ar, lo, hi)
|
10
|
+
else
|
11
|
+
while (lo < hi)
|
12
|
+
p = partition(ar, lo, hi)
|
13
|
+
quicksort(ar, lo, p - 1)
|
14
|
+
lo = p+1
|
15
|
+
#quicksort(ar, p + 1, hi)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
return ar
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.partition(ar, lo, hi)
|
23
|
+
|
24
|
+
index = choose_pivot(ar, lo, hi)
|
25
|
+
pivot = ar[index]
|
26
|
+
ar[index], ar[hi] = ar[hi], ar[index]
|
27
|
+
i = lo - 1
|
28
|
+
(lo...hi).each do |j|
|
29
|
+
if (ar[j] <= pivot)
|
30
|
+
i = i + 1
|
31
|
+
ar[i],ar[j] = ar[j],ar[i]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
i = i + 1
|
35
|
+
ar[i], ar[hi] = ar[hi], ar[i]
|
36
|
+
|
37
|
+
return i
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.choose_pivot(ar, lo, hi)
|
41
|
+
|
42
|
+
left = lo
|
43
|
+
right = hi
|
44
|
+
center = (left + right) / 2
|
45
|
+
if ((ar[left] - ar[right]) * (ar[center] - ar[left]) >= 0)
|
46
|
+
return left
|
47
|
+
elsif ((ar[right] - ar[left]) * (ar[center] - ar[right]) >= 0)
|
48
|
+
return right
|
49
|
+
else
|
50
|
+
return center
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.insertionSort(ar, lo, hi)
|
55
|
+
|
56
|
+
(lo + 1..hi).each do |i|
|
57
|
+
value = ar[i]
|
58
|
+
j = i - 1
|
59
|
+
while (j >= 0 && ar[j] > value)
|
60
|
+
ar[j+1] = ar[j]
|
61
|
+
j -= 1
|
62
|
+
end
|
63
|
+
ar[j+1] = value
|
64
|
+
end
|
65
|
+
|
66
|
+
return ar
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'quicksort_median_of_three/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "quicksort_median_of_three"
|
8
|
+
spec.version = QuicksortMedianOfThree::VERSION
|
9
|
+
spec.authors = ["Iuliia Kotlenko"]
|
10
|
+
spec.email = ["kotlenko.julia@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Quicksort algorithm}
|
13
|
+
spec.description = %q{Quicksort algorithm with optimisations.}
|
14
|
+
spec.homepage = "https://github.com/IuliiaKot/quicksort_median_of_three"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
17
|
+
# delete this section to allow pushing this gem to any host.
|
18
|
+
#if spec.respond_to?(:metadata)
|
19
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
20
|
+
#else
|
21
|
+
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
22
|
+
#end
|
23
|
+
|
24
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
30
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
31
|
+
spec.add_development_dependency "rspec"
|
32
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "quicksort_median_of_three"
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: quicksort_median_of_three
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Iuliia Kotlenko
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Quicksort algorithm with optimisations.
|
56
|
+
email:
|
57
|
+
- kotlenko.julia@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
70
|
+
- lib/quicksort_median_of_three.rb
|
71
|
+
- lib/quicksort_median_of_three/version.rb
|
72
|
+
- quicksort_median_of_three.gemspec
|
73
|
+
- rspec/spec_helper.rb
|
74
|
+
homepage: https://github.com/IuliiaKot/quicksort_median_of_three
|
75
|
+
licenses: []
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.4.8
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Quicksort algorithm
|
97
|
+
test_files: []
|