Advertisement
dmesticg

Untitled

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