Computer Science Canada Data Structure with Python |
Author: | alpesh [ Fri Jul 12, 2024 6:41 am ] | ||||
Post subject: | Data Structure with Python | ||||
Program collection for Data Structure Binary Search -------------------------
Bubble Sort ----------------------
|
Author: | alpesh [ Fri Jul 12, 2024 6:47 am ] |
Post subject: | RE:Data Structure with Python |
Insertion Sort --------------------- def insertion_sort(sort_list): for i in range(1, len(sort_list)): key = sort_list[i] j = i - 1 while j >= 0 and key < sort_list[j]: sort_list[j + 1] = sort_list[j] j -= 1 sort_list[j + 1] = key print('\nThe sorted list: \t', sort_list) print('\n') lst = [] size = int(input("\nEnter size of the list: \t")) for i in range(size): elements = int(input("Enter the element: \t")) lst.append(elements) insertion_sort(lst) |
Author: | alpesh [ Fri Jul 12, 2024 6:47 am ] |
Post subject: | RE:Data Structure with Python |
Merge Sort ---------------------- def merge_sort(sort_list): print("splitting", sort_list) if len(sort_list) > 1: mid = len(sort_list) // 2 leftHalf = sort_list[:mid] rightHalf = sort_list[mid:] merge_sort(leftHalf) merge_sort(rightHalf) i = 0 j = 0 k = 0 while i < len(leftHalf) and j < len(rightHalf): if leftHalf[i] < rightHalf[j]: sort_list[k] = leftHalf[i] i = i + 1 else: sort_list[k] = rightHalf[j] j = j + 1 k = k + 1 while i < len(leftHalf): sort_list[k] = leftHalf[i] i = i + 1 k = k + 1 while j < len(rightHalf): sort_list[k] = rightHalf[j] j = j + 1 k = k + 1 print("merging...", sort_list) lst = [] size = int(input("Enter size of the list: \t")) for i in range(size): elements = int(input("Enter an element: \t")) lst.append(elements) merge_sort(lst) |