Skip to content

heap sorting added #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python-lib-master/allalgorithms/searches/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .binary_search import *
20 changes: 20 additions & 0 deletions python-lib-master/allalgorithms/searches/binary_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: UTF-8 -*-
#
# Binary search works for a sorted array.
# The All ▲lgorithms library for python
#
# Contributed by: Carlos Abraham Hernandez
# Github: @abranhe
#
def binary_search(arr, query):
lo, hi = 0, len(arr) - 1
while lo <= hi:
mid = (hi + lo) // 2
val = arr[mid]
if val == query:
return mid
elif val < query:
lo = mid + 1
else:
hi = mid - 1
return None
1 change: 1 addition & 0 deletions python-lib-master/allalgorithms/sorting/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .merge_sort import *
40 changes: 40 additions & 0 deletions python-lib-master/allalgorithms/sorting/heap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
def heapify(arr, n, i):
largest = i
l = 2 * i + 1
r = 2 * i + 2


if l < n and arr[i] < arr[l]:
largest = l


if r < n and arr[largest] < arr[r]:
largest = r


if largest != i:
arr[i],arr[largest] = arr[largest],arr[i] # swap


heapify(arr, n, largest)


def heapSort(arr):
n = len(arr)


for i in range(n, -1, -1):
heapify(arr, n, i)


for i in range(n-1, 0, -1):
arr[i], arr[0] = arr[0], arr[i] # swap
heapify(arr, i, 0)


arr = [ 12, 11, 13, 5, 6, 7]
heapSort(arr)
n = len(arr)
print ("Sorted array is")
for i in range(n):
print ("%d" %arr[i]),
40 changes: 40 additions & 0 deletions python-lib-master/allalgorithms/sorting/heapsort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
def heapify(arr, n, i):
largest = i
l = 2 * i + 1
r = 2 * i + 2


if l < n and arr[i] < arr[l]:
largest = l


if r < n and arr[largest] < arr[r]:
largest = r


if largest != i:
arr[i],arr[largest] = arr[largest],arr[i] # swap


heapify(arr, n, largest)


def heapSort(arr):
n = len(arr)


for i in range(n, -1, -1):
heapify(arr, n, i)


for i in range(n-1, 0, -1):
arr[i], arr[0] = arr[0], arr[i] # swap
heapify(arr, i, 0)


arr = [ 12, 11, 13, 5, 6, 7]
heapSort(arr)
n = len(arr)
print ("Sorted array is")
for i in range(n):
print ("%d" %arr[i]),
26 changes: 26 additions & 0 deletions python-lib-master/allalgorithms/sorting/merge_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: UTF-8 -*-
#
# Merge Sort Algorithm
# The All ▲lgorithms library for python
#
# Contributed by: Carlos Abraham Hernandez
# Github: @abranhe
#
def merge_sort(arr):
if len(arr) == 1:
return arr

