Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please write the C++program for this description. If you have any questions, please let me know. Thanks in advance!! Suppose we want to sort n
Please write the C++program for this description. If you have any questions, please let me know.
Thanks in advance!!
Suppose we want to sort n integers of an array A in ascending order using the algorithm described below. For the following explanation given below, assume that the number of elements is n > 5 and that there are no duplicates in the array: . . We first compare A[O] with A[1]. If A[O] > A[1] then A[O] and A[1] are swapped. We next compare A[1] with A[2]. If A[1] > A[2] then A[1] and A[2] are swapped. We continue comparing in pairs and swapping as necessary until all n elements of the array are compared and possibly swapped. The largest element of the array is now in A[n-1). We repeat the whole process but this time in the opposite direction ignoring A[n-1): we compare A[n-2] with A[n-3]. If A[n-3] > A[n-2] then A[n-2] and A[n-3] are swapped. We next compare A[n-3] with A[n-4]. If A[n-4] > A[n-3) then A[n-4] and A[n-3] are swapped. We continue comparing in pairs like that down the array until all n-1 elements have been compared. The smallest element in the array is then in A[0]. Then the next pass is back in the "opposite direction ignoring A[O] (we think of it as going up this time) comparing A[1] with A[2]. If A[1] > A[2] then A[1] and A[2] are swapped. We continue comparing and swapping in pairs as necessary all the way until A[n-3] is compared (and possibly swapped) with A[n-2). At this point A[n-2] has the second largest element. Now on the way down we compare A[n-3] with A[n-4) and swap if necessary and so on. We continue until the whole array is sorted in ascending order. For your assignment and in general, do not assume that n > 5.There could be duplicates in the input data. I just made those two simplifications to explain the algorithm above. Implement the algorithm described in the previous and use the function prototype void sort (int A[], int n); and test on a small array of integers. You may overload the function sort but you must still use the function prototype above and call the overloaded function. Do not bother keeping track of the swaps, i.e. you don't need a flag (Boolean variable) that would help you optimize whenever any elements were not swapped in a pass. Now using the rand() library function, generate the 21 random integers to sort. Make the random numbers be 0 or positive and less than 100. Print out the array before you sort it and after you sort it. Print 7 integers per line. Use a named constant for the values given here e.g. 7. Format your output so that you can tell quickly whether the sorting is workingStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started