0% found this document useful (0 votes)
4 views

Merge sort and quick sort__

Quick sort is an internal sorting algorithm that uses a divide and conquer strategy, partitioning elements based on a pivot. Its time complexity is O(n log n) in the best and average cases, and O(n^2) in the worst case, with a space complexity of O(n). Merge sort, an external algorithm, also employs a divide and conquer strategy, consistently achieving O(n log n) time complexity across all cases and requiring O(n) space.

Uploaded by

it10800223115
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Merge sort and quick sort__

Quick sort is an internal sorting algorithm that uses a divide and conquer strategy, partitioning elements based on a pivot. Its time complexity is O(n log n) in the best and average cases, and O(n^2) in the worst case, with a space complexity of O(n). Merge sort, an external algorithm, also employs a divide and conquer strategy, consistently achieving O(n log n) time complexity across all cases and requiring O(n) space.

Uploaded by

it10800223115
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Quick sort:

Quick sort is an internal algorithm which is based on divide and


conquer strategy.

The array of elements is divided into parts repeatedly until it is not


possible to divide it further.
It is also known as “partition exchange sort”.
It uses a key element (pivot) for partitioning the elements.
One left partition contains all those elements that are smaller than
the pivot and one right partition contains all those elements which
are greater than the key element.

Time Complexity
Best Case: O(n log n)
Average Case: O(n log n)
Worst Case: O(n2)

Space Complexity: O(n)


Algorithm
 Pick an element, called a pivot, from the array.
 Partitioning: reorder the array so that all elements with values
less than the pivot come before the pivot, while all elements with
values greater than the pivot come after it (equal values can go
either way). After this partitioning, the pivot is in its final position.
This is called the partition operation.
 Recursively apply the above steps to the sub-array of elements
with smaller values and separately to the sub-array of elements
with greater values.

There are multiple variants of quick sort depending on the choice of


pivot. Some popular choices of pivots being:

First element of the unsorted array


Last element of the unsorted array
Middle element of the unsorted array
Random element from the unsorted array
Merge sort
Merge sort is an external algorithm and based on divide and conquer
strategy.

The elements are split into two sub-arrays (n/2) again and again until
only one element is left.
Merge sort uses additional storage for sorting the auxiliary array.
Merge sort uses three arrays where two are used for storing each
half, and the third external one is used to store the final sorted list
by merging other two and each array is then sorted recursively.
At last, the all sub arrays are merged to make it ‘n’ element size of
the array.

Time Complexity
Best Case: O(n log n)
Average Case: O(n log n)
Worst Case: O(n log n)

Space Complexity: O(n)


Algorithm
The algorithm looks something like this:

 Sort the left half of the array


 Sort the right half of the array
 Merge both the halves of the array

You might also like