Advertisement
dmesticg

Untitled

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