diff --git a/Searching/BFS/bfs in python.py b/Searching/BFS/bfs in python.py new file mode 100644 index 0000000..5caf044 --- /dev/null +++ b/Searching/BFS/bfs in python.py @@ -0,0 +1,12 @@ +# BREADTH FIRST SEARCH + +def bfs(self,vertex): + visited = [vertex] # O(1) + queue = [vertex] + while queue: # O(V) ,,,v is num of vertices + deVertex = queue.pop(0) + print(deVertex) + for adjacentVertex in self.gdict[deVertex]: #O(E) ,,,E is num of edges + if adjacentVertex not in visited: + visited.append(adjacentVertex) + queue.append(adjacentVertex) \ No newline at end of file diff --git a/Sorting/Bucket-Sort/BucketSort.py b/Sorting/Bucket-Sort/BucketSort.py new file mode 100644 index 0000000..3be461f --- /dev/null +++ b/Sorting/Bucket-Sort/BucketSort.py @@ -0,0 +1,43 @@ +# BOCKET SORT WITH THE HELP OF INSERTION SORT + + +# INSERTION SORT +def insertionSort(customList): + for i in range(1,len(customList)): + key=customList[i] + j=i-1 + while j>=0 and key < customList[j]: + customList[j+1] = customList[j] + j -= 1 + customList[j+1] = key + return customList + + +# BUCKET SORT +import math +def bucketSort(customList): + numberofBuckets = round(math.sqrt(len(customList))) + maxValue = max(customList) + arr =[] + + for i in range(numberofBuckets): + arr.append([]) + for j in customList: + index_b = math.ceil(j*numberofBuckets/maxValue) + arr[index_b-1].append(j) + + for i in range(numberofBuckets): + arr[i] = insertionSort(arr[i]) + + k = 0 + for i in range(numberofBuckets): + for j in range(len(arr[i])): + customList[k] = arr[i][j] + k+=1 + return customList + + + +cList=[2,1,3,6,9,7,4,8,5] +print(bucketSort(cList)) +