52.0k views
5 votes
Design an algorithm to rearrange elements of a given array of n real numbers so that all its negative elements precede all its positive elements. Your algorithm should be both time-efficient and space-efficient.

a) Bubble sort algorithm
b) QuickSort algorithm
c) MergeSort algorithm
d) Insertion sort algorithm

User Solkin
by
7.2k points

1 Answer

4 votes

The QuickSort algorithm and the MergeSort algorithm can efficiently rearrange the elements of a given array of real numbers.

Both the QuickSort algorithm and the MergeSort algorithm can efficiently rearrange the elements of a given array of real numbers so that all negative elements precede all positive elements.

The QuickSort algorithm works by selecting a pivot element, partitioning the array into two sub-arrays (one with negative elements and the other with positive elements), and then recursively sorting the sub-arrays.

The MergeSort algorithm works by dividing the array into two halves, sorting each half separately, and then merging the sorted halves to obtain the final sorted array.

User Berenice
by
7.2k points