Question
Asked Dec 2, 2019

In C++: 

Use a rand() function (seeded with srand(time(0))) to generate a small arrays of dimensions 20, such that the entries are random positive integers less than 100. Implement the bubble sort and the selection sort and sort your random array of integers. 

check_circle

Expert Answer

Step 1

Program Algorithm-

START

Step1: Import the essential headers

Step2: Define the main function.

Step3: Declare all the variables and their datatypes accordingly.

Step4: Define the arrays and their size.

Step5: Use rand() function to insert random values in the array.

Step6: Use for loop to input the elements in each iteration.

Step7: Define two functions for implementing the logic of Bubble sorting and Selection Sorting.

Step8: Call both the function simultaneously.

Step9: Use another for loop to print the sorted elements of the array.

END

 

Step 2

Here in this program we have used the concept of pointers which eventually helped in reducing the complexity of program. Pointers store address of variables or a memory location. Pointer execution time is faster because the address is used to manipulate the data, i.e. direct access to the location of the memory.

Syntax for pointers-

datatype *var_name;

int *ptr;

 

Code(in C++)-

#include <iostream>

using namespace std;

//swap function

void swap(int *xp, int *yp)

{

//Swapping is performed

int temp = *xp;

*xp = *yp;

*yp = temp;

}

// A function to implement bubble sort

void bubbleSort(int arr[], int n)

{

int i, j;

for (i = 0; i < n-1; i++)

 

// Last i elements are already in place

for (j = 0; j < n-i-1; j++)

if (arr[j] > arr[j+1])

swap(&arr[j], &arr[j+1]);

}

// Implementing Selection sort using function

void selectionSort(int arr[], int n)

{

int i, j, min_idx;

// One by one move boundary of unsorted subarray

for (i = 0; i < n-1; i++)

{

// Find the minimum element in unsorted array

min_idx = i;

for (j = i+1; j < n; j++)

if (arr[j] < arr[min_idx])

min_idx = j;

// Swap the found minimum element with the first element

swap(&arr[min_idx], &arr[i]);

}

}

/* Function to print an array */

void printArray(int arr[], int size)

{

int i;

for (i = 0; i < size; i++)

cout << arr[i] << " ";

cout << endl;

}

// main function

int main()

{

int i, j, n=20, temp;

...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: i need to read a list in a text file and then print it... in python

A: Program Instructions:Take a file, in here the name of the file is “example.txt” and it contains some...

question_answer

Q: - Find all employees who makes more than that all SALESMEN make - Delete all the employees in the de...

A: Query to find the employees for the given criteria:To list all the employees who makes more sales th...

question_answer

Q: Write a c++ program! In this assignment, you need to complete tasks on 2D array of randomly sales fi...

A: Following is the c++ program:Main function is used to execute a nested loop that inserts random valu...

question_answer

Q: - Find those with manager job title but have no supervisees - Which job has the most employees?

A: WHERE is SQL clause used to extract specific data from table.NOT IN is used to eliminate specific pa...

question_answer

Q: Write a wholly creative quiz program that consists of three original questions that you make up acco...

A: As here, no programming language is specified. So, we are providing the solution in java.The below g...

question_answer

Q: I have the following code:   #include &lt;iostream&gt; #include &lt;cstdlib&gt;   using namespace st...

A: program to find that a number entered by the user is a Fibonacci number or not.This program executes...

question_answer

Q: Explain what the difference is between Data flow Diagrams and the System Flowcharts.

A: DefinitionData Flow Diagram (DFD)- DFD refers to the flow of data graphically. It is used to signify...

question_answer

Q: Python 3.7.4: I just need help with the indentation. I don't need you to look at the code itself so ...

A: Answer:“Yes”, the indentation is correct. One small mistake is in line 2, the closing quote is in wr...

question_answer

Q: What is error? Explain types of error?

A: Error:Error is defined as an activity that the user have done which is considered to be wrong.