# Write a computer program that prompts the user for one number, n for

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]); } } }

#Write #computer #program #prompts #user #number

Pages (275 words)
Standard price: \$0.00

### Latest Reviews

Impressed with the sample above? Wait there is more

Related Questions

### Using your text complete the following. In these problems apply your knowledge of the rules and laws associated with Schedule

Using your text, complete the following. In these problems, apply your knowledge of the rules and laws associated with Schedule C.? Problem 37, on page

### BusinessLevel and CorporateLevel Strategies Overview In this assignment you are to use the same corporation you selected and focused on

Business-Level and Corporate-Level StrategiesOverviewIn this assignment, you are to use the same corporation you selected and focused on for the assignments, Strategic Management and Strategic

### Is there a difference between a manager and a leader?

Is there a difference between a manager and a leader? Can someone be a leader in an organization without being a “manager?” Do you

### SOCW 6200: Human Behavior and the Social Environment I Week

SOCW 6200: Human Behavior and the Social Environment I Week 8 Assignment: Connecting Knowledge and Research In order to provide the appropriate intervention, social

Research Paper: Civilization and Traditional Jihad Assignment Instructions Overview The purpose of this paper is to examine, evaluate and compare the concepts known as Civilization

### Week 1 Journal Prior to completing the journal this week,

Week 1 Journal Prior to completing the journal this week, read Chapter 1: Professional Applications of Psychology (INCLUDED IN ATTACHMENTS) in the course textbook, read the

### Each student will identify and select a leader that they

Each student will identify and select a leader that they have access to and that they would identify as a servant leader. The student will

### Overview When you are given a medical case, what is

Overview When you are given a medical case, what is the first thing that you do? You make an observation. Remember, the first step in

### Question: Explain the market failure approach to determining the role of the public sector. Explain how this approach can be

Question: Explain the “market failure” approach to determining the role of the public sector. Explain how this approach can be applied to some examples drawn

### Soil Cycling Soil is very important for nutrient cycling and

Soil Cycling Soil is very important for nutrient cycling and is an integral part of biogeochemical cycles.  For each of the biogeochemical cycles you

### Research and describe ‘Epigenetics’ in about 2-3 pages including at

Research and describe ‘Epigenetics’ in about 2-3 pages including at least one reference in any format. Try to answer the following questions in your description.

### Upon completion of the Required Readings, write a thorough, well-planned

Upon completion of the Required Readings, write a thorough, well-planned narrative answer to the following discussion question. Rely on your Required Readings and the

New questions

### 2 part case study….Can you help? Musculoskeletal Function: Nursing Assignment Help

2 part case study….Can you help? Musculoskeletal Function: G.J. is a 71-year-old overweight woman who presents to the Family Practice Clinic for the first time complaining

### Health & Medical Worksheet Nursing Assignment Help

Q1: AstraZeneca gives a 3% rebate to a PBM for placing Farxiga on Tier 2 (preferred branded) Farxiga WAC price = \$418.00; 30-day supply (1

### In a Word document, include your journal/note of the experim… Nursing Assignment Help

In a Word document, include your journal/note of the experiments/step that you did and the real world application for your bacteria. • Include the tables/

### CU Financial Data Analysis for Better Patient Outcomes Nursing Assignment Help

Read Case 16 (pages 97-101) from Gapenski’s Cases in Healthcare Finance – “Seattle Cancer Center.” Create a presentation in Microsoft PowerPoint (PPT), suitable for presentation

### ABC Nutrition Rules for Health Discussion Nursing Assignment Help

I’m working on a health & medical multi-part question and need the explanation and answer to help me learn. In order to support society and