C ++ recursive functions

Source: Internet
Author: User

C ++ recursive functions

When it comes to recursive functions, we will think of recursive quick sorting.

1. What is quick sorting?

The basic idea of quick sorting is to divide the records to be sorted into two parts through quick sorting.
The records in one sub-series are smaller than the other sub-series. Then,
Sort two subsequences quickly.
We can see that the core of fast sorting is the molecular sequence.


2. Let's take a look at the idea of quick sorting using recursive functions:

(1) put the data to be sorted into an array (for example, array a []), with the subscript from low to high;

(2) perform the following operations on the array: Take an element value from the array as the pivot

Record (generally take element 0th, that is, a [0]) and store it in key (a variable;

(3) In array a, adjust the key to the appropriate position, and then compare the key
All the big elements are placed on the right of the key, and the number smaller than the key is placed on the key
To the left.

(4) The result of this sort is that the key splits the original array a
Two sub-arrays, the value on the left of the key is smaller than the key, and the value on the right is
The key is large.

(5) Now, the question is how to sort the two sub-arrays. Obviously
Call the above method for the two sub-arrays, that is, call the above method recursively,
Until the sorting is completed.

(6) Apparently, through the above process, when we finish processing all elements, the final result
Is: the element on the left of each element is not greater than this element, and the element on the right is not small
.


3. How to determine the key location?

After determining the key value, we use the key to divide array a into two subarrays.

Specific Method:

(1) Extract The 0th elements of the array and place them in the key as the demarcation value,
That is, key = a [0]; in this case, the array element a [0] is idle, and a left probe left is used.
Indicates that left = 0;
(2) Use the right probe to go from right to left, find elements smaller than the key, and find
Then, the value of this element is placed in the idle position indicated by left.
Indicates that the location is idle;
(3) Let the left probe left go to the right to find elements greater than the key.
The value of the element is placed in the idle position indicated by right. The position indicated by left
Changed to idle;
(4) Perform steps 2 and 3 cyclically until right is no longer greater than left (at this time,
It means that all elements are compared with keys ). Met by left and right
The location is the location of the key.


Specific Code:

# Include

Void QuickSort (int a [], int low, int high );
Void main ()
{
Int a [] = {32, 8, 65, 18, 19, 12, 61 };
Int I;

QuickSort (a, 0, 6 );

For (I = 0; I <7; I ++)
Printf ("% 4d", a [I]);
}

// Quick sorting of functions

Void QuickSort (int a [], int low, int high)
{
Int key;
Int left, right;
// If the column to be sorted is empty or has only one element, no sorting is required.
If (low> = high) return;
// 1 divide the records to be sorted into two subsequences
// 1.1 select the first element in the sequence as the axis record
Key = a [low];
// 1.2 divide the columns to be sorted into two subsequences Based on the axis record key
Left = low;
Right = high;

While (left <right)
{
While (left = Key)
Right --; // right scans forward from the back and skips the "big" element
A [left] = a [right]; // when a "small" element is encountered, move it to the front
While (left Left ++; // left scans from front to back and skips elements smaller than the axis record
A [right] = a [left]; // when a "big" element is encountered, move it to the back
}
// 1.3 place axis record
A [left] = key;
// 2. Sort two subsequences quickly
QuickSort (a, low, left-1 );
QuickSort (a, left + 1, high );
}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.