Advertisement
dmesticg

Untitled

Mar 9th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. import os
  2. from os.path import join
  3.  
  4. filename = input("What is the name of the file?")
  5. what_to_find = input("what do you want to find in the data?")
  6.  
  7. def findfile(file):
  8. data = []
  9. for root, dirs, files in os.walk('C:\Users\324086370'):
  10. print ("searching...")
  11. if file in files:
  12. with open(join(root, file),"r") as k:
  13. data = k.read()
  14. data = data.split("\n")
  15. return(data)
  16.  
  17. def bubbleSort(arr):
  18. isSorted = False
  19. while not(isSorted):
  20. for i in range(len(arr) - 1):
  21. for j in range(len(arr) - 1 - i):
  22. if arr[j] > arr[j+1]:
  23. arr[j], arr[j+1] = arr[j+1],arr[j]
  24. else:
  25. isSorted = True
  26. return (arr)
  27.  
  28. def selectionSort(arr):
  29. for i in range(len(arr)):
  30. minpos = 0
  31. for j in range(i,len(arr)):
  32. if arr[j] < arr[minpos]:
  33. minpos = j
  34. temp = arr[i]
  35. arr[i],arr[minpos] = arr[minpos],temp
  36. arr.append(arr[0])
  37. arr.pop(0)
  38. return (arr)
  39.  
  40. def insertionSort(arr):
  41. for i in range(len(arr)):
  42. element = arr[i]
  43. pos = i
  44. while (pos > 0 and arr[pos-1] > element):
  45. arr[pos] = arr[pos-1]
  46. pos = pos - 1
  47. arr[pos] = element
  48. return (arr)
  49.  
  50. def binarySearch(arr,target):
  51. low = 0
  52. high = len(arr)
  53. while low <= high:
  54. mid = (low + high) // 2
  55. if target == arr[mid]:
  56. return (True,mid)
  57. elif target < arr[mid]:
  58. high = mid - 1
  59. elif target > arr[mid]:
  60. low = mid + 1
  61. return (False,None)
  62.  
  63. data = findfile(filename)
  64.  
  65. didfind,position= binarySearch(insertionSort(data),what_to_find)
  66.  
  67. print("BubbleSort:",bubbleSort(data))
  68. print("SelectionSort:",selectionSort(data))
  69. print("InsertionSort",insertionSort(data))
  70. print("BinarySearch: Found",what_to_find,"in position ",position+1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement