Question

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 science contexts for example, in a telephone directory it is necessary to alphabetize the names of subscribers. Similarly, creating a dictionary requires words be put in alphabetical order. While there are multiple sorting algorithms, some are better than others depending on the information being sorted. One thing in common which all sorting algorithms have is the sorting parameter. This “function” is what decides if two items in a list needs to switch places. While this is easy for the standard data types (int, char, double, …) since the compiler already knows how to compare these, it might be tricky for data types created by you. Structs and classes you create will require implementing comparison functions if you are trying to sort them. For example to compare two dates you can write something like:

bool compDate(Date d1, Date d2){

                  if (d1.year < d2.year

                        return true;

                  else if (d1.year == d2.year && d1.month < d2.month)

                        return true;

                  else if (d1.year == d2.year && d1.month == d2.month && d1.day < d2.day)

                        return true;

                  else

                        return false;

            }

The selection sort, bubble sort, and insertion sort algorithms are the most widely used algorithms. The section sort repeatedly picks the smallest element to append to the result. The insertion sort algorithm splits the list into two portions (sorted and unsorted) and repeatedly adds new element to the sorted portion of the list. The bubble sort algorithm repeatedly compares neighbor pairs and swap if needed.

Problem Description:

Write a C++ program to create 2 identical arrays, list1, and list2 – each list of 5000 elements. The program then sorts list1 using bubble sort, list2 using selection sort and outputs the number of comparisons and item assignments made by each sorting algorithm.

Hint: You need to create a random array and then copy it to end up with two identical lists (List1, List2). These two lists must be identical. You will need to sort List1 using bubble sort, and sort the second list List2 using the selection sort algorithm. Finally, you will need to output the number of comparisons and item assignments made by each sorting algorithm. Please note that the array should be randomly generated and you can do that by using the random function: rand() from the <cstdlib> library.

Sample:

IZACS250\A8_lab\bin\Debug\A8_lab.exe Number of comparisons--- Bubble sort: 12497500 Selection sort: 12497500 Insertion sort:

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

Source Code:

#include<iostream>
#include<cstdlib>
using namespace std;
int* bubblesort(int list[]);
int* selectionsort(int list[]);
int main()
{
   int n, i,list1[5000],list2[5000],r;
   for(i=0; i<5000; i++)
   {
   r = (rand() % 10) + 1;
       list1[i]=r;
       list2[i]=r;
   }
   int *bubble=bubblesort(list1);
   int *selection=selectionsort(list2);
   cout<<"Total comparisions: "<<endl;
   cout<<"Bubble sort= "<<bubble[0]<<endl;
   cout<<"selection sort= "<<selection[0];
   cout<<"\nTotal Assignment: "<<endl;
   cout<<"Bubble sort= "<<bubble[1]<<endl;
   cout<<"selection sort= "<<selection[1]<<endl;
   return 0;
}
int* bubblesort(int arr[])
{
   int i,j,comparision=0,assignments=0,temp;
   static int bubble[10];
   for(i=0; i<5000-1; i++)
   {
       for(j=0; j<(5000-i-1); j++)
       {
           if(arr[j]>arr[j+1])
           {
               temp=arr[j];
               arr[j]=arr[j+1];
               arr[j+1]=temp;
               comparision++;   //as there are singel comparision
               assignments+=3;   //as there are 3 time assign ments
           }
       }
   }
   bubble[0]=comparision;
   bubble[1]=assignments;
   return bubble;
}
int* selectionsort (int arr[])
{
   int i, j,comparision=0,assignments=0,min,loc,temp;
   static int selection[10];
   for(i=0;i<5000-1;i++)
{
min=arr[i];      
loc=i;
for(j=i+1;j<5000;j++)
{
if(min>arr[j])
{
min=arr[j];
loc=j;
comparision++;       //as there are singel comparision
assignments+=2;       //as there are 2 time assignments
}
}
temp=arr[i];
arr[i]=arr[loc];
arr[loc]=temp;
assignments+=5;   //as there are 5 time assignments in the for loop
}
selection[0]=comparision;
selection[1]=assignments;
return selection;
}  

Output:

Add a comment
Know the answer?
Add Answer to:
Need help with program. I'm stuck Objectives: In this assignment, we will practice manipulating lists of...
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
  • 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.

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

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

  • Puodace a char showing the number of moves required to solve the Towers of Hanoi puzle using t 30...

    Puodace a char showing the number of moves required to solve the Towers of Hanoi puzle using t 30. What is an execution frame? What is an activation record? What is contained in it? 31. Write a recursive Java method that computes the factorial of a number 2. Linear search may require up to comparisons while binary search will only require roughly comparisons 33. Sort sorts a list of values by repetitively putting a particular value into its final, sorted,...

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

  • In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program...

    In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program (name it SimpleSort) to implement and test the three sort algorithms (Bubble, Insertion, Selection) discussed in the lecture slides. Define method BubbleSort() to implement Bubble sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array. Define method InsertionSort() to implement insertion sort of an array of integers. Modify the algorithm implementation to count...

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

  • Java We will look at the behavior of three different sorts on randomly generated lists of...

    Java We will look at the behavior of three different sorts on randomly generated lists of different sizes The three sorts are bubblesort, insertion sort, and quicksort. Randomly generate integers in the range 0-99 for your random numbers. You may choose your own random number generation technique (this is an exception to the no-outside-help requirement for this assignment. You must document the source of your random number generation in the code. ° Here is what your code should do: 1....

  • Java We did in lecture, and explain your answer briefly. Problem 4: Sorting practice 14 points;...

    Java We did in lecture, and explain your answer briefly. Problem 4: Sorting practice 14 points; 2 points for each part individual-only Important: When answering these questions, make sure to apply the versions of these algorithms that were discussed in lecture. The Java code for each algorithm can be found in our Sort class. Given the following array: {14, 7, 27, 13, 24, 20, 10, 33 1. If the array were sorted using selection sort, what would the array look...

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