Merge pull request #7 from santhon/improvements

Improvements
This commit is contained in:
Chetan Kaushik 2016-07-30 08:36:33 +05:30 committed by GitHub
commit 93443cb519
6 changed files with 272 additions and 113 deletions

View File

@ -1,26 +1,39 @@
import sys
array=[]; def simple_bubble_sort(int_list):
count = len(int_list)
# input swapped = True
print ("Enter any 6 Numbers for Unsorted Array : "); while (swapped):
for i in range(0, 6): swapped = False
n=input(); for j in range(count - 1):
array.append(int(n)); if (int_list[j] > int_list[j + 1]):
int_list[j], int_list[j + 1] = int_list[j + 1], int_list[j]
# Sorting swapped = True
print("") return int_list
for i in range(0, 6):
for j in range(0,5):
if (array[j]>array[j+1]):
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
# Output
for i in range(0,6):
print(array[i]);
def main():
# Python 2's `raw_input` has been renamed to `input` in Python 3
if sys.version_info.major < 3:
input_function = raw_input
else:
input_function = input
try:
print("Enter numbers separated by spaces:")
s = input_function()
inputs = list(map(int, s.split(' ')))
if len(inputs) < 2:
print('No Enough values to sort!')
raise Exception
except Exception as e:
print(e)
else:
sorted_input = simple_bubble_sort(inputs)
print('\nSorted list (min to max): {}'.format(sorted_input))
if __name__ == '__main__':
print('==== Bubble Sort ====\n')
main()

View File

@ -1,25 +1,40 @@
array=[]; import sys
# input
print ("Enter any 6 Numbers for Unsorted Array : ");
for i in range(0, 6):
n=input();
array.append(int(n));
# Sorting
print("")
for i in range(1, 6):
temp=array[i]
j=i-1;
while(j>=0 and temp<array[j]):
array[j+1]=array[j];
j-=1;
array[j+1]=temp;
# Output
for i in range(0,6):
print(array[i]);
def simple_insertion_sort(int_list):
count = len(int_list)
for i in range(1, count):
temp = int_list[i]
j = i - 1
while(j >= 0 and temp < int_list[j]):
int_list[j + 1] = int_list[j]
j -= 1
int_list[j + 1] = temp
return int_list
def main():
# Python 2's `raw_input` has been renamed to `input` in Python 3
if sys.version_info.major < 3:
input_function = raw_input
else:
input_function = input
try:
print("Enter numbers separated by spaces:")
s = input_function()
inputs = list(map(int, s.split(' ')))
if len(inputs) < 2:
print('No Enough values to sort!')
raise Exception
except Exception as e:
print(e)
else:
sorted_input = simple_insertion_sort(inputs)
print('\nSorted list (min to max): {}'.format(sorted_input))
if __name__ == '__main__':
print('==== Insertion Sort ====\n')
main()

View File

@ -1,21 +1,32 @@
def sequentialSearch(alist, item): import sys
pos = 0
found = False
while pos < len(alist) and not found:
if alist[pos] == item: def sequential_search(alist, target):
found = True for index, item in enumerate(alist):
print("Found") if item == target:
print("Found target {} at index {}".format(target, index))
break
else: else:
pos = pos+1
if found == False:
print("Not found") print("Not found")
return found
print("Enter numbers seprated by space")
s = input()
numbers = list(map(int, s.split()))
trgt =int( input('enter a single number to be found in the list '))
sequentialSearch(numbers, trgt)
def main():
# Python 2's `raw_input` has been renamed to `input` in Python 3
if sys.version_info.major < 3:
input_function = raw_input
else:
input_function = input
try:
print("Enter numbers separated by spaces")
s = input_function()
inputs = list(map(int, s.split(' ')))
target = int(input_function('\nEnter a number to be found in list: '))
except Exception as e:
print(e)
else:
sequential_search(inputs, target)
if __name__ == '__main__':
print('==== Linear Search ====\n')
main()

View File

@ -1,36 +1,59 @@
def mergeSort(alist): import sys
print("Splitting ",alist)
if len(alist)>1:
mid = len(alist)//2 def merge_sort(alist):
lefthalf = alist[:mid] print("Splitting ", alist)
righthalf = alist[mid:] if len(alist) > 1:
mergeSort(lefthalf) mid = len(alist) // 2
mergeSort(righthalf) left_half = alist[:mid]
i=0 right_half = alist[mid:]
j=0 merge_sort(left_half)
k=0 merge_sort(right_half)
while i < len(lefthalf) and j < len(righthalf): i = j = k = 0
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i] while i < len(left_half) and j < len(right_half):
i=i+1 if left_half[i] < right_half[j]:
alist[k] = left_half[i]
i += 1
else: else:
alist[k]=righthalf[j] alist[k] = right_half[j]
j=j+1 j += 1
k=k+1 k += 1
while i < len(lefthalf): while i < len(left_half):
alist[k]=lefthalf[i] alist[k] = left_half[i]
i=i+1 i += 1
k=k+1 k += 1
while j < len(righthalf): while j < len(right_half):
alist[k]=righthalf[j] alist[k] = right_half[j]
j=j+1 j += 1
k=k+1 k += 1
print("Merging ",alist) print("Merging ", alist)
return alist
print("Enter numbers seprated by space")
s = input()
numbers = list(map(int, s.split()))
mergeSort(numbers)
def main():
# Python 2's `raw_input` has been renamed to `input` in Python 3
if sys.version_info.major < 3:
input_function = raw_input
else:
input_function = input
try:
print("Enter numbers separated by spaces:")
s = input_function()
inputs = list(map(int, s.split(' ')))
if len(inputs) < 2:
print('No Enough values to sort!')
raise Exception
except Exception as e:
print(e)
else:
sorted_input = merge_sort(inputs)
print('\nSorted list (min to max): {}'.format(sorted_input))
if __name__ == '__main__':
print('==== Merge Sort ====\n')
main()

