These days I have been troubled with the "backtracking algorithm", it's so complicated to me that i can't get it through in a short time. However during the study of "backtracking algorithm", i picked up some knowledge on the classical " Divide and Conquer" Algorithm.
Let's have a look at it.
The divide-and-conquer design paradigm
1.Dividethe problem (instance) into subproblems.
2.Conquerthe subproblemsby solving them recursively.
3.Combinesubproblemsolutions.
By using this d-a-q, we can get save a lot of time,which means we can decrease the
After having a outline of the algorithm, we should practice some exercises.
1. Find the maximum in the given integer array ( using recursive function here ) .
If you haven't know the Divide-and-Conquer, you may write this function like this:
int Max1( int a[], int length ) {
if( length==0) return a[0];
int temp= Max1( a, length-1);
return a[length]>temp ? a[length] : temp;
}
The T(n) for this function is T( n ); ( we mean it takes linear running time ).
But if you have known the Divide-and-Conquer, we have a more effective function which have a running time as T( lg n );
int Max2( int a[], int b, int e ) {
if( b==e ) return a[b];
int temp1 = Max2( a, b, (b+e)/2 ) ;
int temp2 = Max2( a, (b+e)/2 +1, e);
return temp1 >temp2?temp1:temp2;
}
2. Find the average number of an integer array.
After we have seen above, we can write the divide-and-conquer version of function easiy like this:
float Average( int a[], int b, int e ) {
if( b==e ) return a[b];
float temp1 = Average( a, b, (b+e)/2 ) ;
float temp2 = Average( a, (b+e)/2 +1, e);
return 1.0*( temp1+temp2)/2;
}
3.Use the method above to solve the Quick_Sort problem.
We should analyze the three steps like:
-
Divide: Partition (rearrange) the array A[p ‥ r] into two (possibly empty) subarrays A[p ‥ q - 1] and A[q + 1 ‥ r] such that each element of A[p ‥ q - 1] is less than or equal to A[q], which is, in turn, less than or equal to each element of A[q + 1 ‥ r]. Compute the index q as part of this partitioning procedure.
-
Conquer: Sort the two subarrays A[p ‥ q -1] and A[q +1 ‥ r] by recursive calls to quicksort.
-
Combine: Since the subarrays are sorted in place, no work is needed to combine them: the entire array A[p ‥ r] is now sorted.
The following procedure implements quicksort.
QUICKSORT(A, p, r)
1 if p < r
2 then q ← PARTITION(A, p, r)
3 QUICKSORT(A, p, q - 1)
4 QUICKSORT(A, q + 1, r)
To sort an entire array A, the initial call is QUICKSORT(A, 1, length[A]).
Partitioning the array
The key to the algorithm is the PARTITION procedure, which rearranges the subarray A[p ‥ r] in place.
PARTITION(A, p, r) 1 x ← A[r]
2 i ← p - 1
3 for j ← p to r - 1
4 do if A[j] ≤ x 5 then i ← i + 1
6 exchange A[i] ↔ A[j]
7 exchange A[i + 1] ↔ A[r]
8 return i + 1
( The material above is from the book "Introduction to Algorithm"
#include <iostream>
using namespace std;
void swap( int &a, int & b ) {
int temp = a;
a = b;
b = temp;
}
int partition(int a[],int begin,int end) {
int i = begin-1;
for( int j = begin; j<end; ++j ) {
if( a[j]<=a[end] ) {
i++;
if( a[j-1] > a[end] ) swap( a[i], a[j] ) ;}
}
swap( a[i+1], a[end]);
return i+1;
}
void quick_sort ( int a[], int begin, int end ) {
if( begin<end ) {
int pivot = partition( a, begin, end );
quick_sort( a, begin, pivot-1 );
quick_sort( a, pivot+1, end );
}
}
void print( int a[], int length ) {
for ( int i = 0; i<length; ++i )
cout<<a[i]<<" "<<flush;
cout<<endl;
}
void main() {
int arr[7] = { 5, 1, 8, 2, 45, 5, 8 };
quick_sort( arr, 0, 6 );
print( arr ,7 );
system ( "pause" );
}