Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Data Structure with Python
Author Message
alpesh

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
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)

alpesh

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)
alpesh

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)
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 3 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: