| Author | Message |
|---|---|
| alpesh |
Post Posted: Fri Jul 12, 2024 6:41 am Post subject: Data Structure with Python
|
| Program collection for Data Structure
Binary Search ------------------------- code: def binary_sort(sorted_list, length, key): start = 0 end = length - 1 while start <= end: mid = int((start + end) / 2) if key == sorted_list[mid]: print("\nEntered number %d is present " "at position: %d" % (key, mid)) return -1 elif key < sorted_list[mid]: end = mid - 1 elif key > sorted_list[mid]: start = mid + 1 print("\nElement not found!") return -1 lst = [] size = int(input("Enter size of list: \t")) for n in range(size): numbers = int(input("Enter any number: \t")) lst.append(numbers) lst.sort() print('\n\nThe list will be sorted, the sorted list is:', lst) x = int(input("\nEnter the number to search: ")) binary_sort(lst, size, x) Bubble Sort ---------------------- code: def bubble_sort(sort_list): for j in range(len(sort_list)): for k in range(len(sort_list) - 1): if sort_list[k] > sort_list[k + 1]: sort_list[k], sort_list[k + 1] = sort_list[k + 1], sort_list[k] 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) bubble_sort(lst) |
|