Question

In: Computer Science

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 worst-case running time for aSort with valid workings.
2) Please prove that aSort(A, 1, n) sorts the array A of n elements correctly.

Solutions

Expert Solution

1.The complexity function is:

T(n) = 3T(2n/3) + CONST
This is because:

You have three recursive calls for a problem of size 2n/3
The constant modifier here is O(1), since all non recursive calls operations are bounded to a constant size (Specifically, insertion sort for n<=18 is O(1)).
If you go on and calculate it, you'll get worse than O(n^2) complexity

2.

• By induction. Assume your algorithm works for problems of size n, and let's examine a problem of size n+1. For n+1<18 it is trivial, so we ignore this case.

• After first recursive call, you are guaranteed that first 2/3 of the array is sorted, and in particular you are guaranteed that the first n/3 of the elements are the smallest ones in this part. This means, they cannot be in the last n/3 of the array, because there are at least n/2 elements bigger than them. This means, the n/3 biggest elements are somewhere between m2 and j.

• After second recursive call, since it is guaranteed to be invoked on the n/3 biggest elements, it will place these elements at the end of the array. This means the part between m1 and j is now sorted properly. This also means, 2n/3 smallest elements are somewhere in between i and m1.

• Third recursive call sorts the 2n/3 elements properly, and the n/3 biggest elements are already in place, so the array is now sorted


Related Solutions

Import a data set (txt file) then do the sorting algorithm using bubble sort, radix sort,...
Import a data set (txt file) then do the sorting algorithm using bubble sort, radix sort, insertion sort, and merge sort, It must show how long it took and how many movements occurred. Please write codes in C++ Here's data set (should be stored in txt file) 7426 4524 4737 9436 3997 2757 6288 5414 9590 5968 6638 3199 9514 1541 9866 2144 6731 911 2171 6135 6437 912 9417 2662 6606 6349 707 2890 5386 9718 3492 5068 9674...
Import a data set (txt file) then do the sorting algorithm using quick sort, shell sort,...
Import a data set (txt file) then do the sorting algorithm using quick sort, shell sort, and selection sort. It must show how long it took and how many movements occurred. Please write codes in C++ Here's data set (should be stored in txt file) 7426 4524 4737 9436 3997 2757 6288 5414 9590 5968 6638 3199 9514 1541 9866 2144 6731 911 2171 6135 6437 912 9417 2662 6606 6349 707 2890 5386 9718 3492 5068 9674 8578 8323...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C program which accepts 1 command-line argument: the name of a text file which contains integers, one-per line. Your C program must be named project3. Your C program needs to implement the QuickSort algorithm to sort the integers read from the file specified on the command-line. Your QuickSort implementation must utilize the median-of-three algorithm for choosing a pivot, and BubbleSort any sub arrays with less...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
It's time to write a sorting algorithm! In this lab, you'll be writing Bubble Sort. Much...
It's time to write a sorting algorithm! In this lab, you'll be writing Bubble Sort. Much like the previous lab, you will be tasked with prompting the user for a list of values until the user enters 0 (you may use the same initializeVector that you wrote in the last lab). You will then write bubblesort which sorts the vector from smallest to largest. You will then call a function displayVector which displays the vector to the screen. Keep everything...
1.) (a) There exists a very popular sorting algorithm called Timsort, the default sorting algorithm in...
1.) (a) There exists a very popular sorting algorithm called Timsort, the default sorting algorithm in both Python and Java. This sort is a combination of two different sorting algorithms: Merge sort, and Insertion sort. Recall that the Big-O of Merge sort is O(nlogn) and the Big-O of Insertion sort is O(n 2 ). What advantage would Timsort have to combine the two algorithms if merge-sort has a better Big-O metric? (b) Consider two algorithms: f(n) and g(n). You run...
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 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.
How can I determine or explain how BingoSort is a stable sorting algorithm or not?
The following submission rules apply: • For those questions requiring programs, the solutions must be implemented using JavaScript or Java. o Appropriate self-documenting comments in the source code are mandatory, consistent with good programming practices. o Solutions must be provided in plain text so that formatting is not lost. • All answers must be provided in this document. • Sources must be given accurate and complete citations sufficient for the instructor to find and confirm them.How can I determine or...
Which Sorting algorithm are in place algorithm and which are not? Is it advantageous to design...
Which Sorting algorithm are in place algorithm and which are not? Is it advantageous to design in place sorting algorithm? How this in place term is related to the time complexity and space complexity of the program?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT