A detailed explanation of C + + recursive functions

Source: Internet
Author: User

When it comes to recursive functions, you'll think of a quick sort of recursion.

1. What is fast sorting?

The basic idea of quick sorting is to divide the pending records into two by a quick sort
A subsequence in which the records in one child series are smaller than the other, and then
The two sub-sequences are sorted in a quick order.
It can be seen that the core of a fast sort is the sequence of molecules.


2. Let's look at the idea of a recursive function for quick ordering:

(1) The data to be sorted into an array (such as a[]), subscript from low to high;

(2) The array is performed as follows: Take an element value from the arrays as a pivot

Record (usually take the No. 0 element, i.e. a[0]) and deposit into key (a variable);

(3) In array A, the key is adjusted to the appropriate position, then the key
The large elements are placed on the right side of the key, and the number smaller than the key is placed on the key
to the left.

(4) The result of this sequencing is that key divides the original array A into
Two sub-arrays, the value on the left of key is smaller than key, and the right side is more than
Key is large.

(5) At this point, the question becomes how to sort the two sub-arrays, apparently
Call the above method on these two sub-arrays, that is, recursively call the above method,
Until the sort is complete.

(6) Obviously, through the above process, when we have finished processing all the elements, the final result
is: the element to the left of each element is not more than the element, and the right element is not small
To that element.


3. How to determine the location of key?

After we have determined the value of key, we use key to divide the array a into two sub-arrays.

Specific methods:

(1) Take the No. 0 element of the array and place it in the key as a cutoff value,
That is, key = A[0]; At this point, the array element a[0] is idle, with a left probe
indication, i.e. left = 0;
(2) Use right probe to right to left, look for elements less than key, find
Then the value of the element is placed in the idle position indicated by the left, and right
Indicates that the position becomes an idle position;
(3) Leave the left probe to the right, looking for elements larger than key, and find the
The value of the element is placed in the idle position indicated by right. Position indicated by left at this time
Become idle;
(4) Loop through 2, 3 steps until right is no longer greater than left (at this point,
means that all elements are compared to key). The left and right meet
Place is the location of key.


The specific code:

#include <stdio.h>

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]);
}

Quicksort method to implement fast ordering of functions

void QuickSort (int a[],  int Low,  int high)
{   
int key;
     int left, right;
     //If the sort column is empty or has only one element, you do not need to sort
     if (low >= high)  return;
      //1   divide pending records into two sub-sequences
     //1.1   Select first element in sequence as axis record
     key = A[low];
     //1.2   Divide the pending sequence into two sub-sequences according to the Axis record key
      left = low; 
right = high;

while (left < right)
{
while (Left<right && a[right] >= key)
right--; Right to scan forward from behind, skipping "big" elements
A[left] = A[right]; Encounter the "small" element and move to the front
while (Left<right && A[left] <= key)
left++; Left front scan backward, skipping elements smaller than axis record
A[right] = A[left]; Hit the "big" element and move to the back.
}
1.3 Placing Axis Records
A[left] = key;
2. Quickly sort the two sub-sequences separately
QuickSort (A, low, left-1);
QuickSort (A, left+1, high);
}





A detailed explanation of C + + recursive functions

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.