Home › Forums › C Programming › please help me with bubbleSort argument › Re: Re: please help me
July 16, 2009 at 3:45 pm
#3590
GWILouisaxwzkla
Participant
Heres a bubble sort I wrote awhile ago ( hope this helps ):
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 | <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 /> |