Question

In: Computer Science

Write a function to sort data (in increasing order) in an array using a. pass by...

Write a function to sort data (in increasing order) in an array using a. pass by value b. and pass by reference.

Solutions

Expert Solution

1. Program: PASS BY VALUE

#include<iostream>
using namespace std;

void Sort(int [],int);

int main()
{
int A[] = {10,5,50,15,85,2,32,18};
int n = sizeof(A)/sizeof(A[0]);
Sort(A,n);
cout<<"Sort data in Increasing order:\n";
for (int i = 0; i < n; i++)
cout << A[i] << " ";
return 0;
}

void Sort(int A[], int n)
{
int i, j,temp;
for (i = 0; i < n-1; i++)     
for (j = 0; j < n-i-1; j++)
if (A[j] > A[j+1])
{
temp= A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}

Output:

2. Program: Pass By Reference

#include<iostream>
using namespace std;
void Sort(int *,int);

int main()
{
int A[] = {10,5,50,15,85,2,32,18};
int n = sizeof(A)/sizeof(A[0]);
Sort(A,n);
cout<<"Sort data in Increasing order:\n";
for (int i = 0; i < n; i++)
cout << A[i] << " ";
return 0;
}

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;
}
}

Output:


Related Solutions

Sorting (quick) Sort the array using quick sort. Write the array content after each pass (i.e.,...
Sorting (quick) Sort the array using quick sort. Write the array content after each pass (i.e., from pass 1 to pass 7). Each pass is defined as the completion of one partition. We always pick the first array element as the pivot for each partition.
Sort a string array by frequency given an array of string write a function that will...
Sort a string array by frequency given an array of string write a function that will return an array that is sorted by frequency example {"hello","hola","hello","hello","ciao","hola","hola","hola"} returned array should be {"hola","hello","ciao"}
Sorting (merge) Sort the array (as in Part 2) using merge sort. Write the array content...
Sorting (merge) Sort the array (as in Part 2) using merge sort. Write the array content after each pass (i.e., from pass 1 to pass 9). Each pass is defined as the completion of one merge operation. Suppose an array contains the following integers: -1, 20, 10, -5, 0, -7, 100, -7, 30, -10. Sort the array using the following algorithms: selection sort, bubble sort, and insertion sort. For each algorithm, write the array content after each pass (i.e., from...
Write a Y86 program in C language that sorts an array of data using Bubble Sort....
Write a Y86 program in C language that sorts an array of data using Bubble Sort. Allow the user to input up to 10 numbers from the keyboard. Sort the array in place (i.e., no need to allocate additional memory for the sorted array). Your program should be a complete one
Write program that pass unsorted one dimensional array and a key to function, the function will...
Write program that pass unsorted one dimensional array and a key to function, the function will search for the key(using linear search) and if it is found the function will sort the array from the beginning to the position of the key, and f it is not found the function will sort all the array Note: please solve it by c , not c++
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
use quick sort to sort the following array. show each pass and what the pivot is...
use quick sort to sort the following array. show each pass and what the pivot is during the pass. please explain why you are swapping positions. do not use a compiler. 30 5 40 11 20 9 15 2 60 25 80 3 73 35 4 75 20 6
I'm trying to create a function determines the range of outliers in an array using pass...
I'm trying to create a function determines the range of outliers in an array using pass by reference. I need to write a function that outputs the +3 and -3 standard deviation. I'm having a hard time figuring out what I should identify as my pointers and how to use the pass by reference. This is what I have so far: #include <stdio.h> #define ARRAY_SIZE 20 double homeworkArray[ARRAY_SIZE] = { 1.3, 5.7, 2.1, -1.2, 0.5, 4.3, 2.1, 50.2, 3.4, 1.1,...
Language Javascript Implement Insertion Sort 1. Non-increasing order, 2. At least an array of 10 elements.,...
Language Javascript Implement Insertion Sort 1. Non-increasing order, 2. At least an array of 10 elements., 3. You can use a static array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT