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

Create an integer array of size of 10. Randomly populate the array with integer

ID: 3697037 • Letter: C

Question

Create an integer array of size of 10. Randomly populate the array with integer from 1 to 100 into the array. Apply Selection Sort to the unsorted array. Create a counter to count the number of iteration needed to complete the sort of of the array. Display the result of the array after the sort, and the counter. Apply Merge Sort to the unsorted array. Create a counter to count the number of iteration needed to complete the sort of of the array. Display the result of the array after the sort, and the counter.

Currently there are 2 similar posts like this, but 1 just has pictures of the results and the 2nd post does not work when using the same code.

Explanation / Answer

Program.c:

#include <stdio.h>
#include <stdlib.h>
/* Function to merge the two haves arr[l..m] and arr[m+1..r] of array arr[] */
int merge(int arr[], int l, int m, int r,int c);

/* l is for left index and r is right index of the sub-array
of arr to be sorted */
int mergeSort(int arr[], int l, int r,int c)
{
   c++;
if (l < r)
{
int m = l+(r-l)/2; //Same as (l+r)/2 but avoids overflow for large l & h
c=mergeSort(arr, l, m,c);
c=mergeSort(arr, m+1, r,c);
c=merge(arr, l, m, r,c);
}
return c;
}

/* Function to merge the two haves arr[l..m] and arr[m+1..r] of array arr[] */
int merge(int arr[], int l, int m, int r,int c)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;

/* create temp arrays */
int L[n1], R[n2];

/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++)
{
   L[i] = arr[l + i];
   }

for (j = 0; j < n2; j++)
{
   R[j] = arr[m + 1+ j];

   }

/* Merge the temp arrays back into arr[l..r]*/
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2)
{
   c++;
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}

/* Copy the remaining elements of L[], if there are any */
while (i < n1)
{
   c++;
arr[k] = L[i];
i++;
k++;
}

/* Copy the remaining elements of R[], if there are any */
while (j < n2)
{
   c++;
arr[k] = R[j];
j++;
k++;
}
return c;
}


int main() {
int c,i,d, a[10],ar[10],position,swap ;

printf("Ten random numbers in [1,100] ");

for (i = 1; i <= 10; i++) {
a[i] = rand() % 100 + 1;
ar[i]=a[i];
  
}
for ( i = 0 ; i < 10 ; i++ )
printf("%d ", a[i]);
//selection sort
c=0;
for ( i = 0 ; i < 10 ; i++ )
{
position = i;
c++;

for ( d = i + 1 ; d < 10-1 ; d++ )
{
   c++;
if ( ar[position] > ar[d] )
position = d;
}
if ( position != i )
{
swap = ar[i];
ar[i] = ar[position];
ar[position] = swap;
}
}

printf("Sorted list in ascending order using selection sort:%d ",c);

for ( i = 0 ; i < 10 ; i++ )
printf("%d ", ar[i]);
c=0;
//merge sort
c=mergeSort(a, 0, 10-1,c);
  
printf("Sorted list in ascending order using Marge sort:%d ",c);

for ( i = 0 ; i < 10 ; i++ )
printf("%d ", a[i]);

return 0;
}

output:

Ten random numbers in [1,100]                                                                                                 

1                                                                                                                             

84                                                                                                                            

87                                                                                                                            

78                                                                                                                            

16                                                                                                                            

94                                                                                                                            

36                                                                                                                            

87                                                                                                                            

93                                                                                                                            

50                                                                                                                            

Sorted list in ascending order using selection sort:46

1                                                                                                                             

16                                                                                                                            

36                                                                                                                            

78                                                                                                                            

84                                                                                                                            

87

87                                                                                                                            

93                                                                                                                            

94                                                                                                                            

50                                                                                                                            

Sorted list in ascending order using Marge sort:53                                                                            

1                                                                                                                             

16                                                                                                                            

36                                                                                                                            

50                                                                                                                            

78                                                                                                                            

84                                                                                                                            

87                                                                                                                            

87                                                                                                                            

93                                                                                                                            

94                                                                                                                            

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