Home > C++ > Simple sort

Simple sort

#include

using namespace std;
const int Max = 5;
void dcOrder(int []);
void acOrder(int []);
void swap(int *, int *);
int main(int argc, char *argv[])
{
int iarray[Max] ;
cout << " Enter five numbers " << endl ; for (int i = 0; i < Max ; i++) { cout <> “;
cin >> iarray[i] ;

}
acOrder(iarray);
//dcOrder(iarray);
cout << endl; cout << "The new Order according to the sorting function is " << endl; for(int i = 0; i < Max ; i++) { cout < " << iarray[i] << endl; } //system("PAUSE"); //return EXIT_SUCCESS; return 0; } void dcOrder(int iarray []) { for ( int i = 0; i < Max ; i++) { int tempnum = iarray[i]; int j = i ; while (j tempnum) { swap(&iarray[j],&tempnum); } j++; } iarray[i]=tempnum; } } void acOrder(int iarray []) { for ( int i = 0; i < Max ; i++) { int tempnum = iarray[i]; int j = i ; while (j < Max) { if (iarray[j] < tempnum) { swap(&iarray[j],&tempnum); } j++; } iarray[i]=tempnum; } } void swap(int *n1, int *n2) { int tempnum; tempnum = *n1; *n1 = *n2; *n2 = tempnum; } [/sourcecode] We learn many thing in this exercise. First we learn how to work with function. we learn how to pass parameters to the function , pass by reference. Function dcOrder and acOrder are the function that to do the sorting in descending and accending order . They accept an array parameter. when we call this function we pass the name of array to the function and what happend the fucntion will effect to the input parameter . that mean the value of array will change according to the function. The same way in the swap(int *, int *) function. i us the pointer technique to make the value swap between the two input parameters. or you can use the same technique but different manner , by using swap(int & , int & ) instead. and it will look like this void swap(int &num1, int &num2) { int temp; temp = num1; num1 = num2; num2 = temp; } to call this function just pass the variable name ... it sound better , isn't it.? in additional , Passing arrays to functions , the name of array variable is actually the address of the first element of the array[0] , The parameter of the called function, some_list , can be declared in any of the following ways: functionName(int some_list []) // with no subscript { .... } OR functionName(int some_list[10]) // same size as the array you want to pass to function {..... } OR functionName(int *some_list) // int type pointer {.....} Since the address of the array is always passed to a function , it is easy to alter the contents of the array directly in the function. Therefore, arrays do not have to be returned from function explicitly the return statement.

Categories: C++
  1. steerapong
    July 7, 2008 at 6:28 am

    There are many good sorting algorithm. .. 🙂

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: