Question
can you help me fix this code? #include int insertionSort(int arr[], int size, int* ncompares_ptr, int* nswaps_ptr); int selectionSort(int arr[], int size, int* ncompares_ptr, int*
can you help me fix this code?
#include
int insertionSort(int arr[], int size, int* ncompares_ptr, int* nswaps_ptr);
int selectionSort(int arr[], int size, int* ncompares_ptr, int* nswaps_ptr);
int bubbleSort(int arr[], int size, int* ncompares_ptr, int* nswaps_ptr);
int main(){
int arr1[5] = {1,2,3,4,5};
int arr2[5] = {5,4,3,2,1};
int arr3[5] = {2,1,4,3,5};
int compare=0;
int swap =0;
printf("BubbleSort ");
bubbleSort(arr1,5,&compare,&swap);
printf("compare : %d\tswap : %d ",compare,swap);
compare=0;
swap=0;
bubbleSort(arr2,5,&compare,&swap);
printf("compare : %d\tswap : %d ",compare,swap);
compare=0;
swap=0;
bubbleSort(arr3,5,&compare,&swap);
printf("compare : %d\tswap : %d ",compare,swap);
int arr4[5] = {1,2,3,4,5};
int arr5[5] = {5,4,3,2,1};
int arr6[5] = {2,1,4,3,5};
compare=0;
swap =0;
printf("InsertionSort ");
insertionSort(arr4,5,&compare,&swap);
printf("compare : %d\tswap : %d ",compare,swap);
compare=0;
swap=0;
insertionSort(arr5,5,&compare,&swap);
printf("compare : %d\tswap : %d ",compare,swap);
compare=0;
swap=0;
insertionSort(arr6,5,&compare,&swap);
printf("compare : %d\tswap : %d ",compare,swap);
int arr7[5] = {1,2,3,4,5};
int arr8[5] = {5,4,3,2,1};
int arr9[5] = {2,1,4,3,5};
compare=0;
swap =0;
printf(" SelectionSort ");
bubbleSort(arr7,5,&compare,&swap);
printf("compare : %d\tswap : %d ",compare,swap);
compare=0;
swap=0;
selectionSort(arr8,5,&compare,&swap);
printf("compare : %d\tswap : %d ",compare,swap);
compare=0;
swap=0;
selectionSort(arr9,5,&compare,&swap);
printf("compare : %d\tswap : %d ",compare,swap);
}
int bubbleSort(int a[], int n, int* ncompares_ptr, int* nswaps_ptr){
int i,j;
for( i=n-1;i>=0;i--){
for( j=0;j
(*ncompares_ptr)++;
if(a[j]>a[j+1]){
(*nswaps_ptr)++;
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
int insertionSort(int a[], int n, int* ncompares_ptr, int* nswaps_ptr){
int i;
for(i=1;i int j=i-1,val=a[i]; while(j>=0&&a[j]>val) { (*ncompares_ptr)++; (*nswaps_ptr)++; a[j+1]=a[j]; j--; } a[++j]=val; } } int selectionSort(int a[], int n , int* ncompares_ptr, int* nswaps_ptr){ int min,i,j;
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