Home › Forums › C Programming › beginners needs help › Re: Re: beginners needs help
January 2, 2010 at 9:40 pm
#3619
GWILouisaxwzkla
Participant
Don’t know what type of sort you need to use but heres one with 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 | /**************************************************************** * File Name : c:programstempCG.cpp * Date : December,29,2009 * Comments : new project * Compiler/Assembler : Visual C++ 6.0 * Modifications : * * * * * * Program Shell Generated At: 11:37:28 a.m. =-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/ #include < iostream > //#include < string.h > //#include < conio.h > //#include < math.h > //#include < iomanip > //#include < ctype.h > using namespace std; void bubbleSort ( int * array , int numberItems ); //main function ****************************** const int MAX_NUMBERS = 20; int main ( ) { int input; int numberOfInputItems; int numberCount = 0; int numbers [ MAX_NUMBERS ]; cout << "Enter number of items to input: "; cin >> numberOfInputItems; int i = 0; while ( i < numberOfInputItems ) { cout << "enter a number: "; cin >> numbers [ i ] ; if ( numbers [ i ] == 1 || numbers [ i ] == 4 || numbers [ i ] == 5 || numbers [ i ] == 12 || numbers [ i ] || numbers [ i ] == 3 ) numberCount ++; i ++; } cout << endl; cout << "The count was " << numberCount << endl << endl; bubbleSort ( numbers , i ); int j = 0; while ( j < i ) { cout << numbers [ j ] << " "; j ++; } } /******************************* FUNCTION DEFINITION ****************************** Name : bubbleSort Parameters : array a(n) int * , numberItems a(n) int Returns: Void type Comments: ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ void bubbleSort ( int * array , int numberItems ) { int endSortedArray = numberItems - 1; int lastSwapIndex; int temp; while ( endSortedArray > 0 ) //while not at the end of the unsorted array { lastSwapIndex = 0; //save index of the last item swapped int i = 0; //start at the beggining of the unsorted array while ( i < endSortedArray ) //while not in the sorted items { if ( array [ i ] > array [ i + 1 ] ) //if current item is smaller than next , bubble up { //swap array [ i ] and array [ i + 1 ] temp = array [ i ]; array [ i ] = array [ i + 1 ]; array [ i + 1 ] = temp; lastSwapIndex = i; } i ++; } endSortedArray = lastSwapIndex; //reset swap boundry } return; } |