Question: Draw a flowchart for the bubble sort program shown in Figure 11-34. 1 //Bubble Sort.cpp - uses the bubble sort to sort the 2 //contents
Draw a flowchart for the bubble sort program shown in Figure 11-34.
![1 //Bubble Sort.cpp - uses the bubble sort to sort the 2 //contents of a one-dimensional array in ascending order 3 //Created/revised by on 4 5 #include 6 using namespace std; 7 8 int main() 9 { int numbers[4] {23, 46, 12, 35}; int sub = 0;](https://dsd5zvtm8ll6.cloudfront.net/si.question.images/images/question_images/1597/9/8/9/4125f3f62244db971597989400630.jpg)
1 //Bubble Sort.cpp - uses the bubble sort to sort the 2 //contents of a one-dimensional array in ascending order 3 //Created/revised by on 4 5 #include 6 using namespace std; 7 8 int main() 9 { int numbers[4] {23, 46, 12, 35}; int sub = 0; int temp = 0; int maxSub - 3; int lastSwap = 0; char swap = 'Y'; 10 11 //keeps track of subscripts //variable used for swapping //maximum subscript 7/position of last swap /indicates if a swap was made 12 13 15 16 //repeat loop instructions as long as a swap was made while (swap =- 'Y') 17 18 19 20 Swap = 'N'; //assume no swaps are necessary 21 //begin comparing with first //array element 22 sub - 0; 24 25 //compare adjacent array elements to determine //whether a swap is necessary while (sub < maxSub) 26 27 28 if (numbers[sub] > numbers [sub + 1]) { //a swap is necessary temp = numbers [sub]; numbers [sub] = numbers [sub + 1]; numbers [sub + 1] = temp; swap = 'Y'; lastSwap = sub; } //end if sub += 1; //increment subscript } //end while 29 30 31 32 33 36 37 38 39 40 41 maxSub = lastSwap; //reset maximum subscript 42 } //end while //display sorted array for (int x = 0; x < 4; x += 1) cout numbers[x] < < endl; //end for return 0; 45 46 47 48 49 } //end of main function Figure 11-34 Bubble sort program 111 L1 2~2 2~ 333 333 44 444 44
Step by Step Solution
3.34 Rating (169 Votes )
There are 3 Steps involved in it
O M 1 F stop 4 F start assign 0 to sub temp and lastSwap ass... View full answer
Get step-by-step solutions from verified subject matter experts
