answersLogoWhite

0


Best Answer

#include<iostream>

#include<time.h>

#include<iomanip>

#include<string>

void swap(int& x, int& y)

{

x^=y^=x^=y;

}

void bubble_sort(int* A, int size)

{

while(size)

{

int n=0;

for(int i=1; i<size; ++i)

{

if(A[i-1]>A[i])

{

swap(A[i-1], A[i]);

n=i;

}

}

size=n;

}

}

void insertion_sort(int* A, int size)

{

for(int i=1; i<size; ++i)

{

int value=A[i];

int hole=i;

while( hole && value<A[hole-1] )

{

A[hole]=A[hole-1];

--hole;

}

A[hole]=value;

}

}

void selection_sort(int* A, int size)

{

for(int i=0; i<size-1; ++i)

{

int j=i;

for(int k=i+1; k<size; ++k)

if(A[k]<A[j])

j=k;

if( i!=j )

swap(A[i],A[j]);

}

}

void sort(int* A, int size, int sort_type)

{

switch(sort_type)

{

case(0): bubble_sort( A, size );

case(1): insertion_sort( A, size );

case(2): selection_sort( A, size );

}

}

int* copy_array(int* A, int size)

{

int* copy=new int[size];

memcpy(copy, A, size*sizeof(int));

return(copy);

}

void print_array(int* A, int size, char* prompt)

{

std::cout<<prompt<<"\t";

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

std::cout<<std::setw(2)<<A[i]<<" ";

std::cout<<std::endl;

}

int get_rand(int range_min=0, int range_max=RAND_MAX)

{

return((int) ((double)rand() / (RAND_MAX + 1) * ((range_max + 1) - range_min) + range_min));

}

int input_char(std::string prompt, std::string input)

{

char ch;

do

{

std::cout<<prompt<<": ";

std::cin>>ch;

}

while(input.find(ch)==std::string::npos);

return(input.find(ch)%(input.size()/2));

}

int main()

{

srand((unsigned) time(NULL));

int size = get_rand( 10, 80);

if( int* A = new int[size] )

{

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

A[i]=get_rand( 1, size );

int choice=input_char("Please select a sorting method:\n[B]ubble, [I]nsert, [S]election", "bisBIS");

std::cout<<"You chose ";

switch(choice)

{

case(0): std::cout<<"bubble"; break;

case(1): std::cout<<"insertion"; break;

case(2): std::cout<<"selection"; break;

}

std::cout<<" sort...\n"<<std::endl;

print_array( A, size, "Before sorting" );

sort(A, size, choice);

print_array( A, size, "After sorting" );

delete [] A;

}

return(0);

}

User Avatar

Wiki User

10y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

Yes.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Menu driven program for selection sort bubble sort and insertion sort in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Menu driven program in c programming?

You can find the source code of the menu driven graphics program here: http://www.start2code.com/Cresources/menu-driven-graphics-program-c.html


What is the meaning of the menu driven program?

Menu-driven simply means you execute the application's commands by selecting them from a menu, as opposed to command-driven whereby you enter (type) the commands manually.


What are the advantage and disadvantage of organizing an event?

There are many advantages of event driven programming. This type of programming is the simplest and flexible forms of programming languages. This means the programmer has more control over the program when the user does something. Also, making an event driven program is easy. There are only a few disadvantages that come with the event driven programming. Firstly, sometimes classes are not often reusable and are hard to carry out in other applications. It is also very complex and hard to master.


What does it mean that VBA is objcet and event driven programming language?

Object driven programming languages are all based around objects on a 'form' or a 'room'. For example the program will be ran around buttons or other controls. For example if a user clicks a button a set of defined steps will occur. Event driven is similar. For example if this occurs then do this. For example while counter = 1 do .... Basically it is more simple for novice programmers to use and get their head around.


Write a menu driven program in java to find area of different shapes. 1. Circle 2. Triangle 3. Rectangle?

Remember that the area of a rectangle is width * height: static int getArea(Rectangle r) { return r.width * r.height; }

Related questions

Menu driven program in c programming?

You can find the source code of the menu driven graphics program here: http://www.start2code.com/Cresources/menu-driven-graphics-program-c.html


What is the formation of new species by natural selection?

We call this speciation, and is largely driven by evolution and natural selection.


Are changes in allelic frequency driven by natural selection?

False


What is the difference between sexual and natural selection?

Natural selection is survival of the fittest, while sexual selection is a preference for a given trait made by the limiting sex. It is strangely, but usually in opposition to natural selection (E.G., male peacock's tail feathers that garner attention from predators and prevent the peacock from fleeing well).


What makes natural selection happen?

Natural selection is driven by differences in reproductive success between variants within the same population.


Is operating system an event driven program?

yes


Why is target market selection important for a customer driven marketing strategy?

xzfhgalds'hg[a'pjovbdfohpgjdsaiog


Write c program of menu driven by calculator?

dcdfsadf


What is meant by event driven?

event driven programs do not have a specific pathway in which the programs instructions are executed. different parts of the program are evoked by events that take place during the running of the program.


Does natural selection deal with only plants?

No, all life evolves. Bacteria evolve, viruses evolve, protists evolve, plants evolve, fungi evolve and animals evolve. Evolution is driven by Natural Selection. So, no. The evolution of all life on Earth is driven by Natural Selection: all bacteria, plants, animals, mammals, fish, insects, biochemical pathways, behaviours et cetera evolve by Natural Selection.


What has the author Jonathan Bryan Snape written?

Jonathan Bryan Snape has written: 'Physical and biological factors in bubble-driven bioreactors'


What is the meaning of the menu driven program?

Menu-driven simply means you execute the application's commands by selecting them from a menu, as opposed to command-driven whereby you enter (type) the commands manually.