Question

In: Statistics and Probability

Module 7 showed that one way of comparing different algorithms for accomplishing the same task is...

Module 7 showed that one way of comparing different algorithms for accomplishing the same task is complexity analysis. You will recall that in complexity analysis we express the time an algorithm takes to run as a function of the size of the input, and we used the big-Oh notation. For example, if an algorithm has a complexity of O(1), then it always runs in the same amount of time, no matter what the size of the input is; if it O(n), then the time it takes for the algorithm to run is proportional to the size of the input.

However, complexity analysis has a number of limitations. For example, big-Oh analysis concerns the worst case scenario. For example, some sorting algorithms with a complexity of O(n^2) often run considerably faster if the list that it receives as input is (almost) sorted; other sorting algorithms with a complexity of O(n^2) always take the same amount of time, no matter what state the list is in. Also, in big-Oh, we look at the dominant term in our calculation of the complexity of the algorithm. Thus, when we analyze an algorithm and discover that it runs in 75,312 + n time units, we still say that it has a complexity of O(n). It is therefore deemed to be better than an algorithm that runs in .007 + n^2 time units, as this algorithm has a complexity of O(n^2). We also saw the rationale behind this: If n becomes sufficiently large, the other factors become insignificant.

Fortunately, there is another way to determine how long it takes for an algorithm to run, namely timing experiments. In a timing experiment, you actually implement the algorithm in a programming language, such as Java or C++, and simply measure how long it takes for the algorithm to run.

In the term project for this course, you are going to conduct a timing experiment and compare the results with the results you would get from a complexity analysis. We will compare Bubble Sort with Selection Sort.

