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 =0
swaps =0
def reset_counters():
global comparisons, swaps
comparisons =0
swaps =0
def swap(arr, i, j):
global swaps
arr[i], arr[j]= arr[j], arr[i]
swaps +=1
def selection_sort(arr):
reset_counters()
n = len(arr)
for i in range(n -1):
min_index = i
for j in range(i +1, n):
global comparisons
comparisons +=1
if arr[j]< arr[min_index]:
min_index = j
swap(arr, i, min_index)
return arr
def insertion_sort(arr):
reset_counters()
n = len(arr)
for i in range(1, n):
key = arr[i]
j = i -1
while j >=0 and arr[j]> key:
global comparisons
comparisons +=1
arr[j +1]= arr[j]
j -=1
global swaps
swaps +=1
arr[j +1]= key
return arr
def heapify(arr, n, i):
largest = i
l =2* i +1
r =2* i +2
if l < n and arr[l]> arr[largest]:
largest = l
if r < n and arr[r]> arr[largest]:
largest = r
if largest != i:
swap(arr, i, largest)
heapify(arr, n, largest)
def heap_sort(arr):
reset_counters()
n = len(arr)
for i in range(n //2-1,-1,-1):
heapify(arr, n, i)
for i in range(n -1,0,-1):
swap(arr, i,0)
heapify(arr, i,0)
return arr
def merge_sort(arr):
reset_counters()
if len(arr)>1:
mid = len(arr)//2
L = arr[:mid]
R = arr[mid:]
merge_sort(L)
merge_sort(R)
i = j = k =0
while i < len(L) and j < len(R):
global comparisons
comparisons +=1
if L[i]< R[j]:
arr[k]= L[i]
i +=1
else:
arr[k]= R[j]
j +=1
k +=1
while i < len(L):
arr[k]= L[i]
i +=1
k +=1
while j < len(R):
arr[k]= R[j]
j +=1
k +=1
return arr
def partition(arr, low, high):
global comparisons
pivot = arr[high]
i = low -1
for j in range(low, high):
if arr[j]< pivot:
i +=1
swap(arr, i, j)
comparisons +=1
swap(arr, i +1, high)
return i +1
def quick_sort(arr, low, high):
reset_counters()
if low < high:
pi = partition(arr, low, high)
quick_sort(arr, low, pi -1)
quick_sort(arr, pi +1, high)
return arr
def generate_random_array(size):
return [random.randint(1,1000) for _ in range(size)]
def generate_almost_sorted_array(size):
arr =[i for i in range(size)]
for i in range(size //10):
idx1= random.randint(0, size -1)
idx2= random.randint(0, size -1)
swap(arr, idx1, idx2)
return arr
def generate_almost_reversed_array(size):
arr =[i for i in range(size,0,-1)]
for i in range(size //10):
idx1= random.randint(0, size -1)
idx2= random.randint(0, size -1)
swap(arr, idx1, idx2)
return arr
def generate_sorted_except_last_10_percent(size):
arr =[i for i in range(size)]
for i in range(size - size //10, size):
idx1= random.randint(0, size -1)
idx2= random.randint(0, size -1)
swap(arr, idx1, idx2)
return arr
def test_sorting_algorithms(array_generator, size):
algorithms ={
"Selection Sort": selection_sort,
"Insertion Sort": insertion_sort,
"Heap Sort": heap_sort,
"Merge Sort": merge_sort,
"Quick Sort": lambda arr: quick_sort(arr,0, len(arr)-1)
}
for name, algorithm in algorithms.items():
arr = array_generator(size)
sorted_arr = algorithm(arr.copy())
print(f"{name} on {size} elements with {array_generator.__name__}: Comparisons: {comparisons}, Swaps: {swaps}")
def main():
print("Comparing sorting algorithms on different datasets:")
print("Random Arrays:")
test_sorting_algorithms(generate_random_array, 128)
test_sorting_algorithms(generate_random_array, 1024)
print("
Almost Sorted Arrays:")
test_sorting_algorithms(generate_almost_sorted_array, 128)
test_sorting_algorithms(generate_almost_sorted_array, 1024)
print("
Almost Reversed Arrays:")
test_sorting_algorithms(generate_almost_reversed_array, 128)
test_sorting_algorithms(generate_almost_reversed_array, 1024)
print("
Sorted Except Last 10% Arrays:")
test_sorting_algorithms(generate_sorted_except_last_10_percent, 128)
test_sorting_algorithms(generate_sorted_except_last_10_percent, 1024)
if __name__=="__main__":
main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!