 # Linear Sort

Apr 27th, 2021
108
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. from random import randint
2.
3. def generateOrderedList(x):
4.     elements = list(range(1,x))
5.     return elements
6.
7. def generateRandomList(numberOfElements):
8.     #create list of random numbers
9.     elements = []
10.     for i in range(0,numberOfElements):
11.         value = randint(1,50)
12.         elements.append(value)
13.     return elements
14.
15. def linearSortWhile(elements,element):
16.     #loop through list until found or end of list is reached
17.     pos = 0
18.     while pos < len(elements):
19.         if elements[pos] == element:
20.             return pos
21.         pos +=1
22.     return -1
23.
24. def linearSortFor(elements,element):
25.     #loop through list until found or end of list is reached
26.     for pos in range(len(elements)):
27.         if elements[pos] == element:
28.             return pos
29.         pos +=1
30.     return -1
31.
32.
33. elements = (generateRandomList(25))
34. element = 20
35. location = linearSortFor(elements,element)
36. print(elements)
37. if location == -1:
38.     print(str(element) + ' is not in the list')
39. else:
40.     print(str(element) + ' was found as position ' + str(location +1))
41.
42.