In its least sophisticated form, bubble sort (http://en.wikipedia.org/wiki/Bubble_sort) works as follows:

Assuming that the list contains n elements.

Compare the first and the second element in the list, and swap them if the last element is smaller than the preceding one; otherwise, do nothing to this pair.

Now, compare the second and third elements and swap them if the first of them is larger than the second; otherwise, do nothing to this pair.

Move on the next pair and continue the process until you reach the end of the list.

A little reflection will show that at the end of this iteration, the last element in the list is now the largest element in the list. The last element has bubbled to the top.

Now repeat the process but rather than going to the end of the list, stop when you reach n-1.

Now repeat the process again, but rather than going to the end of the list, stop when you reach n-2.

Keep repeating this until you reach 1.

The Wikipedia entry has a little simulation that shows how bubble sort works. The code looks something like:

bubbleSort(array A){
   n = length(A);

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

swap obviously swaps the elements and can be defined as:

swap(A, pos1, pos2) {
     temp = A[pos1];
     A[pos1] = A[pos2];
     A[pos2] = temp;
}

Another sort is selection sort (http://en.wikipedia.org/wiki/Selection_sort). We saw selection sort in the question in the sub-module on how to determine the complexity of an algorithm. Array A contains n elements, the elements to be sorted. The algorithm starts at the first position in the array and looks through the array for the smallest element. Once it reaches the end of the array, it puts that element in the first cell of the array. It then restarts the whole process from the second position in the array, and continues until the entire array has been sorted.

selection_sort(array A) {
    int i,j
    int iMin;
    for(j = 0; j < n; j++){
       iMin = j;

   for ( i = j+1; i < n; i++) {

       if (a[i] < a[iMin]) {

           iMin = i;

       }

   }

   if ( iMin != j ) {

       swap(a[j], a[iMin]);

   }

} } If you like the Hungarian dancers, they perform selection sort at http://www.youtube.com/watch?v=Ns4TPTC8whw

THE PROJECT

The purpose of the project is to perform a timing experiment. You are required to complete the following activities:

Write a computer program that prompts the user for a number, creates an array for that number of random integers, and then usees the bubble sort to order the array. The program should print out the array prior to the call to the sorting algorithm and afterwards. You can write the program in either Java, C++, C#, or whatever language you are most comfortable in. Do Not use an API from the language library. Write the program to perform the sort.

Repeat 1 but use selection sort this time. Again, write out the program for the selection sort. DO not use the language library.

1 and 2 are primarily intended to make sure that your algorithms work.

Once you are convinced your programs work, do the following

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 you make the range of the random numbers substantially bigger than the array, ie. if the array size is 500 have the random number generator pick numbers between 1 and 5000. For the largest array have the random number generator pick numbers between 1 and 50,000).

Get the time and set this to start-time (notice the sort is started after each array is built. You want to time the srt process only). You will have to figure out what the appropriate command is in the programming language you are using to find the time (Important: Do not start the timer until after the array is created).

Use bubble sort to sort the array

Get the time and set this to end-time Subtract start-time from end-time and add the result to total_time

Once the program has run, note

The number of items sorted

The average running time for each array (total_time/1000)

Repeat the process using 500, 2500 and 5000 as the size of the array.

Repeat 3 using selection sort.

You now have 6 data points ( the averages from the three array sizes for the two sort algorithms) Create a spreadsheet showing the results of 3 and 4 and create a graph to graphically represent the information. Show both sort algorithms on the same graph for comparison.

Write a one page document explaining the results, bearing in mind that both algorithms have a complexity of O(n^2) and what you know about complexity analysis. Use your knowledge of complexity analysis to explain your results.

Please submit

Program code for 1

Program code for 2

Program code used in 3 and the results of the three program runs

Program code used in 4 and the results of the three program runs

The spreadsheet created in 5

The report. In the report, say which machine you ran the experiments on (type of processor, RAM, etc). Explain your experiment and why you feel you received the results you observed.

Solutions

Expert Solution

ANSWER:-

SAMPLE OUTPUT:-

COPY CODE:-


#include<bits/stdc++.h>
using namespace std;
using namespace std::chrono; //To calculate time in microseconds.


//Bubble Sort algorithm
//Program code for 1.
void bubbleSort(int A[], int n){
for(int j = n; j > 0; j--){
for(int i = 1; i < j; i++) {
if(A[i-1] > A[i]){
//Swap A[i-1] and A[i]
int temp = A[i-1];
A[i-1] = A[i];
A[i] = temp;
}
}
}
}

//Get the index containing minimum element.
int getMinIndex(int arr[], int start, int end){
int minIndex = start;
for(int i=start;i<end;i++){
if(arr[i] < arr[minIndex])
minIndex = i;
}
return minIndex;
}

//Selection Sort algorithm.
//Program code for 2.
void selectionSort(int A[], int n){
for(int i=0;i<n;i++){
int minIndex = getMinIndex(A, i, n);
int temp = A[i];
A[i] = A[minIndex];
A[minIndex] = temp;
}
}

void printArray(int arr[], int n){
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
cout<<endl;
}

int main(){
int n;
cout<<"Enter the size of array: ";
cin>>n;
int arr[n];

double t = 0;

//Program code for 3.
//Calculate the avg running time for bubble sort for 3 different cases.
for(int j=0;j<1000;j++){
srand(time(0));
if(n <= 500){
for(int i=0;i<n;i++)
arr[i] = 1 + (rand()%5000);
}else{
for(int i=0;i<n;i++)
arr[i] = 1 + (rand()%50000);
}

//cout<<"The unsorted array is:\n";
//printArray(arr, n);

//Used to calculate running time.
auto start = high_resolution_clock::now();
bubbleSort(arr, n);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
//cout << "Time taken by function: " << duration.count() << " microseconds" << endl;
t += duration.count();
}

double avg1 = t/1000;
cout<<"Avg time for bubble Sort: "<<avg1<<" microseconds\n";

//cout<<"The sorted array is:\n";
//printArray(arr, n);

t = 0;

//Program code for 4.
//Calculate the avg running time for selection sort for 3 different cases.
for(int j=0;j<1000;j++){
srand(time(0));
if(n <= 500){
for(int i=0;i<n;i++)
arr[i] = 1 + (rand()%5000);
}else{
for(int i=0;i<n;i++)
arr[i] = 1 + (rand()%50000);
}

//Uncomment below to see the array.
//cout<<"The unsorted array is:\n";
//printArray(arr, n);

auto start = high_resolution_clock::now();
selectionSort(arr, n);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
//cout << "Time taken by function: " << duration.count() << " microseconds" << endl;
t += duration.count();
}

double avg2 = t/1000;
cout<<"Avg time for selection Sort: "<<avg2<<" microseconds\n";

//cout<<"The sorted array is:\n";
//printArray(arr, n);

return 0;

}


Related Solutions

Task 8.2 - Individual report. * Instructions: According to the instructions in module one, present an...
Task 8.2 - Individual report. * Instructions: According to the instructions in module one, present an individual report on the assigned topic (choose one of the 10 presented). The report will include aspects such as: definition of the topic, Historical background, advantages and disadvantages and your arguments on the contribution that you consider most significant and impactful for business administration. Present examples of corporations currently applying this method successfully. The report must be written using the essay type writing format...
One-way ANOVA is a procedure for comparing the means of several populations. It is the generalization...
One-way ANOVA is a procedure for comparing the means of several populations. It is the generalization of what procedure for comparing the means of two populations? A. nonpooled t-procedure B. pooled t-procedure C. paired t-test D. None of the above In One-way ANOVA, identify the statistics (SSTR, MSTR, SSE, MSE or F) used (a) as a measure of variation among the sample means (b) as a measure of variation within the samples (c) to compare the variation among the sample...
In the same module/file, write a function processAnimals that takes one argument, the name of an...
In the same module/file, write a function processAnimals that takes one argument, the name of an input file.   The function returns the list of animals created or an empty list if the file didn't contain any lines. The input file contains zero or more lines with the format: species, language, age where species is a string representing an animal's species, language is a string representing an animal's language, and age is an integer representing an animal's age. The items on...
When comparing 4 different brands failure rates for the same product, what would be the appropriate...
When comparing 4 different brands failure rates for the same product, what would be the appropriate model to use for a confidence interval and hypothesis test? My data looks like this, Brand 1: N=281, p-hat= .95805 pq=.04195 Brand 2: N=118 p-hat= .90799 pq=.09201 Brand 3: N=61 p-hat= .86767 pq= .13233 Brand 4: N=63 p-hat= .89112 pq= .10888 These are simple pass/fail trials. I am not sure what direction to go from here, I know how to compare 2 proportions I...
How are autopolyploids different from allopolyploids? Describe one way autopolyploids can be formed and one way...
How are autopolyploids different from allopolyploids? Describe one way autopolyploids can be formed and one way allopolyploids can be formed.
Module 4 ·         Select a developmental age group. ·         Describe one way the RN’s physical assessment...
Module 4 ·         Select a developmental age group. ·         Describe one way the RN’s physical assessment is unique for the developmental age you selected ·         Discuss a health promotion focus specific for the developmental age selected Base your initial post on your readings and research of this topic. Your initial post must contain a minimum of 250 words. References, citations, and repeating the question do not count towards the 250 word minimum.
A researcher conducts a one-way ANOVA comparing satisfaction scores for three groups of patients: in OSF,...
A researcher conducts a one-way ANOVA comparing satisfaction scores for three groups of patients: in OSF, in BroMenn, an in a nursing home. Possible scores on the satisfaction scale range from 20 to 80 points. Descriptive statistics for the three groups are presented below OSF             BroMenn        Nursing Home      X-bar=50.3    X-bar=41.4       X-bar=39.5 SD= 23.2       SD=26.8            SD=28.2 a) Suppose that the results of ANOVA came out to be significant and one of the questions the researcher was specifically interested in answering was...
why do you suppose people write one way and speak to people a different way?
why do you suppose people write one way and speak to people a different way?
1. Given that the end of point of apoptosis is the same, describe one way you...
1. Given that the end of point of apoptosis is the same, describe one way you would expect all of these forms of cell to be the same. (Dead cells is NOT a sufficient answer). Confused on this question, because one of the earlier question said that some forms of apoptosis do NOT require the activation of the caspases. So i'm not unsure of exactly do they have in common? Is it just that they're revolved around caspase whether it's...
There is no one perfect public relations process, or model, to use the same way all...
There is no one perfect public relations process, or model, to use the same way all the time with every organization. Discuss why or why not?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT