Question

Write a program that creates three identical arrays,list1,list2, and list3, of 5000 elements. The program then...

Write a program that creates three identical arrays,list1,list2, and
list3, of 5000 elements. The program then sortslist1using bubble sort,
list2using selection sort, andlist3using insertion sort, and outputs
the number of comparisons and item assignments made by each sorting
algorithm.
Submit a screen shot testing with 20 numbers, 500 numbers, 1000 numbers. Show the output of the arrays of 20 so you can see they are in order. please use c++

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

//C++ code

#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;

void selectionSort(int a[],int n , int &comp , int &assign){
   int min;
   for(int i=0;i<n-1;i++){
       min=i;
       for(int j=i+1;j<n;j++){
           if(a[j]<a[min])min=j;
           comp++;
       }
       if(min!=i){
           int temp=a[i];
           a[i]=a[min];
           a[min]=temp;
           assign+=3;
       }
   }
}
void bubbleSort(int a[],int n, int &comp , int &assign){
   for(int i=n-1;i>=0;i--){
       for(int j=0;j<i;j++){
           if(a[j]>a[j+1]){
               int temp=a[j];
               a[j]=a[j+1];
               a[j+1]=temp;
               assign+=3;
           }
           comp++;
       }
   }
}

void insertionSort(int a[],int n, int &comp , int &assign){
   for(int i=1;i<n;i++){
       int j=i-1,val=a[i];
       assign++;
       while(j>=0 && a[j]>val){
           comp++;
           a[j+1]=a[j];
           assign++;
           j--;
       }
       a[++j]=val;
       assign++;
   }
}

int main(){
   int size = 5000 ;
   int num , comparison , assign;
   int list1[size] , list2[size],list3[size];
  
   srand(time(NULL));
  
   for(int i=0;i<size;i++){
       num = rand()%size;
       list1[i] = num;
       list2[i] = num;
       list3[i] = num;
   }
  
   comparison = 0;
   assign = 0;
   bubbleSort(list1 , size , comparison , assign);
   cout<<"Sorting List1 using Bubble sort\n";
   cout<<"Number of comparisons: "<<comparison<<endl;
   cout<<"Number of assignments: "<<assign<<endl;
  
   comparison = 0;
   assign = 0;
   selectionSort(list2 , size , comparison , assign);
   cout<<"\nSorting List2 using Selection sort\n";
   cout<<"Number of comparisons: "<<comparison<<endl;
   cout<<"Number of assignments: "<<assign<<endl;
  
   comparison = 0;
   assign = 0;
   insertionSort(list3 , size , comparison , assign);
   cout<<"\nSorting List3 using Insertion sort\n";
   cout<<"Number of comparisons: "<<comparison<<endl;
   cout<<"Number of assignments: "<<assign<<endl;
  
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write a program that creates three identical arrays,list1,list2, and list3, of 5000 elements. The program then...
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 program that creates three identical arrays, list1, list2, and list3, of 5000 elements. The...

    Write a program that creates three identical arrays, list1, list2, and list3, of 5000 elements. The program then sorts list1 using bubble sort, list2 using selection sort, and list3 using insertion sort and outputs the number of comparisons and item assignments made by each sorting algorithm Note: (Complete Programming Exercise #15 on page 1345. Submit a screen shot testing with 20 numbers, 500 numbers, 1,000 numbers. Show the output of the arrays of 20 so you can see they are...

  • sort

    Problem Statement 4Write a program that creates three identical arrays, list1, list2, and list3 of 5000 elements. The program then sorts list1 using bubble sort, list2 using selection sort, and list3 using insertion sort and outputs the number of comparisons and item assignments made by each sorting algorithm.

  • Need help with program. I'm stuck Objectives: In this assignment, we will practice manipulating lists of...

    Need help with program. I'm stuck Objectives: In this assignment, we will practice manipulating lists of data and arranging items in an ascending/descending order. you will also explore storing lists/arrays using different sorting algorithms including, the selection sort, bubble sort, and insertion sort algorithm. Comparison between the three algorithms are made based on the number of comparisons and item assignments (basic operations) each algorithms executes. Background: Ordering the elements of a list is a problem that occurs in many computer...

  • HOME WORK 9. Sorting Benchmarks Write a program that uses two identical arrays of at least...

    HOME WORK 9. Sorting Benchmarks Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should keep a count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other array. It should also keep count of the number of exchanges it...

  • Benchmark Searching and Sorting Write a program that has an array of at least 20 strings...

    Benchmark Searching and Sorting Write a program that has an array of at least 20 strings that you will have your user enter. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also keep...

  • 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 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...

  • Using C++ Create a program that performs the Bubble Sort, the Insertion Sort, and the Selection...

    Using C++ Create a program that performs the Bubble Sort, the Insertion Sort, and the Selection Sort with arrays of varying lengths. You will time each algorithm. The algorithms' time complexities should allow us to predict how these algorithms will perform. Program needs Four separate arrays, each with the same length and filled with the same sequence of randomly chosen numbers. Four separate functions, one for each of the four algorithms. All four functions will need to be timed. Be...

  • C++ Sorting and Searching 1. Mark the following statements as true or false. a. A sequential...

    C++ Sorting and Searching 1. Mark the following statements as true or false. a. A sequential search of a list assumes that the list elements are sorted in ascending order. b. A binary search of a list assumes that the list is sorted. 2. Consider the following list: 63 45 32 98 46 57 28 100 Using a sequential search, how many comparisons are required to determine whether the following items are in the list or not? (Recall that comparisons...

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
ADVERTISEMENT