Skip to content
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
4 changes: 4 additions & 0 deletions data_structures_and_algorithms/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"ponicode.testSettings.testLocation.locationType": "Same folder as source file",
"ponicode.testSettings.testLocation.path": "{rootDir}/{filePath}/{fileName}.test.{ext}"
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"ponicode.testSettings.testLocation.locationType": "Same folder as source file",
"ponicode.testSettings.testLocation.path": "{rootDir}/{filePath}/{fileName}.test.{ext}"
}
Empty file.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = '0.1.0'
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def insertion_sort(arr):
# for each char in the array
if len(arr) < 1:
return "Not a valid input"
if len(arr) == 1:
return arr
for char in range(1, len(arr)):
# set check_char to 0
check_char = char - 1
# set temp to arr at 1
temp = arr[char]
# iterate through the array and check if temp is < arr[check]
while check_char >= 0 and temp < arr[check_char]:
# move a the check_char +1 to the check_char position
arr[check_char + 1] = arr[check_char]
# set check_char to check_char minus 1
check_char = check_char - 1
# set temp to the position after check_char
arr[check_char + 1] = temp
return arr
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# SELECTION-SORT

We divide the array into two parts: sorted and unsorted. The left part is sorted subarray and the right part is unsorted subarray. Initially, sorted subarray is empty and unsorted array is the complete given array.
or
Selection sort is an algorithm that selects the smallest element from an unsorted list in each iteration and places that element at the beginning of the unsorted list.

# Visualization
![Getting Started](selection-sort GIF.gif)

# Pseudocode
SelectionSort(int[] arr)
DECLARE n <-- arr.Length;
FOR i = 0; i to n - 1
DECLARE min <-- i;
FOR j = i + 1 to n
if (arr[j] < arr[min])
min <-- j;

DECLARE temp <-- arr[min];
arr[min] <-- arr[i];
arr[i] <-- temp;

# APPROACH FOR SELECTION SORT

1.Set the first element as minimum.

2.Compare minimum with the second element. If the second element is smaller than minimum, assign the second element as minimum.

Compare minimum with the third element. Again, if the third element is smaller, then assign minimum to the third element otherwise do nothing. The process goes on until the last element.

3.After each iteration, minimum is placed in the front of the unsorted list

4.For each iteration, indexing starts from the first unsorted element. They are repeated until all the elements are placed at their correct positions.

# steps
![Getting Started](selection-sort GIF.gif)
![Getting Started](selection-sort GIF.gif)
![Getting Started](selection-sort GIF.gif)
![Getting Started](selection-sort GIF.gif)
![Getting Started](selection-sort GIF.gif)

# COMPLEXITY: O(n2)

Also, we can analyze the complexity by simply observing the number of loops. There are 2 loops so the complexity is n*n = n2.

## Time Complexities:

# Worst Case Complexity: O(n2)
If we want to sort in ascending order and the array is in descending order then, the worst case occurs.
# Best Case Complexity: O(n2)
It occurs when the array is already sorted
# Average Case Complexity: O(n2)
It occurs when the elements of the array are neither ascending nor descending.


The time complexity of the selection sort is the same in all cases. At every step, you have to find the minimum element and put it in the right place. The minimum element is not known until the end of the array is not reached.


# Space Complexity:

Space complexity is O(1) because an extra variable temp is used.



# LINK FOR REFERENCE
mycodeschool:-
https://www.youtube.com/watch?v=GUDLRan2DWM
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[tool.poetry]
name = "Insertion_Sort"
version = "0.1.0"
description = ""
authors = ["MsDiala <diala-is-diala@hotmail.com>"]

[tool.poetry.dependencies]
python = "^3.9"

[tool.poetry.dev-dependencies]
pytest = "^5.2"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Stacks and Queues

we created a data structure called **Stack** and **Queue** in Python. We used three classes, _Node_ and _Queue_, and _Stack_.

### Challenge
- Write queue and stack classes with their methods

## Approach & Efficiency
Stack :
- Define a method called push
- Define a method called pop
- Define a method called peek

Queue:
- Define a method called enqueue
- Define a method called dequeue
- Define a method called peek big
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
class Node:
def __init__(self, value):
self.value = value
self.next = None

def __repr__(self):
return f'{self.value}'


class Stack:

def __init__(self):
'''initialize stack with top, bottom and length'''
self.top = None
self.bottom = None
self.length = 0

def isEmpty(self):
return self.top == None

def push(self, value):
'''adds new node to top of stack by pointing it to self.top'''
node = Node(value)
node.next = self.top
self.top = node
self.length += 1

def pop(self):
'''Takes item from top of stack and returns it by reassigning the current top
to the next item in the stack. Stores the value in a temp variable to be returned'''
if self.length <= 0:
print('nothing to pop')
return

temp = self.top
self.top = self.top.next
popped = temp.value
self.length -= 1
return popped

def peek(self):
'''prints and returns the top of the stack'''
if self.length <= 0:
print('nothing to peek')
return
print(self.top.value)
return self.top.value


class Queue:

def __init__(self):
'''initializes a queue instance'''
self.front = None
self.rear = None
self.length = 0

def isEmpty(self):
return self.front == None

def enqueue(self, value):
'''appends value to front of queue'''

self.length += 1
new_node = Node(value)

if self.rear == None:
self.front = self.rear = new_node

return

self.rear.next = new_node
self.rear = new_node

def dequeue(self):
'''removes value from front of queue, if length is zero it returns the queue
and prints a message'''
self.length -= 1

if self.isEmpty():
self.queue = []
print('queue is empty')
return self.queue

temp = self.front
self.front = temp.next

if self.front == None:
self.rear = None

return str(temp.value)

def peek(self):
'''returns the first value in a queue'''
return self.front.value
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
from stacks_and_queues import Node, Stack, Queue

def test_empty_stack():
test_stack = Stack()
assert isinstance(test_stack, Stack)

def test_push():
test_stack = Stack()
test_stack.push(1)
test_stack.push(2)
assert test_stack.top.value == 2

def test_muliple_nodes():
test_stack = Stack()
test_stack.push(1)
test_stack.push(2)
test_stack.push(3)
assert test_stack.length == 3

def test_pop():
test_stack = Stack()
test_stack.push(1)
test_stack.push(2)
test_stack.push(3)
popped = test_stack.pop()
assert popped == 3
assert test_stack.length == 2
assert test_stack.top.value == 2

def test_multipop():
test_stack = Stack()
test_stack.push(1)
test_stack.push(2)
test_stack.push(3)
test_stack.pop()
test_stack.pop()
test_stack.pop()
assert test_stack.length == 0
assert test_stack.bottom == None

def test_peek():
test_stack = Stack()
test_stack.push(1)
test_stack.push(2)
test_stack.push(3)

assert test_stack.peek() == 3

def test_enqueue():
q = Queue()
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)

assert q.front.value == 1

def test_enqueue_multiple():
q = Queue()
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)

assert q.length == 3

def test_dequeue():
q = Queue()
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)
q.dequeue()

assert q.length == 2
assert q.front.value == 2

def test_enqueue_empty_queue():
q = Queue()
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)
q.dequeue()
q.dequeue()
q.dequeue()

assert q.length == 0

def test_instantiate_empty():
q = Queue()

assert q.length == 0
assert q.front == None


def test_q_peek():
q = Queue()
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)

assert q.peek() == 1
Binary file not shown.
Loading