Knowledge Base Reasoning
Sunday, March 24, 2019
Sunday, February 3, 2019
Bucket Sort
Algorithm:
Stable:no
Python Implementation:
def bucket_sort(arr):
n = len(arr)
buckets = [[] for _ in range(n)]
for i in range(n):
b_index = arr[i]*n
buckets[b_index] = arr[i]
result = []
for bucket in buckets:
result = result+insertion_sort(bucket)
return result
def insertion_sort(arr):
j = len(arr)
for i in range(j):
j= i
while j >0 and arr[j-1] > arr[j]:
arr[j-1], arr[j] = arr[j], arr[j-1]
j -= 1
- Create Buckets of same size as of the array
- Distribute element of array into buckets
- Sort each bucket(using any sorting algorithm) and add them back together
- When the elements in the array is uniformly distributed.
- Best/Average: O(n+k) -takes n times to distribute each element to k buckets and k times to add them back together.
- Worst: O(n2)- when all the elements fall into single bucket
- O(n) - usually no extra space is required
Stable:no
Python Implementation:
def bucket_sort(arr):
n = len(arr)
buckets = [[] for _ in range(n)]
for i in range(n):
b_index = arr[i]*n
buckets[b_index] = arr[i]
result = []
for bucket in buckets:
result = result+insertion_sort(bucket)
return result
def insertion_sort(arr):
j = len(arr)
for i in range(j):
j= i
while j >0 and arr[j-1] > arr[j]:
arr[j-1], arr[j] = arr[j], arr[j-1]
j -= 1
Thursday, January 17, 2019
Quick Sort
Algorithm:
Stable:no
Python Implementation
arr = [1,2,5,4,9]
low = 0
high = len(arr)-1
def quick_sort(arr, low, high):
if low < high:
pi = partition(arr, low, high)
quick_sort(arr,low,pi-1)
quick_sort(arr,pi+1,high)
return arr
def partition(arr, low, high):
pivot = arr[high]
i = low -1
for j in range(low, high-1):
if arr[j] < pivot:
i += 1
arr[j], arr[i] = arr[i], arr[j]
arr[i+1], arr[high] = arr[high], arr[i+1]
return i+1
- Select a pivot element in the array
- Move the selected pivot to its correct location in array such that all elements on its left is less than it and all elements on the right are greater than it.
- Best/Average: O(nlogn) -list is divided into two halves and it takes linear time to merge the list back.
- Worst: O(n2)- when either largest or smallest element is selected as pivot. Also, when the list is already sorted.
- O(1) - usually no extra space is required
Stable:no
Python Implementation
arr = [1,2,5,4,9]
low = 0
high = len(arr)-1
def quick_sort(arr, low, high):
if low < high:
pi = partition(arr, low, high)
quick_sort(arr,low,pi-1)
quick_sort(arr,pi+1,high)
return arr
def partition(arr, low, high):
pivot = arr[high]
i = low -1
for j in range(low, high-1):
if arr[j] < pivot:
i += 1
arr[j], arr[i] = arr[i], arr[j]
arr[i+1], arr[high] = arr[high], arr[i+1]
return i+1
Thursday, January 10, 2019
Merge Sort
Algorithm:
Stable: yes
Python Implementation
def merge_sort(arr):
m = len(arr)/2
L = arr[:m]
R = arr[m:]
merge_sort(L)
merge_sort(R)
i = j = k = 0
while i < len(L) and j < len(R):
if L[i] < R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
while i < len(L):
arr[k] = L[i]
i += 1
k += 1
while j< len[R]:
arr[k] = R[j]
j += 1
k += 1
- Iteratively Divide the list to two halves
- Sort and Merge the divided list until one list remains recursively
- Best/Worst/Average: O(nlogn) -list is divided into two halves and it takes linear time to merge the list back.
- O(n) - usually no extra space is required
Stable: yes
Python Implementation
def merge_sort(arr):
m = len(arr)/2
L = arr[:m]
R = arr[m:]
merge_sort(L)
merge_sort(R)
i = j = k = 0
while i < len(L) and j < len(R):
if L[i] < R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
while i < len(L):
arr[k] = L[i]
i += 1
k += 1
while j< len[R]:
arr[k] = R[j]
j += 1
k += 1
Thursday, January 3, 2019
Insertion Sort
In insertion sort, the sorting task is similar to that of sorting a deck of cards.
Algorithm:
Stable: yes
Python Implementation
def insertion_sort(arr):
n = len(arr)
for i in range(n):
j = i
while j > 0 and arr[j-1] > arr[j]:
arr[j-1], arr[j] = arr[j], arr[j-1]
j -= 1
Algorithm:
- Start at the beginning
- Find the next element and bring it to its sorted position
- Best: O(n) - when the list is already sorted
- Worst: O(n2) - when required to reverse the entire list
- O(1) - usually no extra space is required
Stable: yes
Python Implementation
def insertion_sort(arr):
n = len(arr)
for i in range(n):
j = i
while j > 0 and arr[j-1] > arr[j]:
arr[j-1], arr[j] = arr[j], arr[j-1]
j -= 1
Friday, December 21, 2018
Selection Sort
Algorithm:
Stable: yes
Python Implementation:
def selection_sort(arr):
n = len(arr)
for i range(n):
min_index = i
for j in range(i+1, n):
if a[min_index] > a[j]:
min_index = j
a[i], a[min_index] = a[min_index], a[i]
- Find smallest element with linear scan
- Move it to the front
- Continue until all elements are in place
- Best: O(n2) - when the list is already sorted (https://stackoverflow.com/questions/43298396/best-case-time-complexity-for-selection-sort)
- Worst: O(n2) - when required to reverse the list
- O(1) - usually no extra space is required
Stable: yes
Python Implementation:
def selection_sort(arr):
n = len(arr)
for i range(n):
min_index = i
for j in range(i+1, n):
if a[min_index] > a[j]:
min_index = j
a[i], a[min_index] = a[min_index], a[i]
Bubble Sort
Algorithm:
Stable: yes
Python Implementation
def bubble_sort(array):
n = len(array)
for j in range(n):
for i in range(n-1-j):
if a[i] > a[i+1]:
a[i+1], a[i] = a[i], a[i+1]
- Start at the beginning
- Swap first two element if first is greater than second
- Continue to next pair
- Repeat until sorted
- Best: O(n) - when the list is already sorted
- Worst: O(n2) - when required to reverse the list
- O(1) - usually no extra space is required
Stable: yes
Python Implementation
def bubble_sort(array):
n = len(array)
for j in range(n):
for i in range(n-1-j):
if a[i] > a[i+1]:
a[i+1], a[i] = a[i], a[i+1]
Subscribe to:
Posts (Atom)
-
I have created an easy to follow step-by-step guide to setup virtuoso server and add DBpedia data to it. 1. Download open-link virtuoso u...
-
Positive Examples: Positive examples for predicate P are any entity pairs (x,y) in DBpedia such that (x,P,y) is in it. For example, the ...
-
Algorithm: Select a pivot element in the array Move the selected pivot to its correct location in array such that all elements on its le...