Home python o-large for sort () / sorted () in python

# o-large for sort () / sorted () in python

Author

Date

Category

Studying Standard Functions and Python Methods A question arose about the implementation of Sort () and Sorted () in Python. How fast do they work and is there any sense to write an algorithm, for example, quick sorting, solving olympiad tasks, or save time, simply by writing ryarr.sort ()?

was done by experimentally so – created a randomic array of n elements,
0 & lt; MYARR [I] & LT; n.
With uncomplicated operations, these results were obtained, the time to create arrays was not taken into account:

``````sort ():
10.000 = 0.004 (i.e. an array of 10.000 email, each email equal {1, 10.000})
100.000 = 0.015
1.000.000 = 0.228.
10.000.000 = 3.445
Sorted ():
10.000 = 0.004
100.000 = 0.015
1.000.000 = 0.232
10.000.000 = 3.476
algorithm:
10.000 = 0.020
100.000 = 0.311
1.000.000 = 4.286.
10.000.000 = 62.884
``````

Algorithm for comparison with built-in functions was used here (quick sorting algorithm):

``````Def Quicksort:
IF LEN (Array) & LT; 2:
# Base Case, Arrays WITH 0 OR 1 ELEMENT ARE ALREADY "SORTED"
Return Array.
ELSE:
# RECURSIVE CASE
Pivot = Array 
# Sub-Array of All The Elements Less Than the Pivot
less = [i for i in array [1:] if i & lt; = pivot]
# Sub-Array of All The Elements Greater Than The Pivot
greater = [i for i in array [1:] if i & gt; Pivot]
Return Quicksort + [Pivot] + Quicksort (Greater)
``````

Of course, the experiment is non-ideal, every time a new array was created, this I am neglected. For many, if not for everyone, it is obvious that standard functions and methods work much faster, but nevertheless, at least now there are specific values ​​and the difference on the face.

Standard framework algorithms are designed to solve a large circle of tasks, that is, these are general-purpose algorithms that will help you in the general case and they most likely work for O (NLOGN). During the solution of the Olympiad tasks, you will Know the context of using the algorithm and in some cases you can apply more optimal sorting (for example, sorting counting or, bonnetic sorting), which can be faster than standard. Because if you see the ability to sort your data faster than O (NLOGN), then write your sorting, if not, it is not necessary to invent quickly sorting or sorting merge – use the standard one.

In response to what is more important – algorithm or compile …

That’s how I could and Nayal on Python – I hope it is quite a closer. 🙂

``````import numpy as np
From Timeit Import Default_Timer AS Timer
DEF QSORT (M, B, E):
IF B == E:
Return;
L = B;
R = E;
pivot = m [l];
While L & LT; = R:
While m [l] & lt; Pivot:
L = L + 1;
While M [R] & GT; Pivot:
R = R - 1;
IF L & LT; = R:
t = m [l];
M [l] = m [r];
M [r] = t;
L = L + 1;
R = R - 1;
IF B & LT; R:
qsort (m, b, r);
IF E & GT; L:
QSORT (M, L, E);
Return;
IDX = 100000.
M = np.random.randint (0, IDX, IDX)
Start = Timer ()
qsort (m, 0, idx-1);
END = TIMER ()
Print (End - Start)
#for i in Range (0, IDX):
# Print (M [i]);
``````

But – inserts on C++:

``````void insertionsort (vector & lt; int & gt; & amp; data)
{
Vector & lt; int & gt; :: iterator b = data.begin (), E = data.end (), i, j, k;
i = b;
For (++ i; i! = E; ++ i)
{
j = k = i;
For (- j; (k! = b) & amp; & amp; (* k & lt; * j); --j, --k)
{
swap (* k, * j);
}
}
}
``````

Results on my car (about several launches, in MS; measurement only sorting)

``````Quick_python Insertion_cpp
1000 4 0.4.
10000 51 38.
100000 635 4010.
1000000 7500 did not wait
``````

Conclusions Make yourself 🙂

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.