Home › Forums › C Programming › Algorithms In C++ (Urgently needed) › Re: Re: Algorithms In C++ (Urgently needed)
June 2, 2009 at 6:18 pm
#3579
GWILouisaxwzkla
Participant
first part:
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 | <br /> /****************************************************************<br /> * File Name : c:programstempCG.cpp<br /> * Date : June,2,2009<br /> * Comments : new project<br /> * Compiler/Assembler :<br /> * Modifications :<br /> *<br /> *<br /> *<br /> *<br /> *<br /> * Program Shell Generated At: 12:54:06 p.m.<br /> =-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /> <br /> <br /> #include <br /> //#include <br /> //#include <br /> //#include <br /> //#include <br /> //#include <br /> <br /> //using namespace std;<br /> <br /> <br /> //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@<br /> <br /> int binarySearch ( int * array , int itemToFind , int maxItems );<br /> int sequentialSearch ( int * array , int itemToFind , int maxItems );<br /> <br /> //##################################################################################<br /> <br /> <br /> //main function ******************************<br /> <br /> int main ( )<br /> {<br /> int array [ 10 ] = { 1 , 2 , 3 , 4 , 5 , 6 ,7 , 8 , 9 , 10 };<br /> int indexFoundAt;<br /> int itemToFind ;<br /> <br /> printf ( "enter an item to find ( 1 to 10 ) n" );<br /> scanf ( "%i" , & itemToFind );<br /> <br /> <br /> <br /> indexFoundAt = binarySearch ( array , itemToFind , 10 );<br /> printf ( "The item %i was found at index %i n" , itemToFind , indexFoundAt );<br /> indexFoundAt = sequentialSearch ( array , itemToFind , 10 );<br /> printf ( "The item %i was found at index %i n" , itemToFind , indexFoundAt );<br /> return 0 ;<br /> }<br /> <br /> <br /> /******************************* FUNCTION DEFINITION ******************************<br /> <br /> Name : binarySearch<br /> Parameters :<br /> <br /> array a(n) int * ,<br /> maxItems a(n) int<br /> <br /> <br /> Returns: Void type<br /> Comments:<br /> <br /> <br /> <br /> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br /> int binarySearch ( int * array , int itemToFind , int maxItems )<br /> {<br /> <br /> int left , right;<br /> int midPoint = ( ( maxItems - 1 ) + 0 ) / 2;<br /> <br /> <br /> left = 0;<br /> right = maxItems - 1;<br /> <br /> while ( array [ midPoint ] != itemToFind && right > left )<br /> {<br /> if ( array [ midPoint ] > itemToFind )<br /> {<br /> right = midPoint - 1;<br /> midPoint = ( right + left ) / 2;<br /> }<br /> else<br /> {<br /> left = midPoint + 1;<br /> midPoint = ( right + left ) / 2;<br /> }<br /> <br /> }<br /> if ( array [ midPoint ] == itemToFind )<br /> return midPoint;<br /> else<br /> return -1; //not found<br /> }<br /> /******************************* FUNCTION DEFINITION ******************************<br /> <br /> Name : sequentialSearch<br /> Parameters :<br /> <br /> array a(n) int * ,<br /> maxItems a(n) int<br /> <br /> <br /> Returns: Void type<br /> Comments:<br /> <br /> <br /> <br /> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br /> int sequentialSearch ( int * array , int itemToFind , int maxItems )<br /> {<br /> <br /> int i = 0;<br /> <br /> while ( i < maxItems && array [ i ] != itemToFind )<br /> i ++;<br /> <br /> if ( array [ i ] == itemToFind )<br /> return i;<br /> else<br /> return -1; //not found<br /> }<br /> <br /> <br /> <br /> <br /> |
second part:
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | <br /> /****************************************************************<br /> * File Name : c:programstempCG.cpp<br /> * Date : June,2,2009<br /> * Comments : new project<br /> * Compiler/Assembler : Visual C++ 6.0<br /> * Modifications :<br /> *<br /> *<br /> *<br /> *<br /> *<br /> * Program Shell Generated At: 1:42:17 p.m.<br /> =-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /> <br /> <br /> #include <br /> #include <br /> //#include <br /> //#include <br /> //#include <br /> //#include <br /> <br /> //using namespace std;<br /> <br /> struct node<br /> {<br /> <br /> int data;<br /> node * prior;<br /> node * next;<br /> };<br /> <br /> <br /> <br /> //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@<br /> <br /> void addNodeFront ( int item , node ** frontNode , node ** backNode );<br /> void addNodeBack ( int item , node ** frontNode , node ** backNode );<br /> void printList ( node * front );<br /> void destroyList ( node * front );<br /> <br /> //##################################################################################<br /> <br /> <br /> //main function ******************************<br /> <br /> int main ( )<br /> {<br /> <br /> node * front = 0;<br /> node * back = 0;<br /> <br /> <br /> addNodeFront ( 1 , & front , & back );<br /> addNodeBack ( 2 , & back , & back );<br /> printList ( front );<br /> destroyList ( front );<br /> return 0 ;<br /> }<br /> <br /> <br /> /******************************* FUNCTION DEFINITION ******************************<br /> <br /> Name : addNodeFront<br /> Parameters :<br /> <br /> item a(n) int ,<br /> frontNode a(n) node ** ( node ** )<br /> <br /> <br /> Returns: Void type<br /> Comments:<br /> <br /> <br /> <br /> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br /> void addNodeFront ( int item , node ** frontNode , node ** backNode )<br /> {<br /> <br /> node * temp = ( node * ) malloc ( sizeof ( node ) );<br /> if ( temp == 0 )<br /> {<br /> printf ( "allocation error n " );<br /> return;<br /> }<br /> <br /> if ( * frontNode == 0 ) //empty list<br /> {<br /> temp -> data = item;<br /> temp -> prior = 0;<br /> temp -> next = 0;<br /> * frontNode = temp;<br /> * backNode = temp;<br /> }<br /> else //one node<br /> {<br /> temp -> data = item;<br /> temp -> next = * frontNode;<br /> ( * frontNode ) -> prior = temp;<br /> temp -> prior = 0;<br /> * frontNode = temp;<br /> }<br /> return;<br /> }<br /> /******************************* FUNCTION DEFINITION ******************************<br /> <br /> Name : addNodeBack<br /> Parameters :<br /> <br /> item a(n) int ,<br /> backNode a(n) node ** ( node ** )<br /> <br /> <br /> Returns: Void type<br /> Comments:<br /> <br /> <br /> <br /> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br /> void addNodeBack ( int item , node ** frontNode , node ** backNode )<br /> {<br /> <br /> node * temp = ( node * ) malloc ( sizeof ( node ) );<br /> if ( temp == 0 )<br /> {<br /> printf ( "allocation error n " );<br /> return;<br /> }<br /> <br /> if ( * frontNode == 0 ) //empty list<br /> {<br /> temp -> data = item;<br /> temp -> prior = 0;<br /> temp -> next = 0;<br /> * frontNode = temp;<br /> * backNode = temp;<br /> }<br /> else //one node<br /> {<br /> temp -> data = item;<br /> temp -> next = 0;<br /> ( * backNode ) -> next = temp;<br /> temp -> prior = * backNode;<br /> * backNode = temp;<br /> }<br /> return;<br /> <br /> <br /> }<br /> /******************************* FUNCTION DEFINITION ******************************<br /> <br /> Name : printList<br /> Parameters :<br /> <br /> front a(n) node * ( node * )<br /> <br /> <br /> Returns: Void type<br /> Comments:<br /> <br /> <br /> <br /> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br /> void printList ( node * front )<br /> {<br /> <br /> node * temp = front;<br /> <br /> while ( temp != 0 )<br /> {<br /> printf ("%i " , temp -> data , " " );<br /> temp = temp -> next;<br /> <br /> }<br /> return;<br /> }<br /> /******************************* FUNCTION DEFINITION ******************************<br /> <br /> Name : destroyList<br /> Parameters :<br /> <br /> front a(n) node * ( node * )<br /> <br /> <br /> Returns: Void type<br /> Comments:<br /> <br /> <br /> <br /> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br /> void destroyList ( node * front )<br /> {<br /> <br /> node * temp = front;<br /> <br /> while ( temp != 0 )<br /> {<br /> front = temp -> next;<br /> free ( temp );<br /> temp = front;<br /> }<br /> <br /> return;<br /> }<br /> <br /> |
bubble 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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | <br /> /****************************************************************<br /> * File Name : c:programstempCG.cpp<br /> * Date : March,28,2009<br /> * Comments : new project<br /> * Compiler/Assembler : Visual C++ 6.0<br /> * Modifications :<br /> *<br /> * Notes: A good sort for a small number of items. Total run time<br /> * is proportional to the number of items squared ( o ( N^2 ) ).<br /> *<br /> *<br /> * Program Shell Generated At: 11:25:48 a.m.<br /> =-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /> <br /> <br /> #include <br /> //#include <br /> //#include <br /> //#include <br /> //#include <br /> //#include <br /> #include <br /> #include <br /> <br /> using namespace std;<br /> <br /> //>>>>>>>>>>>>>>>>>>>>>>>> GLOBAL DATA <<<<<<<<<<<<<<<<<<<<<<<<<<br /> const int MAX_NUMBERS = 50;<br /> int NUMBERS_PER_LINE = 15;<br /> //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<br /> <br /> <br /> <br /> <br /> //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@<br /> <br /> void generateNumbers ( int * numbers , int numberItems );<br /> void bubbleSort ( int * array , int numberItems );<br /> void printArray ( int * array , int numberItems );<br /> <br /> //##################################################################################<br /> <br /> <br /> //main function ******************************<br /> <br /> int main ( )<br /> {<br /> <br /> int array [ MAX_NUMBERS ];<br /> generateNumbers ( array , MAX_NUMBERS );<br /> cout << endl << endl;<br /> cout << "unsorted array " << endl;<br /> printArray ( array , MAX_NUMBERS );<br /> bubbleSort ( array , MAX_NUMBERS );<br /> cout << endl << endl << "sorted array: " << endl;<br /> printArray ( array , MAX_NUMBERS );<br /> cout << endl;<br /> <br /> <br /> return 0 ;<br /> }<br /> <br /> <br /> /******************************* FUNCTION DEFINITION ******************************<br /> <br /> Name : bubbleSort<br /> Parameters :<br /> <br /> array a(n) int * ,<br /> numberItems a(n) int<br /> <br /> <br /> Returns: Void type<br /> Comments:<br /> <br /> <br /> <br /> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br /> void bubbleSort ( int * array , int numberItems )<br /> {<br /> <br /> int endSortedArray = numberItems - 1;<br /> int lastSwapIndex;<br /> int temp;<br /> while ( endSortedArray > 0 ) //while not at the end of the unsorted array<br /> {<br /> lastSwapIndex = 0; //save index of the last item swapped<br /> int i = 0; //start at the beggining of the unsorted array<br /> while ( i < endSortedArray ) //while not in the sorted items<br /> {<br /> if ( array [ i ] > array [ i + 1 ] ) //if current item is smaller than next , bubble up<br /> {<br /> //swap array [ i ] and array [ i + 1 ]<br /> temp = array [ i ];<br /> array [ i ] = array [ i + 1 ];<br /> array [ i + 1 ] = temp;<br /> lastSwapIndex = i;<br /> }<br /> i ++;<br /> }<br /> endSortedArray = lastSwapIndex; //reset swap boundry<br /> }<br /> <br /> <br /> <br /> return;<br /> }<br /> /******************************* FUNCTION DEFINITION ******************************<br /> <br /> Name : printArray<br /> Parameters :<br /> <br /> array a(n) int * ,<br /> numberItems a(n) int<br /> <br /> <br /> Returns: Void type<br /> Comments:<br /> <br /> <br /> <br /> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br /> void printArray ( int * array , int numberItems )<br /> {<br /> <br /> <br /> int i = 0;<br /> cout << endl << endl;<br /> cout << "array : " << endl;<br /> while ( i < numberItems )<br /> {<br /> cout << array [ i ] << " " ;<br /> if ( i != 0 )<br /> if ( i % NUMBERS_PER_LINE == 0 )<br /> cout << endl;<br /> i ++;<br /> }<br /> <br /> return;<br /> }<br /> /******************************* FUNCTION DEFINITION ******************************<br /> <br /> Name : generateNumbers<br /> Parameters :<br /> <br /> numbers a(n) int ,<br /> numberItems a(n) int<br /> <br /> <br /> Returns: Void type<br /> Comments:<br /> <br /> <br /> <br /> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br /> void generateNumbers ( int * numbers , int numberItems )<br /> {<br /> <br /> int i = 0;<br /> <br /> srand ( time ( NULL ) );<br /> while ( i < numberItems )<br /> {<br /> numbers [ i ] = rand () % MAX_NUMBERS;<br /> i ++;<br /> }<br /> <br /> <br /> return;<br /> }<br /> <br /> <br /> |
selection 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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | <br /> /****************************************************************<br /> * File Name : c:programstempCG.cpp<br /> * Date : March,28,2009<br /> * Comments : new project<br /> * Compiler/Assembler : Visual C++ 6.0<br /> * Modifications :<br /> *<br /> * Notes: A good sort for a small number of items. Total run time<br /> * is proportional to the number of items squared ( o ( N^2 ) ).<br /> *<br /> *<br /> * Program Shell Generated At: 11:25:48 a.m.<br /> =-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /> <br /> <br /> #include <br /> //#include <br /> //#include <br /> //#include <br /> //#include <br /> //#include <br /> #include <br /> #include <br /> <br /> using namespace std;<br /> <br /> //>>>>>>>>>>>>>>>>>>>>>>>> GLOBAL DATA <<<<<<<<<<<<<<<<<<<<<<<<<<br /> const int MAX_NUMBERS = 50;<br /> int NUMBERS_PER_LINE = 15;<br /> //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<br /> <br /> <br /> <br /> <br /> //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@<br /> <br /> void generateNumbers ( int * numbers , int numberItems );<br /> void selectionSort ( int * array , int numberItems );<br /> void printArray ( int * array , int numberItems );<br /> <br /> //##################################################################################<br /> <br /> <br /> //main function ******************************<br /> <br /> int main ( )<br /> {<br /> <br /> int array [ MAX_NUMBERS ];<br /> generateNumbers ( array , MAX_NUMBERS );<br /> cout << endl << endl;<br /> cout << "unsorted array " << endl;<br /> printArray ( array , MAX_NUMBERS );<br /> selectionSort ( array , MAX_NUMBERS );<br /> cout << "sorted array: " << endl;<br /> printArray ( array , MAX_NUMBERS );<br /> cout << endl;<br /> <br /> <br /> return 0 ;<br /> }<br /> <br /> <br /> /******************************* FUNCTION DEFINITION ******************************<br /> <br /> Name : selectionSort<br /> Parameters :<br /> <br /> array a(n) int * ,<br /> numberItems a(n) int<br /> <br /> <br /> Returns: Void type<br /> Comments: Slow n^2 algorithm<br /> <br /> <br /> <br /> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br /> void selectionSort ( int * array , int numberItems )<br /> {<br /> <br /> int i = 0 , j , minimumIndex , temp;<br /> <br /> while ( i <= numberItems )<br /> {<br /> minimumIndex = i; //select i to be smallest index<br /> j = minimumIndex + 1; //start sorting at next index after<br /> while ( j <= numberItems )<br /> {<br /> if ( array [ j ] < array [ minimumIndex ] ) //if we have found a smaller than minimum<br /> { //element , mark current as smallest<br /> minimumIndex = j;<br /> }<br /> j ++; //goto next item in the array<br /> }<br /> if ( minimumIndex != numberItems ) //if we found a smaller item put it at the front<br /> { //of the sorted array<br /> temp = array [ minimumIndex ]; //swap items<br /> array [ minimumIndex ] = array [ i ];<br /> array [ i ] = temp;<br /> }<br /> i ++; //goto next item in the array<br /> }<br /> <br /> <br /> return;<br /> }<br /> /******************************* FUNCTION DEFINITION ******************************<br /> <br /> Name : printArray<br /> Parameters :<br /> <br /> array a(n) int * ,<br /> numberItems a(n) int<br /> <br /> <br /> Returns: Void type<br /> Comments:<br /> <br /> <br /> <br /> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br /> void printArray ( int * array , int numberItems )<br /> {<br /> <br /> <br /> int i = 0;<br /> cout << endl << endl;<br /> cout << "array : " << endl;<br /> while ( i < numberItems )<br /> {<br /> cout << array [ i ] << " " ;<br /> if ( i != 0 )<br /> if ( i % NUMBERS_PER_LINE == 0 )<br /> cout << endl;<br /> i ++;<br /> }<br /> <br /> return;<br /> }<br /> /******************************* FUNCTION DEFINITION ******************************<br /> <br /> Name : generateNumbers<br /> Parameters :<br /> <br /> numbers a(n) int ,<br /> numberItems a(n) int<br /> <br /> <br /> Returns: Void type<br /> Comments: Generates an array of random numbers.<br /> <br /> <br /> <br /> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br /> void generateNumbers ( int * numbers , int numberItems )<br /> {<br /> <br /> int i = 0;<br /> <br /> srand ( time ( NULL ) );<br /> while ( i < numberItems )<br /> {<br /> numbers [ i ] = rand () % MAX_NUMBERS;<br /> i ++;<br /> }<br /> <br /> <br /> return;<br /> }<br /> <br /> <br /> |