Problem 5: Suppose that a list contains integers that are in order of largest to
ID: 3197088 • Letter: P
Question
Problem 5: Suppose that a list contains integers that are in order of largest to smallest and an integer can appear repeatedly in this list. Devise an algorithm that locates all occurrences of an integer x in the list. Problem 6: The shaker sort (or bidirectional bubble sort) successively compares pairs of adjacent elements, exchanging them if they are out of order, and alternately passing through the list from the beginning to the end and then from the end to the beginning until no exchanges are needed. a) Show the steps used by the shaker sort to sort the following list: 3, 7, 1, 4, 6, 2, 1, 17, 8, 4 b) Express the shaker sort in pseudocode.Explanation / Answer
(PROBLEM-5)To solve this question first we have to know what is algorithm.
algorithm-it is a step by step procedure or a way to solve any question or condition.
now steps for writing an algorithm
SUPPOSE x integers as input to solve the steps that locates all occurances of an integer x in the list
STEPS
procedure all occurance(b1,b2,b3....................bn :natural numbers)
position=1
max:=b1
for i:=2 to x i is integer
if max<b1 then
max:=b1
position=position +1
return position
final value is all occurances of integer x
(PROBLEM-6)-a)-
shaker sort method code
void shakerSort(T a[], int n)
{// Sort a[0:n-1] using shaker sort.
for (int p = 1; p <= a.length / 2; p++)
{// phase p of shaker sort
// first do left to right bubbling pass
for (int i = p - 1; i < n - p; i++)
if (a[i] > a[i+1])
swap(a[i], a[i + 1]);
// now do right to left bubbling pass
for (int i = n - p - 1; i >= p; i--)
if (a[i] < a[i-1])
swap(a[i], a[i - 1]);
}
}
}