Question: please convert this code in c + + import random # Global counters for comparisons and swaps comparisons = 0 swaps = 0 def reset
please convert this code in c
import random
# Global counters for comparisons and swaps
comparisons
swaps
def resetcounters:
global comparisons, swaps
comparisons
swaps
def swaparr i j:
global swaps
arri arrj arrj arri
swaps
def selectionsortarr:
resetcounters
n lenarr
for i in rangen :
minindex i
for j in rangei n:
global comparisons
comparisons
if arrj arrminindex:
minindex j
swaparr i minindex
return arr
def insertionsortarr:
resetcounters
n lenarr
for i in range n:
key arri
j i
while j and arrj key:
global comparisons
comparisons
arrj arrj
j
global swaps
swaps
arrj key
return arr
def heapifyarr n i:
largest i
l i
r i
if l n and arrl arrlargest:
largest l
if r n and arrr arrlargest:
largest r
if largest i:
swaparr i largest
heapifyarr n largest
def heapsortarr:
resetcounters
n lenarr
for i in rangen :
heapifyarr n i
for i in rangen :
swaparr i
heapifyarr i
return arr
def mergesortarr:
resetcounters
if lenarr:
mid lenarr
L arr:mid
R arrmid:
mergesortL
mergesortR
i j k
while i lenL and j lenR:
global comparisons
comparisons
if Li Rj:
arrk Li
i
else:
arrk Rj
j
k
while i lenL:
arrk Li
i
k
while j lenR:
arrk Rj
j
k
return arr
def partitionarr low, high:
global comparisons
pivot arrhigh
i low
for j in rangelow high:
if arrj pivot:
i
swaparr i j
comparisons
swaparr i high
return i
def quicksortarr low, high:
resetcounters
if low high:
pi partitionarr low, high
quicksortarr low, pi
quicksortarr pi high
return arr
def generaterandomarraysize:
return randomrandint for in rangesize
def generatealmostsortedarraysize:
arr i for i in rangesize
for i in rangesize :
idx random.randint size
idx random.randint size
swaparr idx idx
return arr
def generatealmostreversedarraysize:
arr i for i in rangesize
for i in rangesize :
idx random.randint size
idx random.randint size
swaparr idx idx
return arr
def generatesortedexceptlastpercentsize:
arr i for i in rangesize
for i in rangesize size size:
idx random.randint size
idx random.randint size
swaparr idx idx
return arr
def testsortingalgorithmsarraygenerator, size:
algorithms
"Selection Sort": selectionsort,
"Insertion Sort": insertionsort,
"Heap Sort": heapsort,
"Merge Sort": mergesort,
"Quick Sort": lambda arr: quicksortarr lenarr
for name, algorithm in algorithms.items:
arr arraygeneratorsize
sortedarr algorithmarrcopy
printfname on size elements with arraygenerator.name: Comparisons: comparisons Swaps: swaps
def main:
printComparing sorting algorithms on different datasets:"
printRandom Arrays:"
testsortingalgorithmsgeneraterandomarray,
testsortingalgorithmsgeneraterandomarray,
print
Almost Sorted Arrays:"
testsortingalgorithmsgeneratealmostsortedarray,
testsortingalgorithmsgeneratealmostsortedarray,
print
Almost Reversed Arrays:"
testsortingalgorithmsgeneratealmostreversedarray,
testsortingalgorithmsgeneratealmostreversedarray,
print
Sorted Except Last Arrays:"
testsortingalgorithmsgeneratesortedexceptlastpercent,
testsortingalgorithmsgeneratesortedexceptlastpercent,
if namemain:
main
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
