您的位置:首页 > 其它

merge sort and binary search recursive version

2012-09-21 00:03 323 查看
// Merge_Sort.cpp : Defines the entry point for the console application.

/*****************************************************************

*

* 2. have to adopt the same array index to C++ programming lauguage.

* notice the index of the first item is 0, and the last item is array.length-1

* 3. procedure merge_check_empty() check which subarray is empty.

* date: Sep./16/2012

*

* 4. binary search recursive version: in BUGs

* data: Sep./19/2012

*

* 5. binary search recursive version: correct

* 6. recursion procedure must consider atomic case in any time,

* becuase the terminal case of recursion is an atomic case. don't omit it.

* That is terminal condition

* data: Sep./20/2012

*

* Author: Mr. Rocky CHEN

*******************************************************************/

#include "stdafx.h"

#include <iostream>

#include <ctime>

#include <cstdlib>

using namespace std;

#define SENTINEL 9999

int _tmain(int argc, _TCHAR* argv[])

{

void random_array(int * const a, int length);//yield a ramdom array for sorting

void merge(int * const array_, int start_num, int mid_num, int end_num); //merge two array to an array

void print_array(int * const a, int length);

void merge_sort(int * const a, int star, int end);

void merge_check_empty(int * const a, int s, int m, int e);

int binary_search(int* const a, int s, int e, int key); //s=star, e=end, key = search value

int const length = 10; //array length

int a[length];

srand(unsigned (time(0)));

random_array(a, length); //yield

merge_sort(a,0, length-1);

print_array(a,length);

for(int i=0; i <10; i++)

{

cout<<"=============\n i = " << i << endl;

int position = binary_search(a,0,length-1,a[i]);

cout << "return position = " << position << endl;

}

system("pause");

return 0;

}

//yield a ramdom array for sorting

void random_array(int * const a, int length)

{

for (int i = 0; i < length; i++)

{

a[i] = rand()%30;

cout << "a[" << i <<"] = " << a[i] << endl;

}

}

void print_array(int * const a, int length)

{

cout << "==========" << endl;

for (int i = 0; i < length; i++)

{

cout << "a[" << i <<"] = " << a[i] << endl;

}

}

// subroutine "merge" in merge sort

void merge(int * const a, int s, int m, int e) //s=start, m=middle, e=end

{

//calculate the lengths of two subarray

int n1 = m-s+1; //

int n2 = e-m;

int * const larray = (int *) malloc((n1+1)* sizeof(int)); // how to repalce malloc by new******

int * const rarray = (int *) malloc((n2+1)* sizeof(int)); //******

//copy two subarray of a[]

for(int i = 0; i < n1; i++) larray[i] = a[s+i];

for(int i = 0; i < n2; i++) rarray[i] = a[m+1+i];

//set sentinel card

larray[n1] = rarray[n2] = SENTINEL;

print_array(larray,n1+1);

print_array(rarray,n2+1);

for(int i=0, j=0, k=s; k <= e ; k++) //copy smaller item between larray[i] and rarray[j] to a[]

{

if(larray[i] <= rarray[j])

a[k] = larray[i++]; //i++

else

a[k] = rarray[j++]; //j++

}

}

// have the same function to merge(), but don't use sentinels in merging.

void merge_check_empty(int * const a, int s, int m, int e)

{

//calculate length of two subarraies

int n1 = m-s +1;

int n2 = e-m;

//copy elements of array to two subarraies

int *larray = (int *) malloc(n1*sizeof(int));

int *rarray = (int *) malloc(n2*sizeof(int));

for(int i = 0; i< n1; i++) larray[i] = a[s+i];

for(int i =0; i< n2; i++) rarray[i] = a[m+1+i];

//pick up the smaller card on tops of two piles cards; if one subarray is empty, copy the remain cards directly.

for(int i=0, j=0, k=s; k <= e; k++)

{

if(i < n1 && j < n2 && larray[i] <= rarray[j])

a[k] = larray[i++];

else if( i < n1 && j < n2 && larray[i] > rarray[j])

a[k] = rarray[j++];

else if (i == n1) //larray is empty

a[k] = rarray[j++];

else // if the former three conditions are false, j must equals n2, then rarray is empty

a[k] = larray[i++];

}

}

void merge_sort(int * const a, int star, int end)

{

if(star < end)

{

int middle = (star+end)/2; //middle = (star+end)/2*******

merge_sort(a,star, middle);

merge_sort(a,middle+1,end);

//using different merge function to complete meger_sort

merge_check_empty(a,star,middle,end);

//merge(a,star,middle,end);

}

}

//binary search recursive version

int binary_search(int* const a, int s, int e, int key) //s=star, e=end, key = search value

{

//when the length of array is more than 1. don't use s <= e, because when array's length = 1, s=m=e,

//if a[m]!=key, it results in deadloop or pointer overflow.

if(s < e)

{

int m = (s+e)/2; //m = middle pivot

if (a[m]==key) // accelerate to terminate

{

cout << "position = " << m << " a[position] = " << a[m] << " key = " << key << endl;

return m;

}

else if( a[m] > key) binary_search(a,s,m,key); //search key in the left subarray

else binary_search(a,m+1,e,key); //sarch key in the right subarray

}

// s=e. recursion procedure must consider atomic case in any time,

//becuase the terminal case of recursion is an atomic case. don't omit it.

else

{

if(a[s] == key)

{

cout << "position = " << s << " a[position] = " << a[s] << " key = " << key << endl;

return s;

}

else return -1;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: