Home › Forums › C Programming › Algorithms In C++ (Urgently needed)
- This topic has 1 reply, 2 voices, and was last updated 15 years, 5 months ago by GWILouisaxwzkla.
Viewing 1 reply thread
- AuthorPosts
- June 2, 2009 at 10:38 am #2203DeloresCarrolParticipant
I need Urgently help on my assignment please solved these questions I will be very thankful to you.
Q1 Implement the binary search & sequential search algorithms is C language.
Use these values to search 8 from them. 12,13,8,11,9,4,16Q2 Write a program in C language that can insert an element at the start, and end
with a doubly link list.Q3 Write a manual steps to sort the given data by Bubble sort. Values are
12,1,9,10,5,4,16,11,8Q4 Perform the manual steps of insertion sort, bubble sort and selection sort for the
following values 9,12,15,1,2,14,10,11 - June 2, 2009 at 6:18 pm #3579GWILouisaxwzklaParticipant
first part:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132<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:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207<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:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174<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:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174<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 />
- AuthorPosts
Viewing 1 reply thread
- The forum ‘C Programming’ is closed to new topics and replies.