ccplusplus.com
Learn C, C++ Concepts
Friday, January 13, 2012
sort numbers in ascending order c program
/************************************************************** * File : sort-number-ascending-order.c * Author : Saurabh Gupta * Description : sort numbers in ascending order c program * Date : AM 09:13 13 January 2012 * Source : http://www.ccplusplus.com/p/c.html * Note : add descending order sorting too **************************************************************/ #include
int swapNumber (int*, int*); int main() { int i, j; int nNumArray[10]; for ( i = 0; i < 10; i++ ) { printf (" Enter the number %d : ", i + 1 ); scanf ( "%d", &nNumArray[i] ); } printf (" Before sorting:\n" ); for ( i = 0; i < 10 ; i++ ) { printf ( " %d " , nNumArray[i] ); } printf ( "\n" ); for ( i = 0; i < 10; i++ ) { for ( j = 0; j < 10 - i - 1; j++ ) { if ( * ( nNumArray + j ) > * ( nNumArray + (j + 1) )) { swapNumber ( nNumArray + j , nNumArray + (j + 1) ); } } } printf ( "After sorting :\n" ); for ( i = 0 ; i < 10 ; i++) { printf ( "%d ", nNumArray [i] ); } printf ( "\n" ); return 0; } int swapNumber(int *npFirst,int *npSecond ) { int temp; temp = *npFirst; *npFirst = *npSecond; *npSecond = temp; return 0; } /* * OUTPUT * [sgupta@rhel6x64 c]$ c++ sort-number-ascending-order.c -o sort-number-ascending-order [sgupta@rhel6x64 c]$ ./sort-number-ascending-order Enter the number 1 : 10 Enter the number 2 : 9 Enter the number 3 : 2 Enter the number 4 : 4 Enter the number 5 : 0 Enter the number 6 : 12 Enter the number 7 : 44 Enter the number 8 : 22 Enter the number 9 : 6 Enter the number 10 : 7 Before sorting: 10 9 2 4 0 12 44 22 6 7 After sorting : 0 2 4 6 7 9 10 12 22 44 [sgupta@rhel6x64 c]$ */
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment