Home › Forums › C Programming › please help me with bubbleSort argument
- This topic has 3 replies, 2 voices, and was last updated 15 years, 4 months ago by HildegaBarnett.
Viewing 3 reply threads
- AuthorPosts
- July 15, 2009 at 2:24 am #2211HildegaBarnettParticipant
there is something wrong with bubbleSort argument
C12345678910111213141516171819202122232425262728293031#include <stdio.h>void bubbleSort(float arr[],int len){int isSorted=0;do{float temp;int i;isSorted=1;--len;for(i=0;i<len;i++)if(arr>arr[i+1]){isSorted=0;temp=arr;arr=arr[i+1];arr[i+1]=temp;}}while(!isSorted);}int main(){float r[]={10,9,8,7,6,5,4,3,2,1};int j=10,b;bubbleSort(r[j],j);for(b=0;b<10;b++)printf("%4.2fn",r);return 0;}can you help me? I am a rookie. thanks.
- July 15, 2009 at 6:40 am #3589HildegaBarnettParticipant
Ah,I can manage it myself. :P
in main function,”bubbleSort(r[j],j)”,there is no subscript operator”[]”.just “bubbleSort(r,j)”. - July 16, 2009 at 3:45 pm #3590GWILouisaxwzklaParticipant
Heres a bubble sort I wrote awhile ago ( hope this helps ):
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173<br /><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 < iostream ><br />//#include < string.h ><br />//#include < conio.h ><br />//#include < math.h ><br />//#include < iomanip ><br />//#include < ctype.h ><br />#include <stdlib.h><br />#include <time.h><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 /> - July 18, 2009 at 3:32 am #3591HildegaBarnettParticipant
Thanks,dman!
- AuthorPosts
Viewing 3 reply threads
- The forum ‘C Programming’ is closed to new topics and replies.