Menu

(Solved) : 611 Bubble Sort Bubble Sort Presented Fig 615 Inefficient Large Arrays Make Following Simp Q44001051 . . .

write in c programming language #6.11
6.11 (Bubble Sort) The bubble sort presented in Fig. 6.15 is inefficient for large arrays. Make the following simple modifica6.8 Sorting Arrays 235 torreasing order or if the values are ideal e he values as they are. If a pairs to decreasing order, t6.11 (Bubble Sort) The bubble sort presented in Fig. 6.15 is inefficient for large arrays. Make the 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 pass and so on. 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 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. J: 6.8 Sorting Arrays 235 torreasing order or if the values are ideal e he values as they are. If a pairs to decreasing order, their values are swapped in the array, // Fig. 6.15: fig06 15. Sorting an array’s values into ascending order. include <stdio.h> #define SIZE 10 // function main begins program execution 7 int main(void) // initialize a int a[SIZE] = {2, 6, 4, 8, 10, 12, 89, 68, 45, 37); 10 puts(“Data items in original order): // output original array for (size_t i = 0; i < SIZE; ++i) { printf(“%d”, a[i]); 18 20 // bubble sort // loop to control number of passes for (unsigned int pass -1; pass < SIZE; ++pass) // loop to control number of comparisons per pass for (size_t i = 0; i < SIZE – 1; ++i) { // 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; Soba 36 puts(“nData items in ascending order”); 39 // output sorted array for (size_t i = 0; i < SIZE; ++i) { printf(“%d”, a[i]): 42 43 puts(“”); 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. Show transcribed image text 6.11 (Bubble Sort) The bubble sort presented in Fig. 6.15 is inefficient for large arrays. Make the 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 pass and so on. 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 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. J:
6.8 Sorting Arrays 235 torreasing order or if the values are ideal e he values as they are. If a pairs to decreasing order, their values are swapped in the array, // Fig. 6.15: fig06 15. Sorting an array’s values into ascending order. include #define SIZE 10 // function main begins program execution 7 int main(void) // initialize a int a[SIZE] = {2, 6, 4, 8, 10, 12, 89, 68, 45, 37); 10 puts(“Data items in original order): // output original array for (size_t i = 0; i

Expert Answer


Answer to 6.11 (Bubble Sort) The bubble sort presented in Fig. 6.15 is inefficient for large arrays. Make the following simple mod…

OR