Question

In: Computer Science

a) Write Quick Sort and its function algorithm b) Derive the computation time for each statement...

a) Write Quick Sort and its function algorithm

b) Derive the computation time for each statement in the algorithm. (You must explain your reason for each statement about how you get the answer of each computation time in at one or two sentences each.)

Solutions

Expert Solution

Answer : Given data

(a) Quick Short :

* Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of data into smaller arrays.

* A large array is partitioned into two arrays one of which holds values smaller than the specified value, say pivot, based on which the partition is made and another array holds values greater than the pivot value.

* Quicksort partitions an array and then calls itself recursively twice to sort the two resulting subarrays.

* In this algorithm the the best case performance O(n2), average case performance O(n logn), and worst case performance is O(n) auxiliary (naive) and
O(log n) auxiliary (Hoare 1962).

* In this short how to work-

* Taking the analogical view in perspective, consider a situation where one had to sort the papers bearing the names of the students, by name from A-Z. One might use the approach as follows:

a.Select any splitting value, say L. The splitting value is also known as pivot.

b. Divide the stack of papers into two. A-L and M-Z. It is not   necessary that the piles should be equal.

c. Repeat the above two steps with the A-L pile, splitting it into its significant two halves. And M-Z pile, split into its halves. The process is repeated until the piles are small enough to be sorted easily.

d. Ultimately, the smaller piles can be placed one on top of the other to produce a fully sorted and ordered set of papers.

e. The approach used here is reduction at each split to get to the single-element array.

f. At every split, the pile was divided and then the same approach was used for the smaller piles by using the method of recursion.

* Technically, quick sort follows the below steps:
Step 1 − Make any element as pivot.
Step 2 − Partition the array on the basis of pivot.
Step 3 − Apply quick sort on left partition recursively.
Step 4 − Apply quick sort on right partition recursively.

Quick short function algorithm-

* The algorithm was developed by a British computer scientist Tony Hoare in 1959.

* The name "Quick Sort" comes from the fact that, quick sort is capable of sorting a list of data elements significantly faster (twice or thrice faster) than any of the common sorting algorithms.

* It is one of the most efficient sorting algorithms and is based on the splitting of an array (partition) into smaller ones and swapping (exchange) based on the comparison with 'pivot' element selected.

* Due to this, quick sort is also called as "Partition Exchange" sort. Like Merge sort, Quick sort also falls into the category of divide and conquer approach of problem-solving methodology.

* This algo is divide and conqur method.

* This shorting algorithm also known as pivot algorithms.

Quick Sort Pivot Algorithm-

* This algorithm is based on our understanding of partitioning in quick sort, we will now try to write an algorithm for it, which is as follows.

Step 1 − Choose the highest index value has pivot

Step 2 − Take two variables to point left and right of the list excluding pivot

Step 3 − left points to the low index

Step 4 − right points to the high

Step 5 − while value at left is less than pivot move right

Step 6 − while value at right is greater than pivot move left

Step 7 − if both step 5 and step 6 does not match swap left and right

Step 8 − if left ≥ right, the point where they met is new pivot.

(b)-

* Computation is time complexity is how to check the algorithm like best case ,worst case and average case of the algorithm we can derive the time complexity -

* Time complexity :
Big O notation
f(n) = O(g(n)) means
* There are positive constants c and k such that:
0<= f(n) <= c*g(n) for all n >= k.
* For large problem sizes the dominant term(one with highest value of exponent) almost completely
determines the value of the complexity expression. So abstract complexity is expressed in terms of
the dominant term for large N. Multiplicative constants are also ignored.
N^2 + 3N + 4 is O(N^2)
since for N>4, N^2 + 3N + 4 < 2N^2 (c=2 & k=4)
O(1)­ constant time
* This means that the algorithm requires the same fixed number of steps regardless of the size of the task.
Example:
a) a statement involving basic operations
Here are some examples of basic operations.
one arithmetic operation (eg., +, *)
one assignment
one test (eg., x==0)
one read(accessing an element from an array)
b) Sequence of statements involving basic operations.

statement 1;
statement 2;
.....
statement k;
* Time for each statement is constant and the total time is also constant: O(1)
O(n)­ linear time
* This means that the algorithm requires a number of steps proportional to the size of the task.
Examples:
a. Traversing an array.
b. Sequential/Linear search in an array.
c. Best case time complexity of Bubble sort (i.e when the elements of array are in sorted order).
Basic structure is :
for (i = 0; i < N; i++) {
sequence of statements of O(1)
}
* The loop executes N times, so the total time is N*O(1) which is O(N).
O(n^2) ­ quadratic time
O
* The number of operations is proportional to the size of the task squared.

______________THE END_________________


Related Solutions

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[ ] ).
Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Quick sort. Write the algorithm....
Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Quick sort. Write the algorithm. show work
Write a version of the selection sort algorithm in a function called selectionSort that can be...
Write a version of the selection sort algorithm in a function called selectionSort that can be used to sort a string vector object. Also, write a program to test your algorithm. The program should prompt the user for a series of names. The string zzz should end the input stream. Output the sorted list to the console. *Need answer in C++*
Use recursion function to derive computation time for Binary Search by drawing a recursion tree diagram...
Use recursion function to derive computation time for Binary Search by drawing a recursion tree diagram and using algebra calculation.
2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge...
2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge sort, Radix sort, Bucket sort, and Counting sort.
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...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
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
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT