ccplusplus.com
Learn C, C++ Concepts
Friday, January 13, 2012
sort number in ascending order c++
/*************************************************************************************** * File : sort-number-ascending-order.cpp * Author : Saurabh Gupta * Description : sort number in ascending order c++ * Date : AM 09:13 13 January 2012 * Source : http://www.ccplusplus.com/p/c_15.html * Note : add descendign order sorting too ***************************************************************************************/ #include
#include
using namespace std; class CSortNumbers { private: int i, j; int arr[10]; public: int m_swap(int *b,int *c ) { int temp; temp = *b; *b = *c; *c = temp; return 0; } void m_takeInput () { for ( i = 0; i < 10; i++ ) { printf (" Enter the number %d : ", i + 1 ); scanf ( "%d", &arr[i] ); } } void m_display ( ) { for ( i = 0; i < 10 ; i++ ) { printf ( " %d " , arr[i] ); } } void m_sort () { for ( i = 0; i < 10; i++ ) { for ( j = 0; j < 10 - i - 1; j++ ) { if ( * ( arr + j ) > * ( arr + (j + 1) )) { m_swap ( arr + j , arr + (j + 1) ); } } } } }; int main() { CSortNumbers sn; sn.m_takeInput (); printf ("Before sorting:\n" ); sn.m_display (); printf ( "\n" ); sn.m_sort(); printf ( "After sorting :\n" ); sn.m_display (); printf ( "\n" ); return 0; } /* * OUTPUT * [sgupta@rhel6x64 cpp]$ c++ sort-number-ascending-order.cpp -o sort-number-ascending-order [sgupta@rhel6x64 cpp]$ ./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 cpp]$ */
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment