in Algorithms retagged by
534 views
0 votes
0 votes
Sort the following array using quicksort algorithm. [40,11,4,72,17,2,49]
in Algorithms retagged by
534 views

2 Answers

0 votes
0 votes
Best answer
To sort the given array [40, 11, 4, 72, 17, 2, 49] using the quicksort algorithm, we'll follow these steps:

1. Choose a pivot element from the array. In this case, let's choose the last element, 49, as the pivot.

2. Partition the array around the pivot. Rearrange the elements such that all elements smaller than the pivot are placed before it, and all elements greater than the pivot are placed after it. This step ensures that the pivot element is in its final sorted position.

   After partitioning, the array may look like: [11, 4, 17, 2, 40, 49, 72]

3. Recursively apply steps 1 and 2 to the sub-arrays on both sides of the pivot (i.e., the sub-array before the pivot and the sub-array after the pivot). Repeat these steps until the sub-arrays have only one element or are empty.

   Applying quicksort to the sub-array before the pivot: [11, 4, 17, 2, 40]
   After partitioning: [4, 2, 11, 17, 40]

   Applying quicksort to the sub-array after the pivot: [72]
   After partitioning: [72]

4. Once the recursive calls have completed and all sub-arrays are sorted, the entire array will be sorted.

   Final sorted array: [2, 4, 11, 17, 40, 49, 72]

Therefore, using the quicksort algorithm, the given array [40, 11, 4, 72, 17, 2, 49] is sorted as [2, 4, 11, 17, 40, 49, 72].
selected by

1 comment

Thank you
0
0
0 votes
0 votes

Answer is :

1 comment

Thank you
0
0