left = merge_sort(arr[:(len(arr)//2)])
right = merge_sort(arr[(len(arr)//2):])
out = []

while bool(left) or bool(right):
if bool(left) ^ bool(right):
alias = left if left else right
else:
alias = left if left[0] < right[0] else right

out.append(alias[0])
alias.remove(alias[0])

return out
14 changes: 14 additions & 0 deletions python-lib-master/changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div align="left">
<img src="https://cdn.abraham.gq/projects/algorithms/algorithms.svg" alt="Algorithms"> <img src="http://konpa.github.io/devicon/devicon.git/icons/python/python-original.svg" width="50px">
<div>

# Version `0.0.0`

Date: October 9, 2018

### Algorithms:

- Sorting
- Bubble Sort
- Searches
- Merge Sort
1 change: 1 addition & 0 deletions python-lib-master/docs/CNAME
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python.allalgorithms.com
92 changes: 92 additions & 0 deletions python-lib-master/docs/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<div align="center">
<a href="https://pypi.org/project/allalgorithms"><img src="https://cdn.abranhe.com/projects/algorithms/logo.svg" width="30%">
</a>
<br>
<br>
<br>
<br>
The All ▲lgorithms Python library
<br>
<br>
</div>

<p align="center">
<a href="https://travis-ci.org/abranhe/python-lib"><img src="https://img.shields.io/travis/abranhe/python-lib.svg?logo=travis" /></a>
<a href="https://github.com/abranhe/python-lib/blob/master/license"><img src="https://img.shields.io/github/license/abranhe/python-lib.svg" /></a>
<a href="https://github.com/allalgorithms"><img src="https://cdn.abranhe.com/projects/algorithms/badge.svg"/></a>
<a href="https://pypi.org/project/allalgorithms"><img src="https://img.shields.io/pypi/v/allalgorithms.svg"/></a>
<a href='https://coveralls.io/github/abranhe/python-lib?branch=master'><img src='https://coveralls.io/repos/github/abranhe/python-lib/badge.svg?branch=master' alt='Coverage Status' /></a>
</p>

<p align="center">
<br>
<br>
<a href="https://python.allalgorithms.com"><code>python.allalgorithms.com</code></a>
</p>

# Why?

- Why not 😂
- Clean and focused
- Actively maintained
- Because All Algorithms should easy to use in Python

Read the detailed documentation at [python.allalgorithms.com](https://python.allalgorithms.com) or see [Tree](#tree).

## Install

```
pip install allalgorithms
```

## Usage Example

```py
from allalgorithms.searches import binary_search

arr = [-2, 1, 2, 7, 10, 77]

print(binary_search(arr, 7))
# -> 3

print(binary_search(arr, 3))
# -> None
```

# Tree

- Searches
- [Binary Search](searches/binary-search)
- Sorting
- [Merge Sort](sorting/merge-sort)


# Related

- [javascript-lib](https://github.com/abranhe/javascript-lib): All ▲lgorithms Javascript library

# Maintainers

|[![Carlos Abraham Logo][3]][4]|
| :--------------------------: |
| [Carlos Abraham][4] |


# License

[MIT][5] License © [Carlos Abraham][4]

<!-------------------Markdown Images Links ---------------------------------->
[1]: https://cdn.abranhe.com/projects/algorithms/badge.svg
[2]: https://github.com/abranhe/python-lib
[3]: https://avatars3.githubusercontent.com/u/21347264?s=50
[4]: https://github.com/abranhe
[5]: https://github.com/abranhe/python-lib/blob/master/LICENSE
<!-------------------Markdown Images Links ---------------------------------->

<div align="center">
<a href="https://github.com/abranhe/algorithms">
<img src="https://cdn.abranhe.com/projects/algorithms/logo.svg" width="50px">
</a>
<br>
</div>
34 changes: 34 additions & 0 deletions python-lib-master/docs/searches/binary-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Binary Search

In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array

## Install

```
pip install allalgorithms
```

## Usage

```py
from allalgorithms.searches import binary_search

arr = [-2, 1, 2, 7, 10, 77]

print(binary_search(arr, 7))
# -> 3

print(binary_search(arr, 3))
# -> None
```

## API

### binary_search(array, query)

> Return array index if its found, otherwise returns `None`

##### Params:

- `array`: Sorted Array
- `query`: Element to search for in the array
30 changes: 30 additions & 0 deletions python-lib-master/docs/sorting/merge-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Merge Sort

In computer science, merge sort is an efficient, general-purpose, comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output

## Install

```
pip install allalgorithms
```

## Usage

```py
from allalgorithms.sorting import merge_sort

arr = [77, 2, 10, -2, 1, 7]

print(merge_sort(arr))
# -> [-2, 1, 2, 7, 10, 77]
```

## API

### merge_sort(array)

> Returns a sorted array

##### Params:

- `array`: Unsorted Array
21 changes: 21 additions & 0 deletions python-lib-master/license
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Abraham Hernandez <abraham@abranhe.com> (abranhe.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading