Posts

Showing posts from June, 2018

data_structure) 6 sorting algorithms using python(2)

Hello, this is second posting about sorting algorithms. I'll show you rest 3 sorting algorithms. Fourth is merge sort. This algorithm has O(nlogn) time complexity regardless Best, average, Worst case. I think this is really great. Also, this algorithm is stable. def __merge ( data , left , middle , right ): a = data[left:middle + 1 ] b = data[middle + 1 : right + 1 ] i = 0 j = 0 k = left while i < len (a) and j < len (b): if a[i] <= b[j]: data[k] = a[i] i += 1 else : data[k] = b[j] j += 1 k += 1 while i < len (a): data[k] = a[i] i += 1 k += 1 while j < len (b): data[k] = b[j] j += 1 k += 1 def __merge_sort ( data , left , right ): if left >= right: return m = int ((left + right) / 2 ) __merge_sort(data, left, m) ...

Data_structure) 6 sorting algorithms using python~!!

Hello everyone, I'm lulu. Today I'm going to show you 6 sorting algorithms with time complexity and if it's stable or not. First is bubble sort. This algorithm has O(n^2) time complexity and this algorithm is stable. However, I think O(n^2) time complexity regardless Best, Average, Worst case is not that great. def bubble_sort ( data ): n = len (data) for i in range (n - 1 , 0 , - 1 ): for j in range ( 0 , i): if data[j] > data[j + 1 ]: data[j], data[j + 1 ] = data[j + 1 ], data[j] return data a=[ 2 , 5 , 4 , 1 ] print (bubble_sort(a)) This is the source code of bubble_sort. Second is insertion sort. This algorithm's Best case has O(n) time complexity but Average and Worst case has O(n^2) time complexity. Also it is stable. def insertion_sort ( data ): n = len (data) for i in range ( 1 , n): val = data[i] j = i while j > ...