First, choose the sort Select_sort
#Finds the smallest value in an arraydeffindsmallest (arr):#Stores the smallest valuesmallest =Arr[0]#Stores The index of the smallest valueSmallest_index =0 forIinchRange (1, Len (arr)):ifArr[i] <smallest:smallest=Arr[i] Smallest_index=IreturnSmallest_index#Sort ArraydefSelectionsort (arr): NewArr= [] forIinchRange (len (arr)):#Finds the smallest element in the array and adds it to the new arraysmallest =findsmallest (arr) newarr.append (Arr.pop (smallest))returnNEWARRPrintSelectionsort ([5, 3, 6, 2, 10])
First, quick sort Quick_sort
defquicksort (array):ifLen (array) < 2: #base case, arrays with 0 or 1 element is already "sorted" returnArrayElse: #recursive casePivot =Array[0]#Sub-array of the elements less than the pivotless = [i forIinchArray[1:]ifI <=Pivot]#Sub-array of all the elements greater than the pivotGreater = [I forIinchArray[1:]ifi >Pivot]returnQuicksort (less) + [pivot] +quicksort (Greater)PrintQuicksort ([10, 5, 2, 3])
Python implementation of the classical sorting algorithm