(Bubble Sort) The bubble sort presented in Fig. 6.15 is inefficient for large arrays. Make the 6.11 following simple modifications to improve its performance. a) After the first pass, the largest number is guaranteed to be in the highest-numbered el- ement of the array; after the second pass, the two highest numbers are "in place," and so on. Instead of making nine comparisons on every pass, modify the bubble sort to make eight comparisons on the second pass, seven on the third b) The data in the array may already be in the proper or near-proper order, so why make nine passes if fewer will suffice? Modify the sort to check at the end of each any swaps have been made. If none has been made, then the data must already be in the proper order, so the program should terminate. If swaps have been made, then at least CC • and So on. pass whether pass one more pass is needed. adimarninnalonoronorotione 11 6.8 Sorting Arrays increasing order (or if the values are identical), v decreasing order, their values are swapped in the array. 235 // Fig. 6.15: fig06_15.c // Sorting an array's values into ascending order. #include #define SIZE 10 // function main begins program execution int main(void) // initialize a int a[SIZE] = {2, 6, 4, 8, 10, 12, 89, 68, 45, 37}; 10 %3D puts ("Data items in original order"); 12 13 // output original array for (size_ti = 0; i < SIZE; ++i) { printf("%4d", a[i]); } 14 15 16 17 18 // bubble sort //loop to control number of passes for (unsigned int pass 19 20 gmis 21 1; pass < SIZE; ++pass) { 22 // loop to control number of comparisons per pass for (size_t i = 0; i < SIZE 23 24 1; ++i) { 25 // compare adjacent elements and swap them if first // element is greater than second element if (a[i] > a[i + 1]) { int hold = a[i]; a[i] = a[i + 1]; a[i + 1] = hold; 26 27 28 29 30 2.0 Agnial) 31 32 33 } 34 za roy algms 35 aluesi puts("\nData items in ascending order"); 36 37 // output sorted array for (size_t i = 0; i < SIZE; ++i) { printf("%4d", a[i]); ibarm.nssm ad 38 39 40 41 42 puts(""); 43 44 } Data items in original order 2 6 4 8 10 12 89 68 45 37 Data items in ascending order 2 4 6 8 10 12 37 45 68 89 Fig. 6.15 | Sorting an array's values into ascending order.

Question

Question 6.11 . Answer in c program language . 

(Bubble Sort) The bubble sort presented in Fig. 6.15 is inefficient for large arrays. Make the
6.11
following simple modifications to improve its performance.
a) After the first pass, the largest number is guaranteed to be in the highest-numbered el-
ement of the array; after the second pass, the two highest numbers are "in place," and
so on. Instead of making nine comparisons on every pass, modify the bubble sort to
make eight comparisons on the second pass, seven on the third
b) The data in the array may already be in the proper or near-proper order, so why make
nine passes if fewer will suffice? Modify the sort to check at the end of each
any swaps have been made. If none has been made, then the data must already be in the
proper order, so the program should terminate. If swaps have been made, then at least
CC •
and
So on.
pass
whether
pass
one more pass is needed.
adimarninnalonoronorotione
11
Expand
Transcribed Image Text

(Bubble Sort) The bubble sort presented in Fig. 6.15 is inefficient for large arrays. Make the 6.11 following simple modifications to improve its performance. a) After the first pass, the largest number is guaranteed to be in the highest-numbered el- ement of the array; after the second pass, the two highest numbers are "in place," and so on. Instead of making nine comparisons on every pass, modify the bubble sort to make eight comparisons on the second pass, seven on the third b) The data in the array may already be in the proper or near-proper order, so why make nine passes if fewer will suffice? Modify the sort to check at the end of each any swaps have been made. If none has been made, then the data must already be in the proper order, so the program should terminate. If swaps have been made, then at least CC • and So on. pass whether pass one more pass is needed. adimarninnalonoronorotione 11

