-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistsForSorting.py
More file actions
96 lines (67 loc) · 2.42 KB
/
listsForSorting.py
File metadata and controls
96 lines (67 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# This program is used to generate ordered and unordered lists.
# The lists are then used for computer science students to
# apply their understanding of standard searches and sorts.
# the line below imports the random library, which enables us to use lots of functions with random.
import random
# the line below imports the time library, which enables us to use lots of functions with time
import time
# we need to time our program, so we are going to set the start time
start_time = time.time()
myList = []
numberToFind = 700
def unorderedList():
# this function creates an unordered list
for i in range(0,25):
# the line line below simply appends a random number into our list
myList.append(random.randrange(1,90000))
return myList
def orderedList():
# this function creates an ordered list
for i in range(0,25):
myList.append(i)
return myList
# this is where we call the functions to make our lists
# uncomment out whichever list you want to make
unorderedList()
# orderedList()
# =========================================================
# Your different sorting algorithms will go under this line
# =========================================================
def linearSearch(list,target):
length = len(list)
for i in range(0,length):
if i == target:
return i
return -1
def bubbleSort(list):
swapped = True
while swapped:
swapped = False
for i in range(0,(len(list)-1)):
if list[i] > list[(i+1)]:
firstNumber = list[i]
secondNumber = list[(i+1)]
list[i] = secondNumber
list[(i+1)] = firstNumber
swapped=True
return list
# =========================================================
# Your different sorting algorithms will go above this line
# =========================================================
# =========================================================
# The lines below call a function. For sorting, we like to
# print the unsorted list and sorted list
# =========================================================
print(myList)
print("")
print(bubbleSort(myList))
# the lines below are used only for debugging.
# print(myList)
# The line below should print true if our target exists in our list.
# print(numberToFind in myList)
print("--- %s seconds ---" % (time.time() - start_time))
#
# references
#
# I am grateful to stackoverflow for this code for timing a python program:
# https://stackoverflow.com/questions/1557571/how-do-i-get-time-of-a-python-programs-execution