Home › Forums › C Programming › function › Reply To: function
September 4, 2008 at 11:36 am
#3433
Humayan
Participant
merge sort:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | /****************************************************************<br /> * File Name : c:programshelpmergeSort.cpp<br /> * Date : September,3,2008<br /> * Comments : new project<br /> * Compiler/Assembler :<br /> * Program Shell Generated At: 6:27:41 p.m.<br /> =-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /> <br /> #include < iostream ><br /> #include < stdlib.h ><br /> #include < time.h ><br /> //#include < math.h ><br /> //#include < iomanip ><br /> //#include < ctype.h ><br /> #define MAX_NUMBER 50<br /> #define ARRAY_SIZE 20<br /> using namespace std;<br /> <br /> //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@<br /> void mergeSort ( int * array , int * temp , int left , int right );<br /> void mergeSort ( int * array );<br /> void merge ( int * array , int * temp , int left , int right , int rightEnd );<br /> void initialize ( int * array , int size );<br /> //##################################################################################<br /> <br /> //main function ******************************<br /> int main ( )<br /> {<br /> int array [ ARRAY_SIZE ];<br /> initialize ( array , ARRAY_SIZE );<br /> int i = 0;<br /> while ( i < ARRAY_SIZE )<br /> {<br /> printf ( "%i" , array [ i ] );<br /> printf ( "n" );<br /> i ++;<br /> }<br /> mergeSort ( array );<br /> i = 0;<br /> while ( i < ARRAY_SIZE )<br /> {<br /> printf ( "%i" , array [ i ] );<br /> printf ( "n" );<br /> i ++;<br /> }<br /> return 0 ;<br /> }<br /> <br /> /******************************* FUNCTION DEFINITION ******************************<br /> Name : mergeSort<br /> Parameters :<br /> array a(n) int * ,<br /> temp a(n) int * ,<br /> left a(n) int ,<br /> right a(n) int<br /> <br /> Returns: Void type<br /> Comments:<br /> <br /> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br /> void mergeSort ( int * array , int * temp , int left , int right )<br /> {<br /> if ( left < right )<br /> {<br /> int center = ( left + right ) / 2;<br /> mergeSort ( array , temp , left , center );<br /> mergeSort ( array , temp , center + 1 , right );<br /> merge ( array , temp , left , center + 1 , right );<br /> }<br /> return;<br /> }<br /> /******************************* FUNCTION DEFINITION ******************************<br /> Name : mergeSort<br /> Parameters :<br /> array a(n) int *<br /> <br /> Returns: Void type<br /> Comments:<br /> <br /> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br /> void mergeSort ( int * array )<br /> {<br /> int temp [ ARRAY_SIZE ];<br /> mergeSort ( array , temp , 0 , ARRAY_SIZE - 1 );<br /> <br /> return;<br /> }<br /> /******************************* FUNCTION DEFINITION ******************************<br /> Name : merge<br /> Parameters :<br /> array a(n) int * ,<br /> temp a(n) int * ,<br /> left a(n) int ,<br /> right a(n) int ,<br /> rightEnd ( rightEnd )<br /> <br /> Returns: Void type<br /> Comments:<br /> <br /> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br /> void merge ( int * array , int * temp , int left , int right , int rightEnd )<br /> {<br /> int leftEnd = right - 1;<br /> int tempPos = left;<br /> int numberElements = rightEnd - left + 1;<br /> <br /> while ( left <= leftEnd && right <= rightEnd )<br /> if ( array [ left ] <= array [ right ] )<br /> temp [ tempPos ++ ] = array [ left ++ ];<br /> else<br /> temp [ tempPos ++ ] = array [ right ++ ];<br /> while ( left <= leftEnd )<br /> temp [ tempPos ++ ] = array [ left ++ ];<br /> <br /> while ( right <= rightEnd )<br /> temp [ tempPos ++ ] = array [ right ++ ];<br /> <br /> int i = 0;<br /> while ( i < numberElements )<br /> {<br /> array [ rightEnd ] = temp [ rightEnd ] ;<br /> rightEnd --;<br /> }<br /> <br /> return;<br /> }<br /> /******************************* FUNCTION DEFINITION ******************************<br /> Name : test<br /> Parameters :<br /> array a(n) int * ,<br /> size a(n) int<br /> <br /> Returns: Void type<br /> Comments:<br /> <br /> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br /> void initialize ( int * array , int size )<br /> {<br /> <br /> srand ( time ( NULL ) );<br /> int i = 0;<br /> while ( i < size )<br /> {<br /> array [ i ] = rand () % MAX_NUMBER;<br /> i ++;<br /> } <br /> <br /> return;<br /> }<br /> |