Page 1 of 1

Could you please help to implement the Python actual code for this Pseudocode for Merge Sort and Quick sort algorithms,

Posted: Thu Jun 02, 2022 8:20 am
by answerhappygod
Could you please help to implement the Python actual code for
this Pseudocode for Merge Sort and Quick sort algorithms, please.
Do not use dictionaries and inbuilt Python functions or libraries.
Thanks.

MergeSort – Algorithm
METHOD MergeSort IMPORT array, leftIdx, rightIdx EXPORT
array
IF (leftIdx < rightIdx) THEN
midIdx ← (leftIdx + rightIdx) / 2
MergeSort(array, leftIdx, midIdx) ← Recurse: Sort left half
of the current sub-array
MergeSort(array, midIdx+1, rightIdx) ← Recurse: Sort right
half of the current sub-array
Merge(array, leftIdx, midIdx, rightIdx) ← Merge the left and
right sub-arrays
//ELSE
// array is already sorted (only one element!) ← ie: Base
case
ENDIF
ENDMergeSort
METHOD Merge IMPORT array, leftIdx, midIdx, rightIdx EXPORT
array
tempArr ← allocate array of length (rightIdx – leftIdx + 1)
ii ← leftIdx ← Index for the ‘front’ of left sub-array
jj ← midIdx + 1 ← Index for the ‘front’ of right sub-array
kk = 0 ← Index for next free element in tempArr
WHILE (ii <= midIdx) AND (jj <= rightIdx) DO ← Merge
sub-arrays into tempArr
IF (array[ii] <= array[jj]) THEN ← Use <= for a stable
sort
tempArr[kk] ← array[ii] ← Take from left sub-array
ii ← ii + 1
ELSE
tempArr[kk] ← array[jj] ← Take from right sub-array
jj ← jj + 1
ENDIF
kk ← kk + 1
ENDWHILE
FOR ii ← ii TO midIdx DO ← Flush remainder from left
sub-array
tempArr[kk] ← array[ii] NOTE: Goes to midIdx
inclusively
kk ← kk + 1
ENDFOR
FOR jj ← jj TO rightIdx DO ← OR Flush remainder from right
sub-array
tempArr[kk] ← array[jj] NOTE: Goes to rightIdx
inclusively
kk ← kk + 1
ENDFOR
FOR kk ← leftIdx TO rightIdx DO ← Copy the now-sorted tempArr back
to the actual array
array[kk] ← tempArr[kk-leftIdx] ← Use kk-leftIdx to align
tempArr indexing to zero
ENDFOR
QuickSort – Algorithm
METHOD QuickSort IMPORT array, leftIdx, rightIdx EXPORT array
IF (rightIdx > leftIdx) THEN ← Check that the array is > one
element in size
pivotIdx ← (leftIdx+rightIdx) / 2 ← Pivot selection strategy:
middle element
newPivotIdx ← doPartitioning(array, leftIdx, rightIdx,
pivotIdx)
QuickSort(array, leftIdx, newPivotIdx-1) ← Recurse: Sort left
partition
QuickSort(array, newPivotIdx+1, rightIdx) ← Recurse: Sort
right partition
//ELSE
// Base case: array is 1 element or smaller in size – already
sorted
ENDIF
END
METHOD doPartitioning IMPORT array, leftIdx, rightIdx, pivIdx
EXPORT newPivIdx
pivotVal ← array[pivIdx]
array[pivIdx] ← array[rightIdx] ← Swap the pivotVal with the
right-most element
array[rightIdx] ← pivotVal
// Find all values that are smaller than the pivot
// and transfer them to the left-hand-side of the array
currIdx ← leftIdx
FOR (ii ← leftIdx TO rightIdx-1)
IF (array[ii] < pivotVal) THEN ← Find the next value that
should go on the left
temp ← array[ii] ← Put this value to the left-hand-side
array[ii] ← array[currIdx]
array[currIdx] ← temp
currIdx ← currIdx + 1
ENDIF
ENDFOR
newPivIdx ← currIdx
array[rightIdx] ← array[newPivIdx] ← Put the pivot into its
rightful place (the value at
array[newPivIdx] ← pivotVal
END.
Thanks