Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Recursion (Quicksort) The recursive sorting technique called quicksort uses the following basic algorithm for a one-dimensional array of values: Partitioning Step: Take the first

C++ Recursion

(Quicksort)

The recursive sorting technique called quicksort uses the following basic algorithm

for a one-dimensional array of values:

Partitioning Step:

Take the first element of the unsorted array and determine its final

location in the sorted array (i.e., all values to the left of the element in the array are less

than the elements value, and all values to the right of the element in the array are

greater than the elements valuewe show how to do this below). We now have one

value in its proper location and two unsorted sub-arrays.

Recursion Step:

Perform the Partitioning Step on each unsorted sub-array.

Each time Step 1 is performed on a sub-array, another element is placed in its final location of

the sorted array, and two unsorted sub-arrays are created. When a sub-array consists of one

element, that sub-array must be sorted; therefore, that element is in its final location.

The basic algorithm seems simple enough, but how do we determine the final position of the

first element of each sub-array? As an example, consider the following set of values (the

element in bold is the partitioning elementit will be placed in its final location in the sorted

array):

37 2 6 4 89 8 10 12 68 45

Starting from the rightmost element of the array, compare each element with 37 until an element

less than 37 is found. Then swap 37 and that element. The first element less than 37 is 12, so

37 and 12 are swapped. The values now reside in the array as follows:

12 2 6 4 89 8 10 37 68 45

Element 12 is in italics to indicate that it was just swapped with 37.

Starting from the left of the array, but beginning with the element after 12, compare each

element with 37 until an element greater than 37 is found. Then swap 37 and that element. The

first element greater than 37 is 89, so 37 and 89 are swapped. The values now reside in the

array as follows:

12 2 6 4 37 8 10 89 68 45

Starting from the right, but beginning with the element before 89, compare each element with 37

until an element less than 37 is found. Then swap 37 and that element. The first element less

than 37 is 10, so 37 and 10 are swapped. The values now reside in the array as follows:

12 2 6 4 10 8 37 89 68 45

Starting from the left, but beginning with the element after 10, compare each element with 37

until an element greater than 37 is found. Then swap 37 and that element. There are no more

elements greater than 37, so when we compare 37 with itself, we know that 37 has been placed

in its final location of the sorted array.

Once the partition has been applied to the array, there are two unsorted sub-arrays. The sub-

array with values less than 37 contains 12, 2, 6, 4, 10 and 8. The sub-array with values greater

than 37 contains 89, 68 and 45. The sort continues with both sub-arrays being partitioned in the

same manner as the original array.

Based on the preceding discussion, write recursive function quickSort to sort a single-

subscripted integer array. The function should receive as arguments an integer array, a

starting subscript and an ending subscript. Function partition should be called by

quickSort to perform the partitioning step

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database And Transact SQL

Authors: Lucy Scott

1st Edition

1974679985, 978-1974679980

More Books

Students also viewed these Databases questions

Question

how would you have done things differently?

Answered: 1 week ago