Data Structure with Python
Author |
Message |
alpesh
![](http://compsci.ca/v3/uploads/user_avatars/69222709846089e6556515.jpg)
|
Posted: Fri Jul 12, 2024 6:41 am Post subject: Data Structure with Python |
|
|
Program collection for Data Structure
Binary Search
-------------------------
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
----------------------
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) |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
alpesh
![](http://compsci.ca/v3/uploads/user_avatars/69222709846089e6556515.jpg)
|
Posted: 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) |
|
|
|
|
![](images/spacer.gif) |
alpesh
![](http://compsci.ca/v3/uploads/user_avatars/69222709846089e6556515.jpg)
|
Posted: 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) |
|
|
|
|
![](images/spacer.gif) |
|
|