Question

Write a complete C++ program that analyzes the following sorts by keeping track of how long...

Write a complete C++ program that analyzes the following sorts by keeping track of how long they take to sort arrays of longS of size 5000, 10000, and 100000, as well as the number of swaps and comparisons they take in doing so:

Insertion sort

Selection sort

Quick sort

shell sort(using gaps of n/2, n/4, n/8, ..., 1) for 20, the gaps would be 10, 5, 2, and 1

shell sort(using gaps based on 2^k + 1, but starting at 1) for 20, the gaps would be 17, 9, 5, 3, and 1

Specifications:

ask the user for the size of the array to be sorted and validate that number accordingly

your gap arrays will not need to hold more than 20 elements

when generating random numbers for the arrays, just use rand(); do not use %

the only things that should be printed are the times, swaps, and comparisons for each sort

use the same numbers for each sort. To do this, keep a master array and then copy the values from the master array to the array to be sorted before you run each sort (do not have more than two large arrays)

in order to keep track of the numbers of swaps and comparisons, you will need to do the following: declare variables of type long long (2 words) in the main that will store the swaps and comparisons, pass these variables as reference parameters to each of your sort functions, and increment these variables when appropiate within your sort functions

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution=> Please do comment in case of any query.

1. Insertion Sort:- Please do read all the comments in the Program.

Output:-

Proram:-

#include<iostream>
#include <stdio.h>
#include<stdlib.h>
using namespace std;
int no_swap;
int comp;
//used for swapping the elements
void swap(int *x,int *y)
{
  
int temp=*x;
*x=*y;
*y=temp;
}

//this function sort the array using Insertion Sort Algorithm
void Insertion(long array[],int n)
{
long temp;
int j;
for (int i = 1; i < n; i++) {
j = i;
comp++;
while ((j > 0) && (array[j - 1] > array[j])) {
if(array[j-1]>array[j]){
comp++;
}
temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
j--;

no_swap++;//increment swap variable when actually swap is done
}}
}
int main()
{
int n;
cout << "Enter the number of elements: "<<endl;
cin >> n;
long A[n]; //create an array with given number of elements

for(int i = 0; i<n; i++) {
A[i]=rand();
}

cout<<"Array Before Sorting"<<endl;
for(int i=0;i<n;i++)
printf("%d ",A[i]);
printf("\n");

Insertion(A,n);

cout<<"Array After Sorting"<<endl;
for(int i=0;i<n;i++)
printf("%d ",A[i]);
printf("\n");
cout<<"Total No. of comparisions :"<<comp<<endl;
cout<<"Total No. of swaps :"<<no_swap<<endl;
return
0;
}

Selection sort:-  Please make sure to read all the comments in the program.

Program:-

#include<iostream>
#include <stdio.h>
#include<stdlib.h>
using namespace std;
int no_swap;
int comp;
//used for swapping the elements
void swap(long *x,long *y)
{
  
long temp=*x;
*x=*y;
*y=temp;
}

//this function sort the array using Selection Sort Algorithm
void SelectionSort(long A[],int n)
{
int i,j,k;

for(i=0;i<n-1;i++)
{
for(j=k=i;j<n;j++)
{
comp++;
if(A[j]<A[k])
k=j;
}
swap(&A[i],&A[k]);
no_swap++;
}
}

int main()
{
int n;
cout << "Enter the number of elements: "<<endl;
cin >> n;
long A[n]; //create an array with given number of elements

for(int i = 0; i<n; i++) {
A[i]=rand();
}

cout<<"Array Before Sorting"<<endl;
for(int i=0;i<n;i++)
printf("%d ",A[i]);
printf("\n");

SelectionSort(A,n);

cout<<"Array After Sorting"<<endl;
for(int i=0;i<n;i++)
printf("%d ",A[i]);
printf("\n");
cout<<"Total No. of comparisions :"<<comp<<endl;
cout<<"Total No. of swaps :"<<no_swap<<endl;
return
0;
}

Quick Sort:-  Please read all the comments in the program.

Program:-

#include<iostream>
#include <stdio.h>
#include<stdlib.h>
using namespace std;
int no_swap;
int comp;
//used for swapping the elements
void swap(long *x,long *y)
{
  
long temp=*x;
*x=*y;
*y=temp;
}
//Parition function of quicksort
int partition(long A[],int l,int h)
{
int pivot=A[l];
int i=l,j=h;

do
{

do{i++; comp++;}while(A[i]<=pivot);

do{j--; comp++;}while(A[j]>pivot);

if(i<j){
swap(&A[i],&A[j]);
no_swap++;
}}while(i<j);

swap(&A[l],&A[j]);
no_swap++;
return j;
}

