(Bucket Sort) A bucket sort begins with a one-dimensional array of positive integers to be sorted, and a two-dimensional array of integers with rows subscripted from 0 to 9 and columns subscripted from 0 to
The
- Loop through the one-dimensional array and place each of its values in a row of the bucket array based on its ones digit. For example, 97 is placed in row 7, 3 is placed in row 3 and 100 is placed in row 0.
- Loop through the bucket array and copy the values back to the original array. The new order of the above values in the one-dimensional array is 100, 3 and 97.
- Repeat this process for each subsequent digit position (tens, hundreds, thousands, and so on) and stop when the leftmost digit of the largest number has been processed.
On the second pass of the array, 100 is placed in row 0, 3 is placed in row 0 (it had only one digit so we treat it as 03) and 97 is placed in row 9. The order of the values in the one-dimensional array is 100, 3 and 97. On the third pass, 100 is placed in row 1, 3 (003) is placed in row zero and 97 (097) is placed in row zero (after 3). The bucket sort is guaranteed to have all the values property sorted after processing the leftmost digit of the largest number. The bucket sort knows it’s done when all the values are copied into row zero of the two-dimensional array. The two-dimensional array of buckets is ten times the size of the integer array being sorted. This sorting technique provides far better performance than a bubble sort but requires much larger storage capacity. Bubble sort requires only one additional memory location for the type of data being sorted. Bucket sort is an example of a space—time trade-off. It uses more memory but performs better. This version of the bucket sort requires copying all the data back to the original array on each pass. Another possibility is to create a second two-dimensional bucket array and repeatedly move the data between the two bucket arrays until all the data is copied into row zero of one of the arrays. Row zero then contains the sorted array.
Want to see the full answer?
Check out a sample textbook solutionChapter D Solutions
C How to Program (8th Edition)
Additional Engineering Textbook Solutions
Starting out with Visual C# (4th Edition)
C Programming Language
Database Concepts (8th Edition)
Differential Equations: Computing and Modeling (5th Edition), Edwards, Penney & Calvis
Java: An Introduction to Problem Solving and Programming (8th Edition)
Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)
- Hello, I am having trouble with this homework. Random modification of all elements in an integer array.a) Initialize an integer array of capacity 5.b) Implement an array print function.c) Print the array to the console.d) Implement an offset function that accepts an integer array as inputand randomly modifies each element by up to +/- 5.e) Use the offset function to update the array.f) Print the modified array. Example Output (input in bold italics) Original: 5 1 4 9 2Offset: 7 -2 6 11 4arrow_forward9. Implement an array with values 1, 5, 14, 23, 45, 52, 58, 81, 82 91. a) Create a getindex( function which does a linear search upon the array for a specific value n. Return the index of n, or -1 ifn does not exist, b) Print the array. c) Search the array for the values 23, 58, 11, rint the reaults. Qutpur Examnle 15 14 23 45 52 58 71 82 91 Number 23 is located at index Number 58 As located at index C Number 11 i looated at index -1arrow_forwardQuick Answer pleasearrow_forward
- Don't use vector array .using ifstream and ofstream. C++ programarrow_forwardbool 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 functionarrow_forwardPsecode code explaination of the problem C++arrow_forward
- Answer in C++ code please! (Duplicate Elimination with array) Use a one-dimensional array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, validate it and store it in the array only if it isn't a duplicate of a number already read. After reading all the values, display only the unique values that the user entered. Provide for the 'worst case' in which all 20 numbers are different. Use the smallest possible array to solve this problem.arrow_forwardIn C++arrow_forward- array from user and print all the elements after doubuq have to separate function for doubling operation. 2arrow_forward
- use javascriptarrow_forward(Locate the largest element) Write the following function that finds the location of the largest element in a two-dimensional array. void 1ocateLargest(const double a[][4 ], int 1ocation[]) The location is stored in a one-dimensional array location that contains two elements. These two elements indicate the row and column indices of the largest element in the two-dimensional array. Write a test program that prompts the user to enter a 3 × 4 two-dimensional array and displays the location of the largest element in the array.arrow_forwardNeed fun Language code please. The Programs:- (File: count.fun) Write a program which counts from 1 to 10, printing each number on a line by itself. (File: array.fun) For this program you have two requirements: Create a function which takes two arguments, an array and the number of elements in the array. Have this function print the contents of the array, one entry per line. Have the program read in an array of 10 numbers. Next, reverse the contents of the array. Finally have your program print out the contents of the array, one per line.arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning