Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#include #include #include #include using namespace std; static long comparisons = 0 ; static long swaps = 0 ; void swap ( int * a
#include
#include
#include
#include
using namespace std;
static long comparisons ;
static long swaps ;
void swapint a int b
int temp a;
a b;
b temp;
swaps;
void selectionSortint first, int last
for int i first; i last ; i
int minIndex i;
for int j i ; j last; j
comparisons;
if j minIndex
minIndex j;
if minIndex i
swapi minIndex;
void insertionSortint first, int last
for int i first ; i last; i
int key i;
int j i ;
comparisons;
while j first && j key
j j;
j;
swaps;
j key;
Helper function for quicksort partitioning
int partitionint first, int last
int pivot first;
int i first ;
int j last ;
while i j
comparisons;
while i j && i pivot
i;
comparisons;
while i j && j pivot
j;
if i j
swapi j;
swapfirst j;
return j first;
void quickSortHelperint first, int last
if first last
int pivotIndex partitionfirst last;
quickSortHelperfirst first pivotIndex;
quickSortHelperfirst pivotIndex last;
void quickSortint first, int last
quickSortHelperfirst last;
int mainint argc, char argv
unchanged code
Uncomment for level and
cout "Comparisons: comparisons
;
cout "Swaps: swaps
;
delete data;
return ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started