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

Share This Post

Email
WhatsApp
Facebook
Twitter
LinkedIn
Pinterest
Reddit

Order a Similar Paper and get 15% Discount on your First Order

Related Questions

Consultation, Supervision, and Advocacy Plan

Description Introduction For this assignment, imagine you have been contacted by an agency in your community to provide consultation on how they might improve their services to clients. The agency is concerned that clients with serious counseling issues are “falling between the cracks” when they are referred to others in

Case: Nestle – Integrating Marketing Communication into Daily Operations With

Case: Nestle – Integrating Marketing  Communication into Daily Operations  With more than 2000 brands, from global icons to local favourites, and  present in 190 countries, Nestlé is one of the world’s largest food-and beverage companies. It operates in four different strategic business units:  beverage. milk and milk products, prepared dishes

Read the attached case study ” The Rise and Fall

  Read the attached case study ” The Rise and Fall of Blackberry” and answer the case study questions. Writing Requirements 2-3 pages in length  (excluding the cover page, abstract, and reference list) APA format, Use the APA template located in the Student Resource Center to complete the assignment.

Week 2 Compare and Contrast of System Development Approaches Assignment

Week 2 Compare and Contrast of System Development Approaches   Assignment Instructions: Read Chapter 6 of the Mastrian & McGonigle (2021) textbook. (See attcahed) After reflection, complete the provided worksheet. Describe the four different system development approaches discussed (Waterfall, Rapid prototyping or rapid application development (RAD), Object-oriented system development (OOSD),

In this discussion thread you will share one credible source

  In this discussion thread you will share one credible source (select one from your proposal project) and one unreliable source (this should NOT be in your proposal project).  Provide APA-style reference for both sources.  Then, under each reference write a short paragraph explaining why the source is credible or

Vital Signs. Hello, this is a Med Sur Lab assignment.

   Vital Signs.  Hello, this is a Med Sur Lab assignment. Please see the PowerPoint and complete the attachments. Please complete each attachment separately Check off skills on the lists  Check S (satisfactory) if you fully comprehend and can perform the skill. Check U (unsatisfactory) if you need improvement in

3. Research “free infographic maker for students” . a. Select

  3. Research “free infographic maker for students” . a.  Select one of the free tools listed in the articles, and create an infographic about  one of the following concepts from our readings: b.  Include pictures, graphs, and charts in the infographic, as well as written descriptions of the concept your chose

Como parte de esta tarea, define los términos listados a

Como parte de esta tarea, define los términos listados a continuación. Debes hacer este glosario antes de estudiar el material didáctico de la sección de Recursos.  Listado de términos: Medio bidimensionalMedio tridimensionalSoporte (de arte)VolumenMasaRepresentación (de arte)Cubismo (movimiento de arte visual))Dadaismo (movimiento de arte visual))Surrealismo (movimiento de arte visual)Arte conceptual Instrucciones

The Power Grid and Big Data (Clipart from MS Office)

  The Power Grid and Big Data  (Clipart from MS Office) The electric-utility business is going through a major business process reengineering (BPR), stemming from technology improvements, economic forces, and public awareness.  The electricity sector is transforming from large plants that produce hundreds or thousands of megawatts of power that

Extend Sabrina and Sable responses with additional examples about objectivity

Extend Sabrina and Sable responses with additional examples about objectivity in science. What ideas did your peers present that you had not considered before. Sabrina post I do not believe scientists and researchers can ever be fully objective during their work.  Each person comes in with a different viewpoint, different

The prostate is and important accessory gland. What is the

 The prostate is and important accessory gland. What is the prefix or suffix of the disease/disorder. Research one disorder, diseases, or illnesses of the prostate and explain.State the disease, disorder, or injury. What are the symptoms? How is it diagnosed? How does it affect the body? What does it look like? How is it

Facebook Breach Each week, you will be asked to respond

  Facebook Breach  Each week, you will be asked to respond to the prompt or prompts in the discussion forum. Your initial post should be 75-150 words in length, and is due on Sunday. By Tuesday, you should respond to two additional posts from your peers.  Using the information from:  Chapter 3 – the corrective justice

Save each chapter in a word document. CHAPTER 3 QUESTIONS

Save each chapter in a word document.  CHAPTER 3 QUESTIONS 1-Describe the benefits of VoIP in a service desk setting. 2-List four of the capabilities that ACD’s provide. 3-How is skills-based routing different from normal ACD routing? 4-A VRU integrates with another technology to do what? Provide one example. 5-How

One factor of business risk that is outside of the

One factor of business risk that is outside of the firm’s control is political risk. Political risk can range from insignificant to severe across different countries, but can also vary at different times. While firms cannot directly control political risk, understanding it is an important part of evaluating a company’s

Week 2 Term Paper Submission Submit a list of five

  Week 2 Term Paper Submission Submit a list of five to eight current references, in APA format, that will ultimately be incorporated into your Final Term Paper. Briefly describe the content of each reference and how it relates to your selected research topic. Submit your Week 2 list of

Demonstrate the ability to develop a holistic plan of care

Demonstrate the ability to develop a holistic plan of care incorporating Telehealth, defining assessment and intervention of specific populations incorporating unique attributes of populations for health promotion, wellness preservation and maintenance of function across the health-illness continuum.  After completing the readings, prepare a two page PDF flyer as a patient