Show the array [2, 4, 1, 5. 31 after the for loop in pancake_sort executes once. What is the best-case complexity of the pancake_sort algorithm above including helper functions? What is the worst-case complexity of the pancake_sort algorithm above including helper functions? Which of the elementary sorting algorithms discussed in class is closest to (but not exactly the same as) what Joe has written? Use the complexities to guide your response. Circle your answer bubble / selection / insertion

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter6: Arrays
Section: Chapter Questions
Problem 19RQ
icon
Related questions
Question
1. Joe Pancake has just invented what he thinks is a great sorting algorithm. Consider the following
code:
/**
* Returns index of the maximum element in array[0..n-1].
*/
int find_max(int array[], const int length) {
int max_index, i;
for (max_index 0, i
=
0; i < length; ++i) {
if (array[i]> array[max_index]) {
max_index =
}
}
return max_index;
}
}
/**
* Reverses array [0..limit].
*/
void flip(int array[], int limit) {
int temp, start = 0;
while start < limit)
array[start];
}
temp
i;
=
array[start] = array[limit];
array[limit]
= temp;
start++;
limit--;
}
void pancake_sort(int array[], const int length) {
for (int curr_size length; curr_size > 1; --curr_size) {
int max_index find_max(array, curr_size);
if (max_index != curr_size-1) {
}
flip(array, max_index);
flip(array, curr_size-1);
Transcribed Image Text:1. Joe Pancake has just invented what he thinks is a great sorting algorithm. Consider the following code: /** * Returns index of the maximum element in array[0..n-1]. */ int find_max(int array[], const int length) { int max_index, i; for (max_index 0, i = 0; i < length; ++i) { if (array[i]> array[max_index]) { max_index = } } return max_index; } } /** * Reverses array [0..limit]. */ void flip(int array[], int limit) { int temp, start = 0; while start < limit) array[start]; } temp i; = array[start] = array[limit]; array[limit] = temp; start++; limit--; } void pancake_sort(int array[], const int length) { for (int curr_size length; curr_size > 1; --curr_size) { int max_index find_max(array, curr_size); if (max_index != curr_size-1) { } flip(array, max_index); flip(array, curr_size-1);
Show the array [2, 4, 1, 5. 31 after the for loop in pancake_sort executes once.
What is the best-case complexity of the pancake_sort algorithm above including helper functions?
What is the worst-case complexity of the pancake_sort algorithm above including helper functions?
Which of the elementary sorting algorithms discussed in class is closest to (but not exactly the same
as) what Joe has written? Use the complexities to guide your response. Circle your answer
bubble / selection / insertion
Transcribed Image Text:Show the array [2, 4, 1, 5. 31 after the for loop in pancake_sort executes once. What is the best-case complexity of the pancake_sort algorithm above including helper functions? What is the worst-case complexity of the pancake_sort algorithm above including helper functions? Which of the elementary sorting algorithms discussed in class is closest to (but not exactly the same as) what Joe has written? Use the complexities to guide your response. Circle your answer bubble / selection / insertion
Expert Solution
steps

Step by step

Solved in 5 steps with 2 images

Blurred answer
Knowledge Booster
Randomized Select Algorithm
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
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage