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

Language and emotion are 2 primary means of communication. Disorders

Language and emotion are 2 primary means of communication. Disorders of language and of emotional expression and recognition can present significant difficulties for individuals in expressing themselves and in understanding others. Discuss the following: Discuss various language disorders and disorders of emotional expression. Include an explanation of specific symptoms and

Review a job description through a job website (e.g., Career

Link for the career I chose : Interim Sr. Treasury Consultant https://www.careerbuilder.com/jobs?posted=&pay=80&radius=30&emp=all&cb_apply=false&cb_workhome=false&keywords=international+banking&location=texas Identify at least five skills you have obtained through your general education courses that will make you successful at this job. Demonstrate with at least two examples how your newly acquired knowledge and skills have shaped both your

A 3,000-word analytical and critical review of a ‘project’ based

  A 3,000-word analytical and critical review of a ‘project’ based on the knowledge learned throughout MANG6311. For the review, you need to select and review a national ‘project’ from your own country (e.g. restructuring a major bank or an education system, mechanizing the agriculture system, or digitizing the health

Chatterjee (2014) “Knowledge management is the process of capturing, distributing,

Chatterjee (2014) “Knowledge management is the process of capturing, distributing, andeffectively using knowledge.” Thisformal definition may be simplyredefined as “management oflearned information asknowledge”, aimed at systemizingthe process of knowledgemanagement so that the knowledge processes can be organized into a structuralframework (Chatterjee, 2014). Furthermore, Vaccarezza & Rizzi (2014) an effective and

This is a PowerPoint presentation of 4-5 slides max. The

This is a PowerPoint presentation of 4-5 slides max. The information to be used for this work is attached. additional facts can be added to slide.  The title page and reference page do no count towards the 4-5 page count. The presentation is to talk about facts according to the

Financial issues – Premium Paper Help

Premium Paper Help is a professional writing service that provides original papers. Our products include academic papers of varying complexity and other personalized services, along with research materials for assistance purposes only. All the materials from our website should be used with proper references.

Read the assignment instructions for the Final. Next, develop a

 Read the assignment instructions for the Final. Next, develop a formal outline for your Final  writing in full sentences (12 point font, Times New Roman font, double spaced). The outline must contain an introduction section with thesis statement, a body section that identifies the five conflict topics that you will

In 250 words or more, answer the following, For the

In 250 words or more, answer the following, For the past several years employers have started to give a lot of weight to applicants who not only have a degree, but who also possess one or more IT certifications. Engage in research on the Internet and discuss whether you feel

Case Conceptualization and Treatment – Premium Paper Help

Premium Paper Help is a professional writing service that provides original papers. Our products include academic papers of varying complexity and other personalized services, along with research materials for assistance purposes only. All the materials from our website should be used with proper references.

HU Obesity Rising Prevalence in Developing Countries Nursing Assignment Help

I’m working on a health & medical writing question and need the explanation and answer to help me learn. What are the multifaceted factors contributing to the rising prevalence of obesity in developed and developing countries, and how can healthcare systems effectively address this global health crisis?   You are

Blake Glover’s recent job performance needs to be addressed. You

Blake Glover’s recent job performance needs to be addressed. You hired Blake over a year ago because he was highly qualified for the job, and in his first year, you and other managers were impressed by his excellent work and potential for leadership in the company. However, over the last

Macroeconomics of The Bank of India and The Federal Reserve

II. Central Banking: Explain the current macroeconomic conditions (unemployment, inflation, and economic growth [GDP] rates) that the Federal Reserve and the central bank you have chosen (The Bank of India) observe in each country, and compare the performance of the two countries. III. Monetary Policy A. Explain the primary tools

Global health initiatives are often pivotal in the advancement of

  Global health initiatives are often pivotal in the advancement of human rights. When addressing global public health issues, international organizations have a responsibility to develop policies and practices that support marginalized groups’ basic human rights. Policies that affect a woman’s reproductive and sexual health can be contradictory of the

BSB50420 Diploma of Leadership and Management Assessment Type Assessment -02

 BSB50420 Diploma of Leadership and ManagementAssessment Type Assessment -02 (Project) Time allowedDue Date Location AHIC Term / YearUnit of CompetencyNational Code/Title BSBPEF501 Manage Personal and Professional DevelopmentStudent DetailsStudent Name Student IDStudent Declaration: I declare that the work submitted ismy own, and has not been copied or plagiarised from anyperson or

Determine the relevancy of the following acts today: The Norris-La

Determine the relevancy of the following acts today: The Norris-La Guardia Act. The National Labor Relations (Wagner) Act. The Labor Management Relations (Taft-Hartley) Act. Provide three specific examples to support your position on the current relevancy of these acts. The following article provides more context for the relevancy of these

4 DQ 2 Organization culture is about shared values, beliefs,

 4 DQ 2 Organization culture is about shared values, beliefs, and behaviors within an organization. Every organization has its own unique culture that workers are expected to adhere to. Nurse leaders play a great role in advocating for the welfare of patients through collaboration with the top-level management. In some

In her chapter from What Terrorists Want, Louise Richardson speaks

In her chapter from What Terrorists Want, Louise Richardson speaks of her experience growing up as a sympathizer of the Irish Republican Army. Because of her experience growing up during a time of conflict in Northern Ireland, she writes that “when I hear of a terrorist atrocity, I do not think of

Combating terrorism through addressing radicalization and violent extremism has become

 Combating terrorism through addressing radicalization and violent extremism has become a ubiquitous feature of national strategies, resulting in the emergence of many policies and practices directed toward countering and preventing violent extremism. These soft-power approaches aim at intervention before violence occurs. One prominent theme in the literature is that violent