1 answer

Data Structure using C++ only Write 4 different sorting functions: Selection Sort, Insertion Sort, Merge Sort...

Question:

Data Structure using C++ only

Write 4 different sorting functions: Selection Sort, Insertion Sort, Merge Sort and Quick sort. For each sort you may store the numbers in an array or a linked list (this may be different for each sort). Write your program so that it accepts arguments from the command line using argc and argv in the main function call.


Answers

Hi Friend, Implementing all sorting algorithm in one post is too big.

I have implemented Selction Sort.

Please repost for other in separate post.


#include <iostream>
using namespace std;


void swap(int &xp, int &yp)
{
int temp = xp;
xp = yp;
yp = temp;
}

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

// Driver program to test above functions
int main()
{
int arr[] = {56, 12, 34, 64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
cout<<"Sorted array: \n";
printArray(arr, n);
return 0;
}

fourthweek fourthweek fourthweek fourthweek g++ selectionSort.cpp fourthweek ./a.out Sorted array: 11 12 12 22 25 34 56 64 fo

.

Similar Solved Questions

1 answer
USING TVM SOLVER IN CALCULATOR ONLY (TI 83-84) plz show me how to do it in...
USING TVM SOLVER IN CALCULATOR ONLY (TI 83-84) plz show me how to do it in the calculator (TVM) Sarah secured a bank loan of $175,000 for the purchase of a house. The mortgage is to be amortized through monthly payments for a term of 15 years, with an interest rate of 3%/ year compounded monthly on...
1 answer
Consider the circuit shown below. Calculate the total power dissipated by all the resistors in the...
Consider the circuit shown below. Calculate the total power dissipated by all the resistors in the circuit. R2 = 4 13 中宁 R4 = 22 V2 = 5 V V1 = 12 V Is R2 = 32 R3 = 212 12 13...
1 answer
As ames for Required: For each of the following accounts from Moonbilr's recent balance sheet, complete...
As ames for Required: For each of the following accounts from Moonbilr's recent balance sheet, complete the following table Indicate whether the account is classified as a current asset (CA), noncurrent asset (NCA), current liability (CL), noncurrent liability (NCL), or stockholders' equity ...
1 answer
Can someone explain why the answer is B please? Exercise The p-value for a two-sided test...
Can someone explain why the answer is B please? Exercise The p-value for a two-sided test of the null hypothesis, Ho: u = 10 was found to be 0.06. Therefore, using the same data, A) a 90% confidence interval would contain the value of 10. B) a 95% confidence interval would contain the value of 10. C...
1 answer
Question Completion Status: QUESTION 5 3 points What type of stimull does the example in the...
Question Completion Status: QUESTION 5 3 points What type of stimull does the example in the figure below depict7(Hint: For more information regarding the differences in the answer choices, visit https:// courses.lumenlearning.com/boundless-biology/chapterregulation of hormone production) Stomach re...
1 answer
20. [-14 Points] DETAILS LARPCALC10 8.4.085. Solve for x. (Enter your answers as a comma-separated list.)...
20. [-14 Points] DETAILS LARPCALC10 8.4.085. Solve for x. (Enter your answers as a comma-separated list.) |x + 4 2 = 0 1 x + 3 X =...
1 answer
The air in a 48 cubic metre kitchen is initially clean, but when David buns his toast while makin...
can anyone solve for question part d The air in a 48 cubic metre kitchen is initially clean, but when David buns his toast while making breakfast, smoke is mixed with the rooms ar at a rate of 0.08 mg conditioning system exchanges the mixture of air and smoke with clean air at a rate of 5 cubic m...
1 answer
The Blue Yonder Airlines flight from Seattle to New York has a capacity of 250 people....
The Blue Yonder Airlines flight from Seattle to New York has a capacity of 250 people. The Airlines sold 270 tickets for the flight at a price of $300 per ticket. Tickets are non refundable. The variable cost of flying a passenger is $30. If more than 250 people show up to the flight, the flight wil...
1 answer
Physics
A projectile (mass=0.20 kg) is fired atand embeds itself in a target (mass = 2.50 kg).The target(with the projectile in it) flies offafter being struck. What percentage of the projectile's incident kinetic energy doesthe target (with the projectile in it) carry off after beingstruck?...
1 answer
Only last part of the problem Problem 9 A linearly polarized electromagnetic wave is sent through...
only last part of the problem Problem 9 A linearly polarized electromagnetic wave is sent through two sheets of polarizing material. The first sheet, A, is oriented so that its transmission axis makes an angle of 30° with respect to the incident electric field of the wave. The second sheet, B, ...
1 answer
How do you evaluate #49\div 7+ 10[ 3( 9- 2) - 2( 17- 8) ]#?
How do you evaluate #49\div 7+ 10[ 3( 9- 2) - 2( 17- 8) ]#?...
1 answer
Top managers of Movie Street are alarmed by their operating losses. They are considering dropping the...
Top managers of Movie Street are alarmed by their operating losses. They are considering dropping the DVD product line. Company accountants have prepared the following analysis to help make this decision: (Click the icon to view the analysis.) Assume that Movie Street can avoid $36,000 of fixed cost...
1 answer
Question 6 1 pts You have casually observed that statistics students seem to be much taller...
Question 6 1 pts You have casually observed that statistics students seem to be much taller than average. You compare the mean height of a sample of statistics students with data from the general student body, and calculate a p-value of 0.04. This means that... Assuming the null hypothesis is true, ...
1 answer
Holt Enterprises recently paid a dividend, D0, of $2.75. It expects to have nonconstant growth of...
Holt Enterprises recently paid a dividend, D0, of $2.75. It expects to have nonconstant growth of 14% for 2 years followed by a constant rate of 5% thereafter. The firm's required return is 8%. How far away is the horizon date? The terminal, or horizon, date is infinity since common stocks do ...
1 answer
In terms of gas phase bacisity, acetone < diethylketone < triphenylphosphine. Explain why this is the...
In terms of gas phase bacisity, acetone < diethylketone < triphenylphosphine. Explain why this is the case....
1 answer
Aggrastat at 15.2 mg in 72 mL is to be infused at 8 mcg/kg/hr in a...
Aggrastat at 15.2 mg in 72 mL is to be infused at 8 mcg/kg/hr in a patient who weighs 38 kg. At what flow rate in mL/hr will you set the pump?...
1 answer
Please answer all the questions. QUESTION 1 ; a question like, "Isn't that the guy we...
Please answer all the questions. QUESTION 1 ; a question like, "Isn't that the guy we usually see on the bus?" requires (2006) A question like, "What's the name of the waiter?" requires a. recognition; recall b.familiarity, source memory Oc source memory; familiarity d. recal...