for each of the listed methods, make and test the changes needed, and list both
ID: 3770457 • Letter: F
Question
for each of the listed methods, make and test the changes needed, and list both the number of swaps and th number of compares needed by the Sorts program to sort an array of 50 random intergers. you must include one or more statements to increment your counter within the sorting methods themselves.
a) selectionSort swaps____________ compares _________________
b) bubbleSort swaps ____________ compares _________________
c) shortBubble swaps ____________ compares _________________
d) insertionSort swaps ____________ compares _________________
Explanation / Answer
The following programs can illustrate the Sorting methods with examples :
a)Program on selection sort
#include<stdio.h>
#include<conio.h>
#include<process.h>
int array[22],n,i,j,loc,min;
void main()
{
clrscr();
printf("enter the size of the array,");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf(" enter %d elements:",i);
scanf("%d",&array[i]);
}
printf(" before sorting ");
for(j=0;j<n;j++)
printf(" %d",array[j]);
for(i=0;i<n;i++)
{
loc=i;
min=array[i];
for(j=i+1;j<n;j++)
{
if(array[j]<min)
{
min=array[j];
loc=j;
}
}
array[loc]=array[i];
array[i]=min;
}
printf(" aftersort ");
for(j=0;j<n;j++)
printf(" %d",array[j]);
getch();
}
--------------------------------------------------
output:
enter the size of the array,5
enter 0 elements:11
enter 1 elements:88
enter 2 elements:77
enter 3 elements:66
enter 4 elements:55
before sorting 11 88 77 66 55
aftersort 11 55 66 77 88
b) Program on bubble sort
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a[22],i,j,temp,n;
clrscr();
printf("in enter the length of an array");
scanf("%d",&n);
printf("in enter %d elements",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf(" assending order list is");
for(i=0;i<n;i++)
{
printf(" %d",a[i]);
}
getch();
}
---------------------------------------
output:
enter the length of an array5
enter 5 elements 11 55 33 99 77
assending order list is 11 33 55 77 99
d) Program on Insertion sort
#include<stdio.h>
#include<conio.h>
#include<math.h>
int i,j,k,n;
void main()
{
int a[22];
clrscr();
printf(" enter the size of array: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf(" enter the a[%d] elements: ",i);
scanf("%d",&a[i]);
}
printf(" before sort:");
for(i=0;i<n;i++)
printf(" %d",a[i]);
for(j=0;j<n;j++)
{
k=a[j];
for(i=j-1;i>=0 && k<a[i];i--)
a[i+1]=a[i];
a[i+1]=k ;
printf(" pass %d element inserted in proper place %d ",j,k);
for(i=0;i<n;i++)
printf(" %d",a[i]);
}
printf(" sorted array:");
for(i=0;i<n;i++)
printf(" %d",a[i]);
getch();
}
--------------------------------------------
output:
enter the size of array:
3
enter the a[0] elements:
22
enter the a[1] elements:
11
enter the a[2] elements:
66
before sort: 22 11 66
pass 0 element inserted in proper place 22
22 11 66
pass 1 element inserted in proper place 11
11 22 66
pass 2 element inserted in proper place 66
11 22 66
sorted array: 11 22 66
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.