Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in C that uses two identical arrays of just eight intergers. It

ID: 3781925 • Letter: W

Question

Write a program in C that uses two identical arrays of just eight intergers. It should display the contents of the first array, then call a function to sort the array using an ascending order bubble sort modified to print out the array contents after each pass of the sort. Next, the program should display the contents of the second array, then call a funtion to sort the array using an ascending order selction sort modifier to print out the array contents after each pass of the sort.The code should be written in C.

Explanation / Answer

// C code

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void displayArray(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}

printf(" ");
}

void selection_sort(int a[], int n)
{
int i,j,min,id;
for(i=0;i<n-1; i++)
{
id=i;
min=a[i];
  
for(j=i+1;j<n;j++)
{
if(min>a[j])
{
id=j;
min=a[j];
}
}
  
int temp=a[i];
a[i]=a[id];
a[id]=temp;

printf("Array after iteration number %d: ",(i+1));
displayArray(a,n);
}   
}

void bubble_sort(int a[],int n)
{
int i,j;

for(i=0;i<n-1; i++)
{
for(j=0;j<n-1-i; j++)
{
if(a[j+1]<a[j])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
  
printf("Array after iteration number %d: ",(i+1));
displayArray(a,n);
}
}
  
int main()   
{
int array1[8],array2[8];
int i;
int size = 8;

for(i=0;i<size;i++)
{
printf("Enter element %d: ", (i+1));;
scanf("%d",&array1[i]);
array2[i]=array1[i];
}

printf(" Bubble Sort Implemantation ");
displayArray(array1,size);
bubble_sort(array1,size);

printf(" Selection sort Implementation ");
displayArray(array2,size);
selection_sort(array2,size);   
return 0;
}


/*
output:

Enter element 1: 34
Enter element 2: 21
Enter element 3: 11
Enter element 4: 45
Enter element 5: 66
Enter element 6: 43
Enter element 7: 23
Enter element 8: 19

Bubble Sort Implemantation
34 21 11 45 66 43 23 19
Array after iteration number 1: 21 11 34 45 43 23 19 66
Array after iteration number 2: 11 21 34 43 23 19 45 66
Array after iteration number 3: 11 21 34 23 19 43 45 66
Array after iteration number 4: 11 21 23 19 34 43 45 66
Array after iteration number 5: 11 21 19 23 34 43 45 66
Array after iteration number 6: 11 19 21 23 34 43 45 66
Array after iteration number 7: 11 19 21 23 34 43 45 66


Selection sort Implementation
34 21 11 45 66 43 23 19
Array after iteration number 1: 11 21 34 45 66 43 23 19
Array after iteration number 2: 11 19 34 45 66 43 23 21
Array after iteration number 3: 11 19 21 45 66 43 23 34
Array after iteration number 4: 11 19 21 23 66 43 45 34
Array after iteration number 5: 11 19 21 23 34 43 45 66
Array after iteration number 6: 11 19 21 23 34 43 45 66
Array after iteration number 7: 11 19 21 23 34 43 45 66

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote