Question

In: Computer Science

Develop the code for this sort algorithm: Shaker shakerSort is a variation of the bubbleSort where...

Develop the code for this sort algorithm:

Shaker

shakerSort is a variation of the bubbleSort where we alternately go up and then down switching out-of-order pairs until done.

In it have code that counts the number of moves and number of compares.

Solutions

Expert Solution

Solution:

Algorithm:

void shakerSort(int a[])
{
boolean swapped = true;
int start = 0;
int end = a.length;
  
while (swapped == true) {
// reset the swapped flag on entering the
// loop, because it might be true from a
// previous iteration.
swapped = false;
  
// loop from bottom to top same as
// the bubble sort
for (int i = start; i < end - 1; ++i) {
if (a[i] > a[i + 1]) {
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
swapped = true;
}
}
  
// if nothing moved, then array is sorted.
if (swapped == false)
break;
  
// otherwise, reset the swapped flag so that it
// can be used in the next stage
swapped = false;
  
// move the end point back by one, because
// item at the end is in its rightful spot
end = end - 1;
  
// from top to bottom, doing the
// same comparison as in the previous stage
for (int i = end - 1; i >= start; i--) {
if (a[i] > a[i + 1]) {
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
swapped = true;
}
}
  
// increase the starting point, because
// the last stage would have moved the next
// smallest number to its rightful spot.
start = start + 1;
}
}


Hit the thumbs up if you liked the answer. :)


Related Solutions

The Binary Insertion Sort Algorithm is a variation of the Insertion Sort Algorithm that uses a...
The Binary Insertion Sort Algorithm is a variation of the Insertion Sort Algorithm that uses a binary search technique rather than a linear search technique to insert the ith element in the correct place among the previously sorted elements. (i) Express the Binary Insertion Sort Algorithm in pseudocode. (ii) Compare the number of comparisons of elements used by the Insertion Sort Algorithm and the Binary Insertion Sort Algorithm when sorting the list (7,4,3,8,1,5,4,2). (iii) Show that the Insertion Sort Algorithm...
Develop an algorithm for INSERTION SORT. Give the pseudo-code version. Convert your pseudo-code into a Java...
Develop an algorithm for INSERTION SORT. Give the pseudo-code version. Convert your pseudo-code into a Java program.
the sort below shows an algorithm for sorting aSort(A, i, j) // where A is the...
the sort below shows an algorithm for sorting aSort(A, i, j) // where A is the array to be sorted; i is the start index and j is the end index. n = j – i + 1 If (n < 18) { sort A[i…j] by insertion-sort return } m1 = i + 2 * n / 3 m2 = i + n / 3 aSort(A, i, m1) aSort(A, m2, j) aSort(A, i, m1) questions: 1) Please state the asymptotic...
This is c++ code. Create a file sort.cpp. to mix functions with the selection sort algorithm:...
This is c++ code. Create a file sort.cpp. to mix functions with the selection sort algorithm: ·Write a function int least(vector<string> strs, int start)to return the index of the smallest value in the vector. You are to assume there is at least one value in the vector. ·Write a function void selsort(vector<string> & strs) to use selection sort to sort the vector of strings. It is a worthwhile experiment to try leaving out the & and seeing that the vector...
a. Develop a divide-and-conquer algorithm to perform a parallel merge sort of an array. Hint: After...
a. Develop a divide-and-conquer algorithm to perform a parallel merge sort of an array. Hint: After division, each process sorts its part of the array using an efficient algorithm. Then, the subarrays are merged into larger sorted subarrays. b. Analyze the communication and computation times if the number of processes is equal to the number of array elements, n. c. Repeat Part b if the number of processes is less than the number of array elements. Assume that the computation...
Analyzing Selection Sort Algorithm The selection sort algorithm works by first finding the smallest value in...
Analyzing Selection Sort Algorithm The selection sort algorithm works by first finding the smallest value in a list and swapping the first value with it, then finding the second smallest value and swapping the second value with it, continuing until all the values are in order. Implement this algorithm, then determine its growth function, and hence the order of the algorithm. Find how many swaps are made. Use Java Code to create algorithm
Please write a python code which implements the counting sort algorithm by creating a CountingSort function...
Please write a python code which implements the counting sort algorithm by creating a CountingSort function with an array input in the form of a list. Then write code to sort 3 randomly generated arrays and the data array listed below, print the output clearly for all four test arrays, develop and comment on the growth function of your code. Comment on the Big O notation of the code. ( Please do not forget to comment on your code to...
1. Sort the given keys using Counting sort algorithm. Also write the algorithm.          4, 1,...
1. Sort the given keys using Counting sort algorithm. Also write the algorithm.          4, 1, 0, 2, 1, 5, 0, 4                                                                     No code or programs, please. Manually solve the problem, please. Thanks
4) Implement the Selection Sort algorithm discussed in class to sort a list of a certain...
4) Implement the Selection Sort algorithm discussed in class to sort a list of a certain size. The list is to be implemented using a dynamic array as well as a singly linked list. You are required to compare the performance (sorting time) for the dynamic array and singly-linked list-based implementations. You are given the startup codes for the dynamic array and singly-linked list based implementations. You are required to implement the Selection Sort function in each of these codes....
Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT