mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Changed BubbleSort.py and InsertionSort.py to allow x number of inputs. Also worked on LinearSearch.py
This commit is contained in:
parent
549915acd4
commit
b7eae6b0e3
@ -12,13 +12,15 @@ def simple_bubble_sort(int_list):
|
|||||||
return int_list
|
return int_list
|
||||||
|
|
||||||
|
|
||||||
def main(num):
|
def main():
|
||||||
inputs = []
|
|
||||||
print("Enter any {} numbers for unsorted list: ".format(num))
|
|
||||||
try:
|
try:
|
||||||
for i in range(num):
|
print("Enter numbers separated by spaces:")
|
||||||
n = input()
|
s = raw_input()
|
||||||
inputs.append(n)
|
inputs = list(map(int, s.split(' ')))
|
||||||
|
if len(inputs) < 2:
|
||||||
|
print('No Enough values to sort!')
|
||||||
|
raise Exception
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
else:
|
else:
|
||||||
@ -27,5 +29,4 @@ def main(num):
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print('==== Bubble Sort ====\n')
|
print('==== Bubble Sort ====\n')
|
||||||
list_count = 6
|
main()
|
||||||
main(list_count)
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
|
|
||||||
def simple_insertion_sort(int_list):
|
def simple_insertion_sort(int_list):
|
||||||
for i in range(1, 6):
|
count = len(int_list)
|
||||||
|
for i in range(1, count):
|
||||||
temp = int_list[i]
|
temp = int_list[i]
|
||||||
j = i - 1
|
j = i - 1
|
||||||
while(j >= 0 and temp < int_list[j]):
|
while(j >= 0 and temp < int_list[j]):
|
||||||
@ -11,13 +12,15 @@ def simple_insertion_sort(int_list):
|
|||||||
return int_list
|
return int_list
|
||||||
|
|
||||||
|
|
||||||
def main(num):
|
def main():
|
||||||
inputs = []
|
|
||||||
print('Enter any {} numbers for unsorted list: '.format(num))
|
|
||||||
try:
|
try:
|
||||||
for i in range(num):
|
print("Enter numbers separated by spaces:")
|
||||||
n = input()
|
s = raw_input()
|
||||||
inputs.append(n)
|
inputs = list(map(int, s.split(' ')))
|
||||||
|
if len(inputs) < 2:
|
||||||
|
print('No Enough values to sort!')
|
||||||
|
raise Exception
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
else:
|
else:
|
||||||
@ -26,5 +29,4 @@ def main(num):
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print('==== Insertion Sort ====\n')
|
print('==== Insertion Sort ====\n')
|
||||||
list_count = 6
|
main()
|
||||||
main(list_count)
|
|
||||||
|
@ -1,21 +1,24 @@
|
|||||||
def sequentialSearch(alist, item):
|
|
||||||
pos = 0
|
|
||||||
found = False
|
|
||||||
|
|
||||||
while pos < len(alist) and not found:
|
|
||||||
|
|
||||||
if alist[pos] == item:
|
|
||||||
found = True
|
|
||||||
print("Found")
|
|
||||||
else:
|
|
||||||
pos = pos+1
|
|
||||||
if found == False:
|
|
||||||
print("Not found")
|
|
||||||
return found
|
|
||||||
|
|
||||||
print("Enter numbers seprated by space")
|
|
||||||
s = input()
|
|
||||||
numbers = list(map(int, s.split()))
|
|
||||||
trgt =int( input('enter a single number to be found in the list '))
|
|
||||||
sequentialSearch(numbers, trgt)
|
|
||||||
|
|
||||||
|
def sequential_search(alist, target):
|
||||||
|
for index, item in enumerate(alist):
|
||||||
|
if item == target:
|
||||||
|
print("Found target {} at index {}".format(target, index))
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
print("Not found")
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
try:
|
||||||
|
print("Enter numbers separated by spaces")
|
||||||
|
s = raw_input()
|
||||||
|
inputs = list(map(int, s.split(' ')))
|
||||||
|
target = int(raw_input('\nEnter a single number to be found in the list: '))
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
else:
|
||||||
|
sequential_search(inputs, target)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print('==== Insertion Sort ====\n')
|
||||||
|
main()
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
### **All Algorithms implemented in Python!**
|
### **All Algorithms implemented in Python!**
|
||||||
|
|
||||||
|
These are for demonstration purposes only. There are many implementations of sorts in the Python standard library that are much better for performance reasons.
|
||||||
|
|
||||||
## Sorting
|
## Sorting
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user