Question: C# 1. Given a subarray (between first index and last index), swap all the elements (from first to last - 1) that are smaller than

C#

1. Given a subarray (between first index and last index), swap all the elements (from first to last - 1) that are smaller than the last element to before a specific position. We will call this position the pivot because it is a fixed position. Finally, we need to move the last element to the pivot. /// /// /// /// /// A subarray: [1, 3,2, 6, 9,4, 8, 7,5] /// /// Ordering doesn't matter. You will need to swap the elements so that you are moving all the elements that are smaller than the pivot value to the beginning of the subarray. However, unit tests require to move your elements in a specific way such that the elements are swapped from the beginning to end. /// That is, when the subarray is sorted, the pivot index of that element will remain unchanged and will be the exact position of that value. /// the index of the pivot

public int Pivot(int[] array, int firstIndex, int lastIndex) {

2. We will first pivot our subarray to get the pivot index. Since the pivot will be fixed even after the array is sorted, we will partition the subarray into two halves: before the pivot and after the pivot. This will call recursively. /// /// /// /// ///

public void PartitionAndPivot(int[] array, int firstIndex, int lastIndex) {

3. Quick sort an array by invoking the PartitionAndPivot method on the entire array.

/// ///

public void QuickSort(int[] array) {

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!