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

Modify the following bubble sort program to sort 50 random integers using two-di

ID: 3634044 • Letter: M

Question

Modify the following bubble sort program to sort 50 random integers using two-directional bubbling(or shaker sorted)in each pass. The first bubbling the smallest integer is bubbled up and the second bubbling the largest integer is bubbled down. Program should have a main, random number generator, shaker or bubble sort, and display function.

#include <stdio.h>
#define MAX_ARY_SIZE 15

void bubbleSort(int list[ ], int last);

int main (void)
{
int i;
int ary[ MAX_ARY_SIZE ] = { 89, 72, 3, 15, 21,
57, 61, 44, 19, 98,
5, 77, 39, 59, 61 };


printf("Unsorted array: ");

for (i = 0; i < MAX_ARY_SIZE; i++)
printf("%3d", ary[ i ]) ;

bubbleSort (ary, MAX_ARY_SIZE - 1) ;

printf(" Sorted array: ");

for (i = 0; i < MAX_ARY_SIZE; i++)
printf("%3d", ary[ i ]);
printf(" ");

system("PAUSE");
return 0;
}

void bubbleSort (int list [], int last)
{
int temp, current, sorted, walker;

for ( current = 0, sorted = 0;
current <= last && !sorted;
current++)
for (walker = last, sorted = 1;
walker > current;
walker--)
if (list[ walker ] < list[ walker - 1 ])
// Any exchange means list is not sorted
{
sorted = 0;
temp = list[walker];
list[walker] = list[walker - 1];
list[walker - 1] = temp;
} // if
return;
}

Explanation / Answer

//Define array size

#define MAX_ARY_SIZE 50

//Define sort function
void bubbleSort(int list[],int last);

//main begins
int main()
{

//Declare variables
int i;
int ary[MAX_ARY_SIZE];

//Loop repeats 50 times to get 50 random numbers
for(i=0;i<50;i++)
ary[i]=rand()%100;

//Display the unsorted 50 numbers
printf("UnSorted Array :");
for(i=0;i<50;i++)
printf("%3d",ary[i]);

//call function bubbleSort
bubbleSort(ary,MAX_ARY_SIZE-1);

//Display the sorted 50 numbers
printf(" Sorted Array :");
for(i=0;i<50;i++)
printf("%3d",ary[i]);
printf(" ");
system("PAUSE");
return 0;
}//end main

//Function bubbleSort
void bubbleSort(int array[],int n)
{

//Declare variables
int j;
int st=-1;

//Loop repeats until the last number reach

while(st<n){

//update st and n

st++;
n--;

//sort from smallest to up
for(j=st;j<=n;j++){

if(array[j] > array[j+1]){
int t=array[j];
array[j]=array[j+1];
array[j+1]=t;
}
}

//sort from largest from bottom to up
for(j=n;--j>=st; ){
if(array[j]>array[j+1]){
int t=array[j];
array[j]=array[j+1];
array[j+1]=t;
}
}
}
}//end function

------------------------------------------------------------------------------------------------------------------------

Output:

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