Skip to content
This repository has been archived by the owner on Oct 2, 2020. It is now read-only.

queue in python #407

Open
wants to merge 2 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
30 changes: 30 additions & 0 deletions Algorithms/Counting_Sort/!
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Sorting.Util
class CountingSort:
def sort(self,array):
smaller = min(array)

keyValue = 0
if smaller < 0: keyValue = 1 - smaller

lenArray = len(array)
tempArray = [0] * lenArray

for i in range(lenArray):
tempArray[i] = array[i] + keyValue

bigger = max(tempArray)
smaller = min(tempArray)
size = (bigger - smaller) + 2

contArray = [0] * size

for i in range(len(tempArray)):
contArray[tempArray[i] - smaller + 1] += 1

for i in range(1, len(contArray)):
contArray[i] += contArray[i - 1]


for i in range(len(tempArray)):
array[contArray[tempArray[i] - smaller + 1] -1] = tempArray[i]
contArray[tempArray[i] - smaller + 1] -= 1
31 changes: 31 additions & 0 deletions Data Structures/Queue/queue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Queue:

def __init__(self):
self._array = []

def add(self, element):
self._array.append ( element )

def peek(self):
ans = None
if not self.isEmpty:
ans = self._array[0]
return ans


@property
def isEmpty(self):
return len ( self._array ) == 0


def poll(self):
ans = None
if not self.isEmpty:
ans = self._array[0]
self._leftShift ()
self._array.pop()
return ans

def _leftShift(self):
for i in range ( len ( self._array ) - 1 ):
self._array[i] = self._array[i + 1]