Skip to content

Commit b566055

Browse files
committed
Merge branch 'master' of https://github.com/TheAlgorithms/Python
2 parents 5f90dfb + b63a111 commit b566055

File tree

7 files changed

+1854
-12
lines changed

7 files changed

+1854
-12
lines changed

README.md

+14-11
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ These are for demonstration purposes only. There are many implementations of sor
1010
### Bubble
1111
![alt text][bubble-image]
1212

13-
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.
13+
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.
1414

1515
__Properties__
1616
* Worst case performance O(n<sup>2</sup>)
@@ -44,7 +44,7 @@ __Properties__
4444
### Insertion
4545
![alt text][insertion-image]
4646

47-
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.
47+
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.
4848

4949
__Properties__
5050
* Worst case performance O(n<sup>2</sup>)
@@ -57,7 +57,7 @@ __Properties__
5757
### Merge
5858
![alt text][merge-image]
5959

60-
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.
60+
From [Wikipedia][merge-wiki]: **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.
6161

6262
__Properties__
6363
* Worst case performance O(n log n)
@@ -70,7 +70,7 @@ __Properties__
7070
### Quick
7171
![alt text][quick-image]
7272

73-
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.
73+
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.
7474

7575
__Properties__
7676
* Worst case performance O(n<sup>2</sup>)
@@ -105,7 +105,7 @@ __Properties__
105105
### Selection
106106
![alt text][selection-image]
107107

108-
From [Wikipedia][selection-wiki]: The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.
108+
From [Wikipedia][selection-wiki]: **Selection sort** is an algorithm that divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.
109109

110110
__Properties__
111111
* Worst case performance O(n<sup>2</sup>)
@@ -117,7 +117,7 @@ __Properties__
117117
### Shell
118118
![alt text][shell-image]
119119

120-
From [Wikipedia][shell-wiki]: Shellsort is a generalization of insertion sort that allows the exchange of items that are far apart. The idea is to arrange the list of elements so that, starting anywhere, considering every nth element gives a sorted list. Such a list is said to be h-sorted. Equivalently, it can be thought of as h interleaved lists, each individually sorted.
120+
From [Wikipedia][shell-wiki]: **Shellsort** is a generalization of *insertion sort* that allows the exchange of items that are far apart. The idea is to arrange the list of elements so that, starting anywhere, considering every nth element gives a sorted list. Such a list is said to be h-sorted. Equivalently, it can be thought of as h interleaved lists, each individually sorted.
121121

122122
__Properties__
123123
* Worst case performance O(nlog2 2n)
@@ -132,7 +132,7 @@ From [Wikipedia][topological-wiki]: In the field of computer science, a topologi
132132

133133
### Time-Complexity Graphs
134134

135-
Comparing the complexity of sorting algorithms (Bubble Sort, Insertion Sort, Selection Sort)
135+
Comparing the complexity of sorting algorithms (*Bubble Sort*, *Insertion Sort*, *Selection Sort*)
136136

137137
![Complexity Graphs](https://github.com/prateekiiest/Python/blob/master/sorts/sortinggraphs.png)
138138

@@ -147,8 +147,7 @@ Choosing of a sort technique-Quicksort is a very fast algorithm but can be prett
147147
### Linear
148148
![alt text][linear-image]
149149

150-
From [Wikipedia][linear-wiki]: linear search or sequential search is a method for finding a target value within a list. It sequentially checks each element of the list for the target value until a match is found or until all the elements have been searched.
151-
Linear search runs in at worst linear time and makes at most n comparisons, where n is the length of the list.
150+
From [Wikipedia][linear-wiki]: **Linear search** or *sequential search* is a method for finding a target value within a list. It sequentially checks each element of the list for the target value until a match is found or until all the elements have been searched. Linear search runs in at worst linear time and makes at most n comparisons, where n is the length of the list.
152151

153152
__Properties__
154153
* Worst case performance O(n)
@@ -159,7 +158,7 @@ __Properties__
159158
### Binary
160159
![alt text][binary-image]
161160

162-
From [Wikipedia][binary-wiki]: Binary search, also known as half-interval search or logarithmic search, is a search algorithm that finds the position of a target value within a sorted array. It compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until it is successful.
161+
From [Wikipedia][binary-wiki]: **Binary search**, also known as *half-interval search* or *logarithmic search*, is a search algorithm that finds the position of a target value within a sorted array. It compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until it is successful.
163162

164163
__Properties__
165164
* Worst case performance O(log n)
@@ -205,7 +204,7 @@ These memory structures form what is known as the tabu list, a set of rules and
205204

206205
### Caesar
207206
![alt text][caesar]<br>
208-
In cryptography, a **Caesar cipher**, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques.<br>
207+
**Caesar cipher**, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques.<br>
209208
It is **a type of substitution cipher** in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on. <br>
210209
The method is named after **Julius Caesar**, who used it in his private correspondence.<br>
211210
The encryption step performed by a Caesar cipher is often incorporated as part of more complex schemes, such as the Vigenère cipher, and still has modern application in the ROT13 system. As with all single-alphabet substitution ciphers, the Caesar cipher is easily broken and in modern practice offers essentially no communication security.
@@ -219,7 +218,11 @@ Many people have tried to implement encryption schemes that are essentially Vige
219218
###### Source: [Wikipedia](https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher)
220219

221220
### Transposition
221+
<<<<<<< HEAD
222222
In cryptography, a **transposition cipher** is a method of encryption by which the positions held by units of plaintext (which are commonly characters or groups of characters) are shifted according to a regular system, so that the ciphertext constitutes a permutation of the plaintext. That is, the order of the units is changed (the plaintext is reordered).<br>
223+
=======
224+
The **Transposition cipher** is a method of encryption by which the positions held by units of *plaintext* (which are commonly characters or groups of characters) are shifted according to a regular system, so that the *ciphertext* constitutes a permutation of the plaintext. That is, the order of the units is changed (the plaintext is reordered).<br>
225+
>>>>>>> 3dab8e03a465397a7b671128c155c9c03f8e0154
223226
Mathematically a bijective function is used on the characters' positions to encrypt and an inverse function to decrypt.
224227
###### Source: [Wikipedia](https://en.wikipedia.org/wiki/Transposition_cipher)
225228

ciphers/onepad_cipher.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def decrypt(self, cipher, key):
2020
'''Function to decrypt text using psedo-random numbers.'''
2121
plain = []
2222
for i in range(len(key)):
23-
p = (cipher[i]-(key[i])**2)/key[i]
23+
p = int((cipher[i]-(key[i])**2)/key[i])
2424
plain.append(chr(p))
2525
plain = ''.join([i for i in plain])
2626
return plain
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from itertools import accumulate
2+
from bisect import bisect
3+
4+
def fracKnapsack(vl, wt, W, n):
5+
6+
r = list(sorted(zip(vl,wt), key=lambda x:x[0]/x[1],reverse=True))
7+
vl , wt = [i[0] for i in r],[i[1] for i in r]
8+
acc=list(accumulate(wt))
9+
k = bisect(acc,W)
10+
return 0 if k == 0 else sum(vl[:k])+(W-acc[k-1])*(vl[k])/(wt[k]) if k!=n else sum(vl[:k])
11+
12+
print("%.0f"%fracKnapsack([60, 100, 120],[10, 20, 30],50,3))

0 commit comments

Comments
 (0)