View File

@ -1,31 +1,41 @@
import sys
def quicksort(A, p, r):
def quick_sort(A, p, r):
if p < r: if p < r:
q = partition(A, p, r) q = partition(A, p, r)
quicksort(A, p, q - 1) quick_sort(A, p, q - 1)
quicksort(A, q + 1, r) quick_sort(A, q + 1, r)
return A
def partition(A, p, r): def partition(A, p, r):
x = A[r]
i = p - 1 i = p - 1
for j in range(p, r): for j in range(p, r):
if A[j] <= x: if A[j] <= A[r]:
i += 1 i += 1
tmp = A[i] A[i], A[j] = A[j], A[i]
A[i] = A[j] A[i + 1], A[r] = A[r], A[i + 1]
A[j] = tmp
tmp = A[i+1]
A[i+1] = A[r]
A[r] = tmp
return i + 1 return i + 1
if __name__ == "__main__": def main():
print('Enter values seperated by space:') # Python 2's `raw_input` has been renamed to `input` in Python 3
A = [int (item) for item in input().split(' ')] if sys.version_info.major < 3:
# A = [23, 45, 43, 12, 67, 98, 123, 99] input_function = raw_input
# partition(A, 0, 7) else:
print(A) input_function = input
quicksort(A, 0, 7)
print(A) try:
print("Enter numbers separated by spaces")
s = input_function()
inputs = list(map(int, s.split(' ')))
except Exception as e:
print(e)
else:
sorted_input = quick_sort(inputs, 0, len(inputs) - 1)
print('\nSorted list (min to max): {}'.format(sorted_input))
if __name__ == '__main__':
print('==== Quick Sort ====\n')
main()

View File

@ -1,2 +1,89 @@
# Python- # The Algorithms - Python
All Algorithms implemented in Python
### All algorithms implemented in Python (for education)
These are for demonstration purposes only. There are many implementations of sorts in the Python standard library that are much better for performance reasons.
## Sorting Algorithms
### Binary
Add comments here
### Bubble
![alt text][bubble-image]
From [Wikipedia][bubble-wiki]: Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.
__Properties__
* Worst case performance O(n^2)
* Best case performance O(n)
* Average case performance O(n^2)
###### View the algorithm in [action][bubble-toptal]
### Insertion
![alt text][insertion-image]
From [Wikipedia][insertion-wiki]: 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.
__Properties__
* Worst case performance O(n^2)
* Best case performance O(n)
* Average case performance O(n^2)
###### View the algorithm in [action][insertion-toptal]
## Merge
![alt text][merge-image]
From [Wikipedia][merge-wiki]: In computer science, merge sort (also commonly spelled mergesort) 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. Mergesort is a divide and conquer algorithm that was invented by John von Neumann in 1945.
__Properties__
* Worst case performance O(n log n)
* Best case performance O(n)
* Average case performance O(n)
###### View the algorithm in [action][merge-toptal]
## Quick
![alt text][quick-image]
From [Wikipedia][quick-wiki]: Quicksort (sometimes called partition-exchange sort) is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order.
__Properties__
* Worst case performance O(n^2)
* Best case performance O(n log n) or O(n) with three-way partition
* Average case performance O(n^2)
###### View the algorithm in [action][quick-toptal]
## Search Algorithms
### Linear
Add comments here
## Ciphers
### Caesar
Add comments here
[bubble-toptal]: https://www.toptal.com/developers/sorting-algorithms/bubble-sort
[bubble-wiki]: https://en.wikipedia.org/wiki/Bubble_sort
[bubble-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Bubblesort-edited-color.svg/220px-Bubblesort-edited-color.svg.png "Bubble Sort"
[insertion-toptal]: https://www.toptal.com/developers/sorting-algorithms/insertion-sort
[insertion-wiki]: https://en.wikipedia.org/wiki/Insertion_sort
[insertion-image]: https://upload.wikimedia.org/wikipedia/commons/7/7e/Insertionsort-edited.png "Insertion Sort"
[quick-toptal]: https://www.toptal.com/developers/sorting-algorithms/quick-sort
[quick-wiki]: https://en.wikipedia.org/wiki/Quicksort
[quick-image]: https://upload.wikimedia.org/wikipedia/commons/6/6a/Sorting_quicksort_anim.gif "Quick Sort"
[merge-toptal]: https://www.toptal.com/developers/sorting-algorithms/merge-sort
[merge-wiki]: https://en.wikipedia.org/wiki/Merge_sort
[merge-image]: https://upload.wikimedia.org/wikipedia/commons/c/cc/Merge-sort-example-300px.gif "Merge Sort"