C How to Program (8th Edition)
8th Edition
ISBN: 9780133976892
Author: Paul J. Deitel, Harvey Deitel
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Textbook Question
Chapter 6, Problem 6.11E
(Bubble Sort) The bubble sort presented in Fig. 6.15 is inefficient for large arrays. Make the following simple modifications to improve its performance.
- After the first pass, the largest number is guaranteed to be ¡n the highest-numbered element 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 pass and so on.
- 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 pass whether 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 one more pass is needed.
Expert Solution & Answer
Trending nowThis is a popular solution!
Students have asked these similar questions
Subject: Data structure
(Recursive Selection Sort) A selection sort searches an array looking for the smallest elementin the array. When that element is found, it’s swapped with the first element of the array. The process is then repeated for the subarray, beginning with the second element. Each pass of the arrayresults in one element being placed in its proper location. This sort requires processing capabilitiessimilar to those of the bubble sort—for an array of n elements, n – 1 passes must be made, and foreach subarray, n – 1 comparisons must be made to find the smallest value. When the subarray beingprocessed contains one element, the array is sorted. Write a recursive function selectionSort toperform this algorithm
bool ok(int q[])
which takes an array q and returns true if the array represents a valid configuration of the eight
queens and returns false otherwise.
4. Write a function
Chapter 6 Solutions
C How to Program (8th Edition)
Ch. 6 - Fill in the blanks in each of the following: C...Ch. 6 - State which of the following are true and which...Ch. 6 - Write statements to accomplish each of the...Ch. 6 - Consider a 2-by-5 integer array t. Write a...Ch. 6 - (Sales Commissions) Use a one-dimensional array to...Ch. 6 - (Bubble Sort) The bubble sort presented in Fig....Ch. 6 - Write loops that perform each of the following...Ch. 6 - Prob. 6.13ECh. 6 - (Mean, Median and Mode Program Modifications)...Ch. 6 - (Duplicate Elimination) Use a one-dimensional...
Ch. 6 - Label the elements of 3-by-5 two-dimensional array...Ch. 6 - What does the following program do?Ch. 6 - What does the following program do?Ch. 6 - (Dice Rolling) Write a program that simulates the...Ch. 6 - (Game of Craps) Write a program that runs 1000...Ch. 6 - Prob. 6.21ECh. 6 - (Total Sales) Use a two-dimensional array to solve...Ch. 6 - (Turtle Graphics) The Logo language made the...Ch. 6 - Prob. 6.24ECh. 6 - (Knights Tour: Brute-Force Approaches) In Exercise...Ch. 6 - (Eight Queens) Another puzzler for chess buffs is...Ch. 6 - (Eight Queens: Brute-Force Approaches) In this...Ch. 6 - (Duplicate Elimination) In Chapter 12, we explore...Ch. 6 - (Knights Tour: Closed Tour Test) In the Knights...Ch. 6 - (The Sieve of Eratosthenes) A prime integer is any...Ch. 6 - Prob. 6.31RECh. 6 - (Linear Search) Modify the program of Fig. 6.18 to...Ch. 6 - (Binary Search) Modify the program of Fig. 6.19 to...Ch. 6 - Prob. 6.35RECh. 6 - Prob. 6.36RECh. 6 - Prob. 6.37RE
Additional Engineering Textbook Solutions
Find more solutions based on key concepts
The __________ file format is commonly used to export spreadsheet data to a text file. a. spreadsheet data volu...
Starting out with Visual C# (4th Edition)
True or False: All static member variables are initialized to 1 by default.
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Suppose that both a program and the hardware that executes it have been formally verified to be accurate. Does ...
Computer Science: An Overview (12th Edition)
FileArray Class Design a class that has a static method named writeArray. The method should take two arguments:...
Starting Out with Java: Early Objects (6th Edition)
What does the throw statement do?
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
In Exercises 17 through 28, write a line (or lines) of code to carry out the task.
Make lblTwo disappear.
Introduction To Programming Using Visual Basic (11th Edition)
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- (Java) question 6arrow_forwardPlease complete code in Java per the instructions. Thank you!arrow_forwardAIM: Write a program to do Linear Search using a given Search Key in an un- sorted Linear Array 'A' which has N values in it. Description: The linear search compares each element of the array with the search key until the search key is found. To determine that a value is not in the array, the program must compare the search key to every element in the array 'A'. It is also called "Sequential Search" because it traverses the data sequentially to locate the element. Algorithm: (Linear Search) LINEAR SEARCH (A, N, SKEY) Here A is a Linear Array with N elements and SKEY is a given item of information to search. This algorithm finds the location of SKEY in A and if successful, it returns its location otherwise it returns -1 for unsuccessful. 1. Repeat step-2 for K = 0 to N-1 2. if(A [K] = SKEY) return K [Successful Search] [End of loop of step-1 ] 3. return -1 [Un-Successful] 4. End.arrow_forward
- (Compute total marks for each student) Suppose the marks obtained by all students are stored ina two-dimensional array. Each row records a student’s marks for five tests. For example, the followingarray stores the test marks for eight students. Write a program that displays students and the totalmarks they obtained in five tests, in decreasing order of the total marks.arrow_forwardIn C++arrow_forward12) Fun With Mergesort. Given the recursive mergesort function below. Modify the msort function such that, msort calls msort for sub arrays with more than 1024 elements, and msort calls bsort for sub arrays with 1024 or less elements. Assume both functions operate on the same global array. int data[4294967296]; // REALLY BIG array void bsort (int f, int 1); // forward declaration void msort (int f, int 1) int m; if (f<1) { m - (f+1)/2; msort (f, m): msort (m+1,1): merge (f,m, 1);arrow_forward
- 4 Quick Sort Given the following array: {5,1,2,7,9,3,7,8,4}, illustrate the operation of PAR- TITION (which is defined on page 4 of the slides of lecture 4) on the given array. Show what the array looks like in each step. ALWAYS use the first element as the pivot.arrow_forward90. Mos.arrow_forward(Java) choose the correct multiple choicearrow_forward
- In main() for now: do these - one at a time, each in it's own loop (we will make functions out of them later –Declare an array RandArray with 20 integers –Assign each of the 20 values with a random value from 0 to 99 Hint: Use rand()%100 For mimir: Do not call srand at the top of main. // normally: Call srand(time(0)) at the top of main –(you need #include<cstdlib>) –Write another loop that prints the array, with index values Important: If the output values do not match mimir, please add srand(17); // inside your main function - at the top code format: #include <iostream>using namespace std; #include <cstdlib> // required for rand() int main(){ srand(17); // define a constant ARRAYSIZE that is 20 // declare randArray // set the 20 elements in randArray to be a random number between 0 and 99 // hint: use rand()%100 // print the 20 values return 0;}arrow_forward1.""Implementation of the Misra-Gries algorithm.Given a list of items and a value k, it returns the every item in the listthat appears at least n/k times, where n is the length of the array By default, k is set to 2, solving the majority problem. For the majority problem, this algorithm only guarantees that if there isan element that appears more than n/2 times, it will be outputed. If thereis no such element, any arbitrary element is returned by the algorithm.Therefore, we need to iterate through again at the end. But since we have filtredout the suspects, the memory complexity is significantly lower thanit would be to create counter for every element in the list. For example:Input misras_gries([1,4,4,4,5,4,4])Output {'4':5}Input misras_gries([0,0,0,1,1,1,1])Output {'1':4}Input misras_gries([0,0,0,0,1,1,1,2,2],3)Output {'0':4,'1':3}Input misras_gries([0,0,0,1,1,1]Output None""".expl.arrow_forward()8. integer array nums and an integer k, return the length of the shortest non-empty subarray of nums with a sum of at least k. If there is no such subarray, return -1. A subarray is a contiguous part of an array. Example 1: Input: nums = [1], k = 1 Output: 1 Example 2: Input: nums = [1,2], k = 4 Output: -1 Example 3: Input: nums = [2,-1,2], k = 3 Output: 3.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
1.1 Arrays in Data Structure | Declaration, Initialization, Memory representation; Author: Jenny's lectures CS/IT NET&JRF;https://www.youtube.com/watch?v=AT14lCXuMKI;License: Standard YouTube License, CC-BY
Definition of Array; Author: Neso Academy;https://www.youtube.com/watch?v=55l-aZ7_F24;License: Standard Youtube License