Sign in to follow this
Followers
0
-
entries
2 -
comments
5 -
views
1,488
About this blog
A blog on my journey to becoming a better programmer.
Entries in this blog
Ambition for a Long While
Things checked out on my end for the answers. Such a short section with so much useful information, I had to keep going. after more principles, I had failed to realize something that should have been obvious, involving the direction of the book. regardless, I did my first problem of the book: I probably totally failed it, in that microsecond does not constitute 1000 of itself into a second. Had I paid attention in the non-existent weights and measures class they call elementary science, I would have known. But that's kind of a scaling issue as I found out from my implications. So one of the functions of f(n) was f(n) = log2(n) It wasn't so easy to solve. My first assumption was to create real values for n, and not some representative formula. My first attempts did not posses efficiency, because I am so under-educated about RUBY for one. After talking it over with ruby programmers on the server, I had arrived at this method for the solution
My first thoughts are, why does that work. I still don't know. but if t is say, 2, the answer is 4, and so on, until we have a value where t = 1 second, it becomes large quickly. For a day the number n1 became so large that it was unmanageable for modern RUBY, so I had to introduce a factor. and being a decimal starting with 1 and having no other digit seems to have worked perfectly, scaling according to the t value. I did this sort of process for all 64 cells of the 8x8 table. Then I woke up and read chapter 2. And immediately was presented with a real world application for insertion_sort. An algorithm that mimics taking a handful of cards, and aligning them in a sequential order such that the elements are from least to greatest or greatest to least. It was also the first time I had seen pseudo code and attempted to use it as a structure for RUBY code. I toyed around with it for ages. Before reading the book further, and had hit a dead end. All of my debugging techniques couldn't make sense of the garbled mess of the array my method had made. When I read further, it explained that i = j = e. And its use of A[1..j-1] was saying what would happen in the next loop. def insertion_sort(an_array)
size = an_array.length
i = 0
while i < size
current = an_array
j = i
while j > 0 and an_array[j-1] > current
an_array[j] = an_array[j-1]
j -= 1
end
an_array[j] = current
i += 1
end
return an_array
end def insertion_sort(an_array) size = an_array.length i = 0 while i < size current = an_array[i] j = i while j > 0 and an_array[j-1] > current an_array[j] = an_array[j-1] j -= 1 end an_array[j] = current i += 1 end return an_array end this produced this result: And what I had mentioned earlier, of this being so obvious now. I had made this function before when I was making Classical Age World. It was my sort method for arranging the NPCs in a 2d array into family trees with relationships. I know on my walmar machine it will bog down on 10K entries. It might be more efficient now with the latest RUBY. the time coefficient being defined as: * insertion sort – c1n2 Just from the table I had made, I know that N**2 is really not that great, but isn't the worse efficient coefficient algorithm. The worst on the table being, N! I went and did the method in GML, and it worked eventually. Now I can happily say I know an algorithm with it's application and relevance in programming. The book had me write an algorith to return Nil when a value is not in the array. To be honest I am not sure I understand the problem thorough. this was my guess at it Linear search: For j = 0 to A.length V = A[j] If A[j+1].defined? j = j + 1 else V = nil Break Anyways, I thought people would like to know about this particular journey as it develops.
Sign in to follow this
Followers
0


