Browse code

Added insertion sort

hdw authored on 02/08/2020 00:37:36
Showing 3 changed files

1 1
new file mode 100644
... ...
@@ -0,0 +1,28 @@
1
+'''
2
+Insertion sort algorithm
3
+Hayden Walker
4
+1 August 2020
5
+'''
6
+
7
+def insertionSort(listIn):
8
+	# Starting with the second item in the list, iterate through the list
9
+	for item in range(1, len(listIn)):
10
+		indexToTest = item # Get the index we'll be sorting
11
+		indexToSwap = item - 1 # Get the index we'll be testing the previous one against
12
+		
13
+		# Check all previous indexes for a smaller value than the one we're sorting
14
+		while indexToSwap >= 0:
15
+			if listIn[indexToTest] < listIn[indexToSwap]:
16
+				# If a smaller value is found, swap them
17
+				listIn[indexToTest], listIn[indexToSwap] = listIn[indexToSwap], listIn[indexToTest] 
18
+				# Set the test index to the one we swapped with
19
+				indexToTest = indexToSwap
20
+			# Move back an index
21
+			indexToSwap -= 1
22
+
23
+	# Return the sorted list
24
+	return listIn
25
+
26
+toSort = [13, 16, 9, 22, 29, 3]
27
+
28
+print(insertionSort(toSort))
... ...
@@ -45,16 +45,16 @@ Binary to Decimal Converter (18 August 2019)
45 45
     *Conditionals
46 46
     *Math operators
47 47
         *Powers
48
-    
48
+
49 49
     A simple binary to decimal converter; will accept an arbitrtary amount of bytes of arbitrary lengths.
50 50
 ------------------------------------------------------------------------------------------------------------
51 51
 Caesar Cypher (25 July 2019)
52 52
     *Iterables
53 53
     *Conditionals
54
-    *For Loops    
54
+    *For Loops
55 55
     *List wrapping
56 56
     *Functions
57
-    
57
+
58 58
     Takes an encryption key (n) from 0-25 and moves each character ahead n spaces in the alphabet. If the letter reaches z, it wraps back to a.
59 59
 ------------------------------------------------------------------------------------------------------------
60 60
 Cash register (3 August 2019)
... ...
@@ -62,7 +62,7 @@ Cash register (3 August 2019)
62 62
     *While Loops
63 63
     *Conditionals
64 64
     *Functions
65
-    
65
+
66 66
     Takes a sales total and amount tendered, then calculates change and lists the amount of each denomination of change to return.
67 67
 ------------------------------------------------------------------------------------------------------------
68 68
 Collatz Conjecture (7 August 2019) [Requires Matplotlib]
... ...
@@ -74,7 +74,7 @@ Collatz Conjecture (7 August 2019) [Requires Matplotlib]
74 74
         *Floor division
75 75
         *Addition
76 76
     *Matplotlib Graphing Library
77
-    
77
+
78 78
     Takes a positive integer (n), and depending on its parity, will either divide it by 2 or multiply it by 3 then add 1, and repeat the whole process until the integer is equal to 1. It will then display a graph where x is the number of steps and y is the value of n.
79 79
 ------------------------------------------------------------------------------------------------------------
80 80
 Conway's Game of Life (22 August 2019) [Requires Pygame]
... ...
@@ -101,7 +101,7 @@ Line/square drawing (27 July 2019)
101 101
     *For Loops
102 102
     *Functions
103 103
     *Methods
104
-    
104
+
105 105
     A GUI application that takes a number (n) and, depending on which button is pressed, will generate either n lines or n rectangles, all of random size, position, and colour. It will display the last action completed in a status bar at the bottom.
106 106
 ------------------------------------------------------------------------------------------------------------
107 107
 Hangman (5 August 2019)
... ...
@@ -112,9 +112,17 @@ Hangman (5 August 2019)
112 112
     *Conditionals
113 113
     *Iterables
114 114
     *Functions
115
-    
115
+
116 116
     A game of hangman that pulls words from a 100-word text file.
117 117
 ------------------------------------------------------------------------------------------------------------
118
+Insertion Sort (1 August 2020)
119
+    *Iteration
120
+    *Lists
121
+    *For loops
122
+    *Algorithms
123
+
124
+    Basic insertion sort algorithm
125
+------------------------------------------------------------------------------------------------------------
118 126
 Minefield/Minesweeper (20 July 2019)
119 127
     *System commands
120 128
     *Random Library
... ...
@@ -123,7 +131,7 @@ Minefield/Minesweeper (20 July 2019)
123 131
     *Conditionals
124 132
     *Functions
125 133
     *Game loop
126
-    
134
+
127 135
     A game that draws a 5x5 grid, in which a random number of randomly placed mines are hidden. Goal: Clear the board without hitting a mine.
128 136
 ------------------------------------------------------------------------------------------------------------
129 137
 Pig Latin (19 July)
... ...
@@ -140,7 +148,7 @@ Recursive Guessing Game (24 August)
140 148
         *Floor division
141 149
         *Addition
142 150
 
143
-    The user chooses a number between 0 and 100 (unknown to the computer), and the computer will guess it. A light project that I included because of recursion. 
151
+    The user chooses a number between 0 and 100 (unknown to the computer), and the computer will guess it. A light project that I included because of recursion.
144 152
 ------------------------------------------------------------------------------------------------------------
145 153
 Roman Numerals to Integer (19 January 2020)
146 154
     *Dictionary data type
... ...
@@ -158,5 +166,5 @@ Monty Python's Python Soundboard (31 July)
158 166
     *Lambda Expressions
159 167
     *Object-Oriented Programming
160 168
     *Playing audio files
161
-    
169
+
162 170
     A just-for-fun soundboard of clips from Monty Python's Life of Brian. Fun Fact: The Python Programming Language is named after Monty Python!
163 171
Binary files a/README.pdf and b/README.pdf differ