# Declarative thinking

Hello wanderers. We, as travelers by our thoughts, and analyzers of our condition, must understand where it’s good, and where else, where exactly we are, I want to attract the attention of readers to this.

How do we add chains of thoughts, sequentially ?, assuming the output of each step, controlling the flow of control and the state of the cells in the memory? Or simply by describing the problem statement, indicate to the program which particular task is required to be solved, and this is sufficient for drawing up all the programs. Do not turn coding into a stream of commands that will change the internal state of the system, but express the principle as a concept of sorting, it’s not necessary to imagine what the algorithm is hiding there, just need to get sorted data. No wonder the president of America may mention the bubble, he expresses the idea that he understood something in programming. He just found out that there is a sorting algorithm, and the data in the table, on his desktop, cannot themselves line up in some magical way, in alphabetical order.

This idea, that I am a good attitude to the declarative way of expressing thoughts, and expressing everything by a sequence of commands and transitions between them, seems archaic and outdated to me, because our grandfathers, grandfathers connected wires on the switching panel and received flashing lights, and we have a monitor and voice recognition, as being at this level of evolution, you can still think about following commands It seems to me that if you express a program in a logical language, it will look clearer, and it can be done in technology, this was staked back in the 80s.

Well, the introduction was delayed
President of the United States 3r3119. After all, this is how he got these sacred data, he learned about the algorithms and put it up, for example, like this: 3r3202. In order to sort the list, you need to take a couple of elements, compare them with each other and if the first is more than the second, then you need to swap them, rearrange them, and then you need to repeat the search for pairs of such elements from the very beginning of the list until the permutations run out 3r3203. .

Yes, the principle of bubble sorting even sounds longer than the quick sort version, but the second advantage is not only in the brevity of the record, but also in its speed, the expression of the same quick sort, formulated by the algorithm, will it be faster than the version declaratively expressed? It may be necessary to change the views on teaching programming, it is necessary as the Japanese tried to introduce in schools the teaching of Prolog and the associated thinking. You can systematically move to the removal of algorithmic languages ​​of expression.

Sort 2

To play this, I had to turn to 3r377. literature 3r3119. This is a wording from
Hoare
I try to turn it into a python:

` def quicksort (A, lo, hi):if lo < hi:p = partition (A, lo, hi)quicksort (A, lo, p - 1)quicksort (A, p + ? hi)return Adef partition (A, lo, hi):pivot = A[lo]i = lo - 1j = hi + 1while truedo: i = i + 1while A[i]3r399. do: j = j - 1while A[j]> pivotif i> = j: return jA[i], A[j]= A[j], A[i] `

I admire the thought, I need an endless cycle, he would have put in a go-that)), there were jokers.

3r3111. Analysis

Now we will make a long list and make it sort by both methods, and we will understand how faster and more efficiently to express our thoughts. Which approach is easier to take?

Creating a list of random numbers as a separate 3r3118. Problem
, so it can be expressed:

` def qsort (S):if s ==[]: return[]H, T = S, S[1:]return qsort ([X for X in T if X=H])import randomdef test (len):list =[random.randint(-100, 100) for r in range(0,len)]from time import monotonicstart = monotonic ()slist = qsort (list)print ('qsort =' + str (monotonic () - start))## print (slist) `

Here are the measurements obtained:

` test (1?000)qsort = ???r3r3285. test (1?000)qsort = ???r3r3285. test (1?000)qsort = ???r3r3285. test (100000)qsort = ???r3r3285. test (100000)qsort = ???r3r3285. test (100000)qsort = ???r3r3285. `

Now I repeat this in the formulation of the algorithm:

` def quicksort (A, lo, hi):if lo < hi:p = partition (A, lo, hi)quicksort (A, lo, p)quicksort (A, p + ? hi)return Adef partition (A, lo, hi):pivot = A[lo]i = lo-1j = hi + 1while True:while True:i = i + 1if (A[i]> = pivot) or (i> = hi): breakwhile True:j = j-1if (A[j] <=pivot) or (j<=lo): breakif i> = j: return max (j, lo)A[i], A[j]= A[j], A[i]22 .2r2322322 .2r2r2332322 .2r232233 :List =[random.randint(-100, 100) for r in range(0,len)]From time import monotonicStart = monotonic ()Slist = quicksort (list, ? len-1)Print ('quicksort =' + str (monotonic () - start )) 3r3777. 3r3-33278. 3r3-33268.  It was necessary to work on the transformation of the original example of the algorithm from ancient sources in Wikipedia. It means this:you need to take the supporting element and position the elements in the sub-array so that the left is less and less, and the right is more and more. To do this, swap the left with the right element. We repeat this for each sub-list of the reference element divided by the index, if we have nothing to change, we end up with. Total Let's see what the time difference will be for the same list, which is sorted by two methods in turn. Conduct 100 experiments, and let us build a graph: 3r33263.  import randomdef test (len):t? t2 =[],[]for n in range (?100):list =[random.randint(-100, 100) for r in range(0,len)]list2 = list[:]from time import monotonicstart = monotonic ()slist = qsort (list)t1 + =[monotonic() - start]#print ('qsort =' + str (monotonic () - start))start = monotonic ()slist = quicksort (list?? len-1)t2 + =[monotonic() - start]#print ('quicksort =' + str (monotonic () - start))import matplotlib.pyplot as pltfig = plt.figure ()ax = fig.add_subplot (111)ax.plot (range (?100), t? label = 'qsort')ax.plot (range (?100), t? label = 'quicksort')ax.legend ()ax.grid (true)plt.show ()test (1?000) What you see here is that the quicksort () function worksfasterbut its recording is not so visual, although the function is recursive, but to understand the work of the permutations made in it is not at all easy. Well, what an expression of the idea of ​​sorting more thanconsciously? With a small difference in performance, we get such a difference in the amount and complexity of the code. Maybe the truth is enough to learn imperative languages, and what is more attractive for you? Ps. And here is the Prologue:  qsort ([],[]).qsort ([H|T], Res): - 3r-3285. findall (X, (member (X, T), X= H), L2),qsort (L? S1), qsort (L? S2), 3r-3285. append (S?[H|S2], Res). `
+ 0 -

• • • 