6.8 Sorting Arrays
increasing order (or if the values are identical), v
decreasing order, their values are swapped in the array.
235
// Fig. 6.15: fig06_15.c
// Sorting an array's values into ascending order.
#include <stdio.h>
#define SIZE 10
// function main begins program execution
int main(void)
// initialize a
int a[SIZE] = {2, 6, 4, 8, 10, 12, 89, 68, 45, 37};
10
%3D
puts ("Data items in original order");
12
13
// output original array
for (size_ti = 0; i < SIZE; ++i) {
printf("%4d", a[i]);
}
14
15
16
17
18
// bubble sort
//loop to control number of passes
for (unsigned int pass
19
20
gmis
21
1; pass < SIZE; ++pass) {
22
// loop to control number of comparisons per pass
for (size_t i = 0; i < SIZE
23
24
1; ++i) {
25
// compare adjacent elements and swap them if first
// element is greater than second element
if (a[i] > a[i + 1]) {
int hold = a[i];
a[i] = a[i + 1];
a[i + 1] = hold;
26
27
28
29
30
2.0
Agnial)
31
32
33
}
34
za roy
algms
35
aluesi
puts("\nData items in ascending order");
36
37
// output sorted array
for (size_t i = 0; i < SIZE; ++i) {
printf("%4d", a[i]);
ibarm.nssm ad
38
39
40
41
42
puts("");
43
44 }
Data items in original order
2 6 4 8 10 12 89 68 45 37
Data items in ascending order
2 4 6 8 10 12 37 45 68 89
Fig. 6.15 | Sorting an array's values into ascending order.
Expand
Transcribed Image Text

6.8 Sorting Arrays increasing order (or if the values are identical), v decreasing order, their values are swapped in the array. 235 // Fig. 6.15: fig06_15.c // Sorting an array's values into ascending order. #include <stdio.h> #define SIZE 10 // function main begins program execution int main(void) // initialize a int a[SIZE] = {2, 6, 4, 8, 10, 12, 89, 68, 45, 37}; 10 %3D puts ("Data items in original order"); 12 13 // output original array for (size_ti = 0; i < SIZE; ++i) { printf("%4d", a[i]); } 14 15 16 17 18 // bubble sort //loop to control number of passes for (unsigned int pass 19 20 gmis 21 1; pass < SIZE; ++pass) { 22 // loop to control number of comparisons per pass for (size_t i = 0; i < SIZE 23 24 1; ++i) { 25 // compare adjacent elements and swap them if first // element is greater than second element if (a[i] > a[i + 1]) { int hold = a[i]; a[i] = a[i + 1]; a[i + 1] = hold; 26 27 28 29 30 2.0 Agnial) 31 32 33 } 34 za roy algms 35 aluesi puts("\nData items in ascending order"); 36 37 // output sorted array for (size_t i = 0; i < SIZE; ++i) { printf("%4d", a[i]); ibarm.nssm ad 38 39 40 41 42 puts(""); 43 44 } Data items in original order 2 6 4 8 10 12 89 68 45 37 Data items in ascending order 2 4 6 8 10 12 37 45 68 89 Fig. 6.15 | Sorting an array's values into ascending order.

Expert Answer

1 Rating

Want to see the step-by-step answer?

Check out a sample Q&A here.

Want to see this answer and more?

Experts are waiting 24/7 to provide step-by-step solutions in as fast as 30 minutes!*

*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.
Tagged in
Engineering
Computer Science

Related Computer Science Q&A

Find answers to questions asked by students like you.

Q: Write a complete Python for loop to print the names of all friends in pals on one line separated by ...

A:  Program approach-Lists are like the arrays that have been declared in other languages. A single lis...

Q: Write a program in python with no imports that the owner of Chica Chic could use to store data about...

A: open is used to open file in write mode “w”.while(True) is used to repetitive input.input() function...

Q: In Book computer networking a top-down approach sixth edition 4.4 (Network Address Translation (NAT)...

A: NAT table stands for Network Address Translation table.It is used by routers to translate private IP...

Q: Hi, how can I find out what kind of thread models (one-one, one-many or many many) does my operating...

A: Thread model used by operating system:Whenever there is a need for the operating system to multiplex...

Q: Design a 4bits combinational circuit 2’s complementer (the output generates the 2’s complement of th...

A: Truth Table which generates the 2’s compliment of input binary number is given below:

Q: Output the information as a formatted histogram. Each name is right justified with a setw() value of...

A: Program:#include &lt;iostream&gt;#include &lt;string&gt;#include &lt;iomanip&gt; using namespace std...

Q: Trace and document every step of execution of the following program: public class Trace {    public ...

A: Modifying the given program code so that the output steps can be understand easily:public class Trac...

Q: Write one Python statement to print variable price in currency format (with commas for thousands).

A: Program:price = 78200000print("\nOriginal Number: ", price)print("Printing in currency format: "+"{:...

Q: I'm trying to put c code together to convert up to a 5 digit binary to decimal . I keep getting wron...

A: Programming instruction:To obtain the decimal equivalent of a given binary number, extract the digit...

Q: In C++: given the following recursive definition  f(1) = f(2) = 2 f(n) = 2* f(n-1) + f(n-2) write th...

A: Program:#include &lt;iostream&gt;using namespace std; int f(int n){    if ((n == 1) || (n == 2))    ...

Q: Create a query that uses any THREE of the tables (SCHOOL, DEPARTMENT, PROFESSOR, TECHNOLOGY &amp; CO...

A: Creating all the three tables required for the question-For Department-create table DEPARTMENT(DEPAR...

Q: I am programming for java and the instructions require for Hi, I am programming in java. The instruc...

A: A 2-D array is created with 10x10 elements.All entries of 2-D array are filled randomly using random...

Q: Using OOP language Java Given a currency with denominations of 100, 50, 20, 10, 5 and 1 write a meth...

A: Program DescriptionWe define a class called MakeChange which has a private static method called make...

Q: Does Amazon Web Services support Netflix as a Customer relationship management (CRN) or Enterprise r...

A: Customer Relationship Management (CRM):It is a combination of strategies used control, manage and an...

Q: Trace the execution of binary search for key=72  showing each complete pass on the following number ...

A: Answer:Finding the position of binary tree key = 72:

Q: What is error? Explain types of error?

A: Error:Error is defined as an activity that the user have done which is considered to be wrong.

Q: Which variables below are syntactically incorrect?             MAX              _mid              d...

A: The given variables can be declared as follows –int MAX;int _mid;int double;int var@;int peta;int ha...

Q: Provide the code to create an input box for the country field with the default text "United States".

A: Code:&lt;!DOCTYPE html&gt;&lt;html&gt;&lt;body&gt;Country: &lt;input name="country" value="United St...

Q: Find the most frequent character in a sentence and print the letter and then the frequency. , not ju...

A: The python program and approach are given below:Approach:Defining the value of size variable as per ...

Q: Write a program in python with no imports named filemaker.py that will be used to store the first na...

A: Create a file named filemaker.pyPrompt user for name of a friend and his age repeatedly until user e...

Q: Create a program that: 1. Has two functions, both functions have the same name -- increase. 2. One f...

A: Program Instructions: Create a C++ Program and take input from a user and then if a user enters an o...

Q: This lab will exercise your understanding of some of the concepts covered in Chapter 14: pointers an...

A: Create a integer pointer variable intPointer and an integer variable someIntintPointer stores the ad...

Q: How would I decrypt an RSA message? Let's say we're given C, p, q, e.

A: Steps to decrypt an RSA message:The formula to get the plain text from cyber text using RSA algorith...

Q: Why is Cloud Computing a popular choice for small organizations and start-ups? Canlarge organization...

A: Click to see the answer

Q: I need help writing a java object oriented code using threads methods. Thank you

A: Program:import java.util.*; public class Main{public static void main(String[] args){try{ final Thre...

Q: What are looping control structures in c? Explain with the help of program?

A: In a program, Loop control structure is used to execute a block of code a finite number of times unt...

Q: For C++  in a doubly linked list, how would you print the list in reverse?? void double LL::printRev...

A: Program Instructions:Create a node pointer last and use it to point a specific node at a time.Print ...

Q: Write a method called flipMap() that takes in a map from string keys to string values, and returns a...

A: Program approach-Import the essential java packagesDefine the main function.Declare the variables an...

Q: 3.35. Input an integer ( 5 digits or less ) containing only 1's and 0's and print its decimal equiva...

A: A C program to print the decimal equivalent of binary integer is as follows,File name: “main.c”#incl...

Q: C++ please

A: Infix to Postfix:At the time of conversion from infix to postfix, divide the whole expression into s...

Q: Sentinel Value- Python Coding This program runs to figure out if numbers the user enters are odd or ...

A: Program Instructions:Inside the first \'while\' loop, instead of using \'while\' loop using if-else ...

Q: Why is the growth of Streaming disruptive to traditional Cable and Satellite TV providers?

A: The advent of digital technology has changed the way we offer services to customers. For example, di...

Q: Write a method that takes a given string and replaces all occurrences of one string with another str...

A: The given criteria is written in Java programming language as follows,File name: “ReplaceStr.java”//...

Q: Write a program in python w/ no imports that the owner of Chica Chic could use to store data about h...

A: Open a file called inventory.txtThe file inventory.txt has the name of an inventory, cost, and quant...

Q: Write a method called listFilter() that takes in a list of strings, and returns a list containing on...

A: Method://Method definitionpublic List&lt;String&gt; listFilter(List&lt;String&gt; list){//Create a n...

Q: Which of the following describes Routers? There is more than one correct answer.     a. Critical...

A: Router:Connect networks that use different identities of the network.Only transmit the data needed t...

Q: Write a program with total change amount as an integer input that outputs the change using the fewes...

A: Completing the given C++ program so, that it can follow the given rubrics:Firstly, displaying a mess...

Q: Im working on homework and am stuck. I have created the code for taking the input of the user but I ...

A: Create a class named MainClassDeclare 2 dimensional array of students which contains 10 students, ea...

Q: num = 12File nums.txt is open in write mode and associated with object myfile. Write a Python statem...

A: In this question, the write function is used to open in write mode in pythonFunction: 1.  Write used...

Q: Provide JavaScript code to change the source of the first inline image in the document to introlmg.p...

A: Program Instructions:For a given inline image with an ID, document can be used to change its source....

Q: Create a class of students that should contain fields like rollno,name,subject and marks?

A: Programming language assumed is C++:Class:A C++ class is the building block, which contributes to th...

Q: For C++ In binary tree how would you find the number of nodes divible by 3? NumOfNodesDivisbleby3()

A: This C++ program counts the number of nodes divisible by 3.In main() function, user insert the nodes...

Q: I have to add the following solution to a code. The chapter is focused on  strings and string manipu...

A: Program:month = ['placeHolder for 0','January', 'February', 'March', 'April', 'May', 'June', 'July',...

Q: If the keyboard input is: A B a, what is the output for the following program and why? #include &lt;...

A: Source Code#include &lt;iostream&gt;using namespace std;main(){char x,y,z;cin &gt;&gt; x &gt;&gt; y ...

Q: In a Mesh Network, all nodes connect to one another in a flat or non-hierarchical manner. Core or ba...

A: Mesh network:In mesh network, nodes are connected to as many other nodes as possible in a direct, no...

Q: 80/20 rule states that 80% of the instruction ios executed and 20% of the instruction is generated i...

A: This rule is discovered by Wilfredo pareto and it is also called as pareto principle. it is a very u...

Q: For mobile devices that use virtual keyboards, what is the advantage of using a type attribute value...

A:  The input element attribute “tel” in an input box allows a user to enter and edit telephone numbers...

Q: If you were an IT Director, would you recommend SDN? Why or why not?

A: Software Defined Networking (SDN):The Software Defined Networking (SDN) is basically a new technolog...

Q: JavaScript Project     Create a guessing game program.   Prompt the user to guess a random number (M...

A: Save this program to game.html.Double click on html file to open it in browser.

Q: What is the "WHERE statment" in the query attached?

A: Difference between where and having clause:Both WHERE and HAVING clauses are used to filter the data...