Write a function to sort data. (in increasing order) in an array using
a. pass by value
b. and pass by reference.?
#include<iostream>
using namespace std;
//Pass by Reference
void sort(int *A, int n)
{
int i, j,temp;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-1; j++)
if (A[j] > A[j+1])
{
temp= A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
//Pass by value
void Sort(int A[], int m)
{
int j, i,temp;
for (j = 0; j < m-1; j++)
for (i = 0; i < m-j-1; i++)
if (A[i] > A[i+1])
{
temp= A[i];
A[i] = A[i + 1];
A[i + 1] = temp;
}
}
//Testing code
int main(){
int A[] = {10,5,50,15,85,2,32,18};
int m = sizeof(A)/sizeof(A[0]);
Sort(A,m);
for(int i=0; i<m; i++){
cout<<A[i]<<" ";
}
}
Comments
Leave a comment