(Answered) 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.

0
(0)

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 ( 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 ( 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 #number #items #array #sort #create #sort #arrays #size #timing #run #average #time #sort #array #size

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Share This Post

Email
WhatsApp
Facebook
Twitter
LinkedIn
Pinterest
Reddit

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

Related Questions

(Answered) Task 2: Argumentative Research Paper- Organic foods verses GMOs

0 (0) Writing a research paper gives you an opportunity to explore a topic of special interest, research that topic, and organize your research findings in writing for an academic audience.   Through your preparation work, you have established an argumentative thesis statement and have planned a clear organization of

(Answered) M3A1: Citizens United

0 (0) (copy and paste the link) Review Campaign Finance: Abuses and Reforms. [Video File] [48 min 25 sec] before you participate in this activity. Parts of the video will discuss aspects of campaign finance and reform. Before engaging in this activity, please return to the first discussion topic on

(Answered) The decision in Williams v Roffey Bros signals that the courts in deciding whether or not to enforce a promise, may be guided less by technical questions of consideration than by questions of fairness, reasonableness and commercial utility.

0 (0) The decision in Williams v Roffey Bros signals that the courts in deciding whether or not to enforce a promise, may be guided less by technical questions of consideration than by questions of fairness, reasonableness and commercial utility.   The assignment will be in accordance with the following

(Answered) What are the effects of Ketamine treatment on treatment resistant depression?

0 (0) • Introductory statement clearly describes the phenomenon and main health issue • Clearly identifies the problem and gives an argument for examining the problem • Thesis statement is clear and well-articulated • Clearly articulates the particular population with clinical setting provided 1. Operational Definitions • Operational definitions of

(Answered) Management and Organizational Behavior.

0 (0) Description Exercise 17: Read Annual Pay Raises (W-68-69). This is located in the color-coded workbook section in the back of your book. ( Uhl-Bien, M., Osborn, R. N., & Schermerhorn, J. R. (2014).) Organizational Behavior. Hoboken, NJ: Wiley. ISBN:9781118517376 Review the exercise and complete steps 1 and 2.

(Answered) The Wall of Silence Reflective Journal

0 (0)  Assignment Instructions: For this course, you will be reading The Wall of Silence by Gibson and Singh (2003). Please read the book and reflect upon what you have read, and complete the assignment as described. The purpose of this reflective assignment is to provide the approach for connecting

(Answered) HEPATITIS C USING APRI SCORE

0 (0) Determination of Human Subjects Research Form and my project proposal. please, research about IRB. write the Description. Referencing the Determination Checklist on Page 2, briefly describe project objectives, the subject population and types of data/specimens to be collected and analyzed, how the data/specimens will be obtained, and why

(Answered) Company Description and SWOT

0 (0) Non Alcoholic Beverage company!!!! n this assignment, you will conduct a SWOT (Strength, Weakness, Opportunity, and Threat) analysis for the type of beverage you have selected, and for your company overall. As you work on the assignment, consider why you have chosen one type of non-alcoholic beverage over

(Answered) Oral Language chart of phone conversation

0 (0) Instructions In an APA formatted paper, paste the template found in doc. sharing. Define each of the terms and then watch the video of two children talking on the phone. Rate the language development of one of the two children in the video found at Use the SOLOM.

(Answered) Motivations of Private-Sector Organizations

0 (0)  Presidential Policy Directive 8 (PPD-8) is part of the national preparedness goal and provides a description for preparing for the threats in an all-hazards approach that pose the greatest risk to the United States. Community preparedness is a shared responsibility of the whole community. In addition to the

(Answered) Effective Resource Allocation

0 (0)  The discussion assignment for this week includes a review of the Key Assignment Outline completed by one of your classmates, as well as a substantial response to at least one other student. Primary Task Response: Your first task is to post your own Key Assignment Outline to the

(Answered) Ethics Case Study

0 (0) A student’s analysis of the ethic case must use the following written structural framework, which can be written as a memorandum to the file. Without the bullet points – these indicate required headings for each of the four sections. The following the description heading below to form a

(Answered) The Relativist Ethical Position.

0 (0)  In your post, address the following: To what extent would you agree with the view that research should rely on a relativist ethical position—rules or duties may vary across different communities and professional groups? What examples could you suggest to support this view? References: – Doing Research in

(Answered) Poverty in the classroom

0 (0) A four-unit research paper in APA formatting centered around the effects of poverty in education. The first unit needs to be about the effects poverty has on students in the classroom and needs to be 8-10 pages. Second unit is on the effects of substance abuse on students

(Answered) Causes of America’s War in Vietnam

0 (0) Description Formatting 12 font or smaller, standard script (e.g. Calibri, New Times Roman), 1.00-1.25 margins maximum, double spaced, clean appearance, etc. Basic organization is simple— don’t make it difficult Title— choose one that reflects your views on the topic to inform and engage your reader from the start,

(Answered) Workflows in telemetry

0 (0) Workflow analysis aims to determine workflow patterns that maximize the effective use of resources and minimize activities that do not add value. There are a variety of tools that can be used to analyze the workflow of processes and clarify potential avenues for eliminating waste. Flowcharts are a

(Answered) Critical Decision Making for Providers

0 (0) View the scenario called “Critical Decision Making for Providers”. In a 750-1,200 word paper, describe the scenario involving Mike, the lab technician, and answer the following questions: What were the consequences of a failure to report? What impact did his decision have on patient safety, on the risk