Need help? We are here

# 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

### Consider the following three scenarios

Consider the following three scenarios: Scenario 1: Ms. Teel brings in her 7-month-old infant for evaluation. She is afraid that the baby might have respiratory

### This is so Alfred Hitchcock or the Twilight Zone. You

This is so Alfred Hitchcock or the Twilight Zone.  You may have YouTubed some of these that were decidedly before your time.   Go to YouTube some time and see a few.  Tightly wound.

### PLEASE READ ENTIRELY Preparation Suppose you have been hired by a small group of partners who are in

PLEASE READ ENTIRELYPreparationSuppose you have been hired by a small group of partners who are in the process of starting a new business. The business

### Create your video presentation for the board members clearly explaining how the visual communication elements on each of the two promotional documents strategically convey the message and meaning to viewers.

Competency This project allows you to demonstrate your mastery of visual communication concepts discussed in this course. Scenario You are newly appointed to the communication

### How would you respond to someone who presents you with

How would you respond to someone who presents you with the arguments proposed by Social Darwinists, OR the stories written by Horatio Alger, OR Carnegie’s

### Informative Speech Outline Overview Strong oral communication skills will be valuable to you throughout your academic journey and are in

Informative Speech OutlineOverviewStrong oral communication skills will be valuable to you throughout your academic journey and are in high demand by many employers.Creating an outline

### Throughout the history of modern terrorism, government officials, counterterrorism professionals,

Throughout the history of modern terrorism, government officials, counterterrorism professionals, and academic scholars have sought to understand the root motives and ideological causes, mechanisms,

### Audi Motor company: Size, sector and type of business

Research notes outlining research undertaken using a variety of independent sources of information to prepare the presentation. The notes should provide evidence of the research

### DUE TODAY BEFORE MIDNIGHT The jury in this case was death qualified which is a requirement in all

DUE TODAY BEFORE MIDNIGHT!!!!The jury in this case was death qualified which is a requirement in all capital cases. Psychologists have conducted considerable research on

### Why Doctors should not be held accountable for preforming abortions

Research a social problem that might affect your career. Argue for a policy that your organization should adopt that will help ensure that people’s careers

### I need these assignments in about 150 words each. 1. Well as we consider Humanistic Psychology HP we as scientists

I need these assignments in about 150 words each.1. Well, as we consider Humanistic Psychology (HP) we, as scientists, must always consider how we evaluate

### One of the initial stages in developing a business plan

One of the initial stages in developing a business plan is to perform a market analysis. A market analysis is the process of gathering information

New questions

### Epidemiological Data Influences Changes in Health Practices Nursing Assignment Help

I’m trying to learn for my Health & Medical class and I’m stuck. Can you help? Discussion Question 1: Describe how epidemiological data influences changes

### Innovation and Customer Experience Essay Nursing Assignment Help

Please respond to the following: Diffusion of Innovation & Customer Experience Management Per the text, health care consumers vary in their willingness to adopt new

### MHA 543 UP Health & Medical Generational Work Trait Nursing Assignment Help

Work traits vary considerably between various generations. Each individual has a unique manner for interacting with others and solving problems or addressing issues that arise.

### Health & Medical Health Screening Tests Issues and Concerns Nursing Assignment Help

Describe what you already know about the module’s topics (if anything) With your limited understanding, how you would answer the module’s guiding question: “Do you

### Few people have not heard about the Food and Drug Nursing Assignment Help

Few people have not heard about the Food and Drug Administration (FDA). The COVID pandemic has placed a renewed focus on its role in assuring safety of