pivot selection previouslyin the random selection algorithmwe selected the first element as the pivot we shall replace that step with sequence of steps that enables us to obtain the true or approximate median this will improve the partitioning of the list about the pivotdef partition(unsorted_arrayfirst_indexlast_index)if first_index =last_indexreturn first_index elsenearest_median median_of_medians(unsorted_array[first_index:last_index]index_of_nearest_median get_index_of_nearest_median(unsorted_arrayfirst_indexlast_indexnearest_medianswap(unsorted_arrayfirst_indexindex_of_nearest_medianpivot unsorted_array[first_indexpivot_index first_index index_of_last_element last_index less_than_pivot_index index_of_last_element greater_than_pivot_index first_index let' now study the code for the partition function the nearest_median variable stores the true or approximate median of given listdef partition(unsorted_arrayfirst_indexlast_index)if first_index =last_indexreturn first_index elsenearest_median median_of_medians(unsorted_array[first_index:last_index]if the unsorted_array parameter has only one elementfirst_index and last_index will be equal first_index is therefore returned anyway howeverif the list size is greater than onewe call the median_of_medians function with the section of the arraydemarcated by first_index and last_index the return value is yet again stored in nearest_median