//Funtion for sorting the array using quicksort
void QuickSort(long A[],int l,int h)
{
int j;

if(l<h)
{
j=partition(A,l,h);
QuickSort(A,l,j);
QuickSort(A,j+1,h);
}
}
int main()
{
int n;
cout << "Enter the number of elements: "<<endl;
cin >> n;
long A[n]; //create an array with given number of elements

for(int i = 0; i<n; i++) {
A[i]=rand();
}

cout<<"Array Before Sorting"<<endl;
for(int i=0;i<n;i++)
printf("%d ",A[i]);
printf("\n");

QuickSort(A,0,n);

cout<<"Array After Sorting"<<endl;
for(int i=0;i<n;i++)
printf("%d ",A[i]);
printf("\n");
cout<<"Total No. of comparisions :"<<comp<<endl;
cout<<"Total No. of swaps :"<<no_swap<<endl;
return
0;
}

Shell Sort :- Please make sure to read all the comments in the program.

Program:-

#include<iostream>
#include <stdio.h>
#include<stdlib.h>
using namespace std;
int no_swap;
int comp;

// Sorting of array using Shell sort
void ShellSort(long A[],int n)
{
int gap,i,j,temp;

for(gap=n/2;gap>=1;gap/=2)
{
for(i=gap;i<n;i++)
{
temp=A[i];
j=i-gap;

while(j>=0 && A[j]>temp)
{comp++;
A[j+gap]=A[j];

j=j-gap;
}
A[j+gap]=temp;
no_swap++;
comp++;

}
}

}

int main()
{
int n;
cout << "Enter the number of elements: "<<endl;
cin >> n;
long A[n]; //create an array with given number of elements

for(int i = 0; i<n; i++) {
A[i]=rand();
}

cout<<"Array Before Sorting"<<endl;
for(int i=0;i<n;i++)
printf("%d ",A[i]);
printf("\n");

ShellSort(A,n);

cout<<"Array After Sorting"<<endl;
for(int i=0;i<n;i++)
printf("%d ",A[i]);
printf("\n");
cout<<"Total No. of comparisions :"<<comp<<endl;
cout<<"Total No. of swaps :"<<no_swap<<endl;
return
0;
}

Hope you will like the answer.

Thanks.

Add a comment
Know the answer?
Add Answer to:
Write a complete C++ program that analyzes the following sorts by keeping track of how long...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays....

    Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...

  • Write a C++ program that does the following : Accepts a positive integer ( n )...

    Write a C++ program that does the following : Accepts a positive integer ( n ) from the keyboard . Create an character array of size n. Using a random number generator, populate the array with characters between 33 – 126. Create 7 individual functions and perform the following 1. In the first function: display elements of the array. Display the first 20 elements If the size is > 20 2. In the second function : Using recursion, Search for...

  • 2. Write a Java program to implement Bucket Sort and write a driver to test it....

    2. Write a Java program to implement Bucket Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 3. In this assignment you will investigate three different variants of algorithms that we have discussed in the class to solve the selection problem. Version 1. “Simultaneous minimum and maximum” selection algorithm. Version 2. Randomized selection algorithm. Version 3. “Median of...

  • Problem: Write a C++ program that does the following : Accepts a positive integer ( n...

    Problem: Write a C++ program that does the following : Accepts a positive integer ( n ) from the keyboard . Create an character array of size n. Using a random number generator, populate the array with characters between 33 – 126. Create 7 individual functions and perform the following In the first function: display elements of the array. Display the first 20 elements If the size is > 20 In the second function : Using recursion, Search for a...

  • Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and...

    Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and Insertion Sort. The numbers to be sorted will be obtained using a library function which generates pseudo-random numbers. TO Do 1. Fill an array with 140 real numbers between 0.00 and 945.00. Generate the numbers using the subroutine that generates random numbers. Make a spare copy of the array; you will need it later. 2. Call a subroutine to print the contents of the...

  • 1. Write a Java program to implement Counting Sort and write a driver to test it....

    1. Write a Java program to implement Counting Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 2. Write a Java program to implement Bucket Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 3. In...

  • Write a C++ program that asks user number of students in a class and their names....

    Write a C++ program that asks user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’ names and ranking. Follow the Steps Below Save the project as A4_StudentRanking_yourname....

  • **C++ only, use standard library, no vectors Write a program to generate a list of 5000...

    **C++ only, use standard library, no vectors Write a program to generate a list of 5000 random numbers between 1 and 10,000 stored in an array. Sorts: Print out the middle 50 numbers of the original list to show the results. Now sort the original numbers using bubble sort. Print out the number of swaps made. Now sort the original numbers using selection sort. Print out the number of swaps made. Now sort the original numbers using insertion sort. Print...

  • Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative perfo...

    Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets. Need Help With this Sorting Algorithm task for C++ Base Code for sorting.cpp is given. The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...

  • Write a computer program that prompts the user for one number, n for the number of...

    Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 different arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times. In the body of the loop, Create an array of n random integers (Make sure...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT