Today we'll talk about some of the classic sorting algorithms, and then use Python to do it, and finally compare the time of several algorithms with data
Select sort
Select sort (Selection sort) is a simple and intuitive sorting algorithm. It works by selecting the smallest (or largest) element of the data element to be sorted each time, storing it at the beginning of the sequence until all the data elements to be sorted are exhausted. Select Sort is an unstable sort method (for example, sequence [5, 5, 3] swaps the first [5] with [3] for the first time, causing the first 5 to move back to the second 5). (Note: Selected from Baidu Encyclopedia)
If there is a sequence a={6,3,1,9,2,5,8,7,4}, the process of selecting a sort should be as follows:
First trip: Select the smallest element and place it in the first position of the array a[0], a[0]=6 and A[2]=1 will be exchanged, at this time a={1,3,6,9,2,5,8,7,4};
Second trip: Because the a[0] position is already the smallest element, so this time from a[1], in the remaining sequence, select a minimum element to exchange it with a[1]. That is, the selection process found the smallest element a[4]=2, and then exchanged with the a[1]=3, at this time a={1,2,6,9,3,5,8,7,4};
Third trip: Because A[0], a[1] has been ordered, so in a[2]~a[8] and then select a minimum element and a[2] to exchange, and then the process has been circulating until all the elements in A sequence so far. This is the essence of the choice of sorting. Therefore, it is easy to write the core code part of the selection sequence, that is, the process of selection, the process of constant comparison and exchange.
The entire selection process is as follows:
Algorithm implementation:
" " Select Sort " " def Selection_sort (a): for in range (Len (a)-1): = i to in range (i + 1, Len (a)): if a[min] > a[j]: = J if min! = I := a[i ], A[min] return A
Insert Sort
There is an ordered sequence of data that requires inserting a number in the already-sorted data sequence, but the data sequence is still ordered after insertion, and a new sorting method is used-insert sort, the basic operation of inserting a sort is inserting a data into the ordered data that is already sorted, In order to obtain a new, number plus one ordered data, the algorithm is suitable for the ordering of a small amount of data, the time complexity of O (n^2). is a stable sorting method. The insertion algorithm divides the array to be sorted into two parts: the first part contains all the elements of the array, except for the last element (where an array has more space to insert), and the second part contains only that element (that is, the element to be inserted). After the first part is sorted, the last element is inserted into the first part of the sequence.
Algorithm implementation:
" " Insert Sort " " def Insertion_sort (a): for in range (1, Len (a)): = i and a[j-1]> a[i]: -= 1 a.insert (j,a[i]) a.pop (i+1 )return A
Quick Sort
Introduced:
Quick Sort (Quicksort) is an improvement to the bubbling sort. On average, sort n items to 0(n log n) comparisons. In the worst case scenario, a 0(n2) comparison is required, but this is not a common situation. In fact, fast sequencing is usually significantly faster than the other 0(n log n) algorithms because its internal loop (inner Loop) can be implemented efficiently on most architectures, and in most real-world data, The choice of design can be determined by reducing the likelihood of two of the time required.
Steps:
- Pick an element from the sequence called "Datum" (pivot),
- Reorder the columns, where all elements are placed in front of the datum in a smaller position than the base value, and all elements are larger than the base value behind the datum (the same number can be on either side). After the partition exits, the datum is in the middle of the sequence. This is called partition (partition) operation.
- recursively (recursive) sorts sub-columns that are smaller than the base value elements and sub-columns that are larger than the base value elements.
Sort effect:
Algorithm implementation:
" "Quick Sort" "defSub_sort (Array,low,high): Key=Array[low] whileLow <High : whileLow < High andArray[high] >=Key:high-= 1 whileLow < High andArray[high] <Key:array[low]=Array[high] Low+ = 1Array[high]=Array[low] Array[low]=Keyreturn LowdefQuick_sort (array,low,high):ifLow <High:key_index=Sub_sort (Array,low,high) quick_sort (array,low,key_index) quick_sort (Array,key_index+1,high)
Then we do a time comparison to the above algorithm:
" "' randomly generate a value between 0~10000000" "defgetrandata (num): a=[] I=0 whilei<num:a.append (random.randint (0,10000000)) I+=1returna" "randomly generate 20 arrays of length 10000" "a= [] forIinchRange (20): A.append (Getrandata (10000)) " "Test Time function" "deftime_it (f,a): t= [] forIinchRange (len (a)): T1=Time.time () f (a[i]) T2=time.time () t.append (T2-T1)returnTTT1=Time_it (Selection_sort,copy.deepcopy (a)) Tt2=Time_it (Dubble_sort,copy.deepcopy (a)) Tt3=Time_it (Insertion_sort,copy.deepcopy (a)) Tt4=Time_it (Quick_sort,copy.deepcopy (a))PrintNp.mean (TT1), Tt1PrintNp.mean (TT2), Tt2PrintNp.mean (TT3), Tt3PrintNp.mean (TT4), TT4
The results of the operation on my computer are as follows:
3.53101536036 [3.1737868785858154, 3.2235000133514404, 3.296314001083374, 3.3746020793914795, 3.6942098140716553, 3.6844170093536377, 3.3293440341949463, 3.6262528896331787, 3.577023983001709, 3.4677979946136475, 3.5323100090026855, 3.3574531078338623, 3.4525561332702637, 3.4662599563598633, 3.8701679706573486, 3.719839096069336, 3.7798891067504883, 3.6078600883483887, 3.803921937942505, 3.582801103591919]7.22842954397 [7.1081390380859375, 7.365921974182129, 6.666350841522217, 7.258342027664185, 6.8559088706970215, 6.805047035217285, 7.230466842651367, 7.948682069778442, 7.901662111282349, 7.448035955429077, 8.134574890136719, 7.731559991836548, 7.3559558391571045, 6.80467414855957, 7.227035999298096, 6.987092018127441, 6.9657158851623535, 6.997059106826782, 6.9417431354522705, 6.834623098373413]2.64084545374 [2.6150870323181152, 2.567375898361206, 2.7965359687805176, 2.616096019744873, 2.561455011367798, 2.76595401763916, 2.603566884994507, 2.542672872543335, 2.783787965774536, 2.643486976623535, 2.5308570861816406, 2.764592170715332, 2.6237199306488037, 2.508751153945923, 2.766709089279175, 2.603114128112793, 2.528198003768921, 2.812356948852539, 2.651564836502075, 2.53102707862854]0.0346855401993 [0.03456306457519531, 0.0337519645690918, 0.03569507598876953, 0.034551143646240234, 0.03506588935852051, 0.03456687927246094, 0.0352480411529541, 0.03591203689575195, 0.03380298614501953, 0.03402996063232422, 0.034184932708740234, 0.03434491157531738, 0.034864187240600586, 0.035611867904663086, 0.03429007530212402, 0.034512996673583984, 0.03453683853149414, 0.03406691551208496, 0.033792972564697266, 0.036318063735961914
Classic sorting algorithm and Python implementation