Question

Use any programming language. 1)Permutations (Johnson Trotter): {1, 2, 3, 4, 5}; Insertion Sort: {45, 24,...

Use any programming language.

1)Permutations (Johnson Trotter): {1, 2, 3, 4, 5}; Insertion Sort: {45, 24, 16, 92, 71, 69, 28} – develop count of # data “insertions”

2) Use sorted data from insertion sort (part A) and develop # of key compares against the following: 16, 77, 24, 92, 44

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1)1st part

#include <iostream>
#include<conio.h>

using namespace std;

const bool LTR = true;
const bool RTL = false;

void displayPermutation(int);

// main function
int main()
{
int n = 5;

displayPermutation(n);

getch();

return 0;
}

// function find the factorial of n
int fact(int n)
{
if(n==1) return 1;
return (n*fact(n-1));
}

// function finding the position of largest integer
int search(int a[], int n, int key)
{
for (int i=0; i<n; i++)
if (a[i] == key)
return i + 1;
}

// function find the largest mobile integer
int getMobile(int a[], bool d[], int n)
{
int prev = 0, m = 0;
for (int i=0; i <n; i++)
{
// 0 represents RIGHT TO LEFT.
if (d[a[i]-1] == RTL && i!=0)
{
if (a[i]>a[i-1] && a[i]>prev)
{
m = a[i];
prev = m;
}
}

// 1 represents LEFT TO RIGHT.
if (d[a[i]-1] == LTR && i!=n-1)
{
if (a[i]>a[i+1] && a[i]>prev)
{
m = a[i];
prev = m;
}
}
}

if (m == 0 && prev == 0)
return 0;
else
return m;
}

// function to interchange two variables
void interchange(int &a, int &b)
{
int t;
t = a;
a = b;
b = t;
}

// function prints a single permutation
int displaySinglePerm(int a[], bool d[], int n)
{
int m = getMobile(a, d, n);
int pos = search(a, n, m);

// interchange the elements
if (d[a[pos - 1] - 1] == RTL)
interchange(a[pos-1], a[pos-2]);

else if (d[a[pos - 1] - 1] == LTR)
interchange(a[pos], a[pos-1]);

for (int i=0; i<n; i++)
{
if (a[i]> m)
{
if (d[a[i]-1] == LTR)
d[a[i]- 1] = RTL;
else if (d[a[i] - 1] == RTL)
d[a[i] - 1] = LTR;
}
}

cout<<" ";
for (int i=0; i<n; i++)
cout<<a[i];

}

//function to display permutation
void displayPermutation(int n)
{
int a[n];
bool d[n];

for (int i=0; i<n; i++)
{
a[i] = i + 1;
cout << a[i];
}
for (int i=0; i<n; i++)
d[i] = RTL;

for (int i=1; i<fact(n); i++)
displaySinglePerm(a, d, n);
}

Output screen:

12345 12354 12534 1523451234 51243 15243 12543 12453 12435 14235 14253 14523 15423 51423 5412345123415234125341235, 41325 413

1) 2nd part:

#include<iostream>
#include<conio.h>

using namespace std;

int insertion(int a[], int n);

//main function
int main()
{
int a[] = {45, 24, 16, 92, 71, 69, 28};
int n = 7;

int c = insertion(a, n);

cout<<"Sorted List: "<<endl;

for(int i=0; i<n; i++)
   cout<<a[i]<< " ";

cout<<endl<<"Number of insertion: "<<c<<endl;

getch();
}

//function of insertion sort
int insertion(int a[], int n)
{
int i, j, t;
int count = 0;

for(i=1; i<n; i++)
   {
t=a[i];

if(t<a[i-1]) count++;

for(j=i-1;j>=0 && t<a[j];j--)
{
a[j+1]=a[j];
}
a[j+1]=t;
}

return count;
}

Output Screen:

Sorted list: 16 24 28 45 69 71 92 Number of insertion: 5

2)

#include<iostream>
#include<conio.h>

using namespace std;

void insertion(int a[], int n);
int inserting(int a[], int&, int);

//main function
int main()
{
int a[20] = {45, 24, 16, 92, 71, 69, 28};
int n = 7;

int c = 0;

insertion(a, n);

c = inserting(a, n, 16);
cout<<endl<<"Number of comparisons to insert 16: "<<c;
c = inserting(a, n, 77);
cout<<endl<<"Number of comparisons to insert 77: "<<c;
c = inserting(a, n, 24);
cout<<endl<<"Number of comparisons to insert 24: "<<c;
c = inserting(a, n, 92);
cout<<endl<<"Number of comparisons to insert 92: "<<c;
c = inserting(a, n, 44);
cout<<endl<<"Number of comparisons to insert 44: "<<c;


getch();
}


int inserting(int a[], int &n, int key)
{
int i;

int count = 1;

for(i=n-1; i>=0 && key<a[i];i--,count++)
{
a[i+1]=a[i];
}

a[i+1]=key;

n = n + 1;

return count;
}

//function of insertion sort
void insertion(int a[], int n)
{
int i, j, t;

for(i=1; i<n; i++)
   {
t=a[i];

for(j=i-1;j>=0 && t<a[j];j--)
{
a[j+1]=a[j];
}
a[j+1]=t;
}
}

Output screen:

Number of comparisons to insert 16: 7 Number of comparisons to insert 77: 2 Number of comparisons to insert 24: 7 Number of c

Add a comment
Know the answer?
Add Answer to:
Use any programming language. 1)Permutations (Johnson Trotter): {1, 2, 3, 4, 5}; Insertion Sort: {45, 24,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Complete function long_list_printer.print_list(). When it's finished, it should be able to print this list, a =...

    Complete function long_list_printer.print_list(). When it's finished, it should be able to print this list, a = [ [93, 80, 99, 72, 86, 84, 85, 41, 69, 31], [15, 37, 58, 59, 98, 40, 63, 84, 87, 15], [48, 50, 43, 68, 69, 43, 46, 83, 11, 50], [52, 49, 87, 77, 39, 21, 84, 13, 27, 82], [64, 49, 12, 42, 24, 54, 43, 69, 62, 44], [54, 90, 67, 43, 72, 17, 22, 83, 28, 68], [18, 12, 10,...

  • Question 1 Question 2 Answer last part Question 3: Do a graph for actors and actresses...

    Question 1 Question 2 Answer last part Question 3: Do a graph for actors and actresses Question Help 4.1.5 Which of the following values cannot be probabilities? 0.03, 1.38, 5/3. 2,0. -0.52,1. 3/5 Select all the values that cannot be probabilities. A. 5 B. 0 C. -0.52 D. 3 5 E. 2 F. 0.03 G. 1 H 1.38 3.3.33 Question Help Use the same s Use the boxplot ling data sets Pulse rates for men and women Click the i...

  • Please use C++ as a Programming language and do the tasks specified per the Guideline above...

    Please use C++ as a Programming language and do the tasks specified per the Guideline above and include comments of your work. Please make sure that the following test cases are working: Example 1 For input D13 D60 D76 D12 A17 D98 A94 D70 D3 A23 A42 D45 A100 D50 A99 A22 A87 A4 A90 D88 A71 A20 D39 D83 A97 A56 D28 A9 D43 A19 D5 A11 A54 A73 D54 A9 A24 A58 D6 D80 A72 A47 A82 A12...

  • Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D...

    Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D array of integers which is 10x10. 2. Prints out the contents of the 2D array after assigning the data to make sure correct data was assigned. 3. Figures out and prints out the square root of the sum of ALL the elements in the 2D array. 4. Figures out and prints out the average of ALL THE ELEMENTS in the 2D array. 5. Figures...

  • == Programming Assignment == For this assignment you will write a program that controls a set...

    == Programming Assignment == For this assignment you will write a program that controls a set of rovers and sends them commands to navigate on the Martian surface where they take samples. Each rover performs several missions and each mission follows the same sequence: deploy, perform one or more moves and scans, then return to base and report the results. While on a mission each rover needs to remember the scan results, in the same order as they were taken,...

  • 'Student Pair' 'Standard Teaching Method' 'New Teaching Method' 1 51 67 2 72 90 3 85...

    'Student Pair' 'Standard Teaching Method' 'New Teaching Method' 1 51 67 2 72 90 3 85 82 4 51 63 5 73 76 6 72 73 7 65 78 8 72 94 9 72 85 10 95 100 11 70 80 12 60 72 13 57 100 14 48 58 15 74 89 16 63 97 17 82 88 18 57 45 19 87 81 20 65 99 21 48 69 22 97 70 23 61 47 24 83 73...

  • python program do not use dictionary, list only Complete the program found in assignment4.py. You may...

    python program do not use dictionary, list only Complete the program found in assignment4.py. You may not change any provided code. You may only complete the sections labeled: #YOUR CODE HERE Write a program that does the following. Reads the contents of Text from the include file, input.txt Create a dictionary of key-value pairs called index.txt Key: This represents the individual word Value: This is a list of the line number from Text where Key appeared Example: If the word...

  • 2 62.8 MEAN = 3 71.9 MEDIAN = 4 69.6 MODE= 5 74.1 RANGE = 6 66.6 MIN. = 7 76.5 MAX.= 8 66.4 STDEV. = 9 73.1 MY HEIGHT =67.00 10 71.6 Z SCORE FOR MY HEIGHT = 11 69.3 12 64.0 13...

    2 62.8 MEAN = 3 71.9 MEDIAN = 4 69.6 MODE= 5 74.1 RANGE = 6 66.6 MIN. = 7 76.5 MAX.= 8 66.4 STDEV. = 9 73.1 MY HEIGHT =67.00 10 71.6 Z SCORE FOR MY HEIGHT = 11 69.3 12 64.0 13 70.9 14 62.2 15 63.3 16 67.7 17 65.2 18 64.2 19 69.4 20 71.7 21 64.6 22 69.0 23 71.3 24 69.1 25 71.6 26 75.9 27 66.2 28 67.4 29 64.6 30 69.6 31...

  • Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In...

    Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In this project, we combine the concepts of Recursion and Merge Sorting. Please note that the focus of this project is on Merging and don't forget the following constraint: Programming Steps: 1) Create a class called Art that implements Comparable interface. 2) Read part of the file and use Merge Sort to sort the array of Art and then write them to a file. 3)...

  • Write a Java program, In this project, you are going to build a max-heap using array...

    Write a Java program, In this project, you are going to build a max-heap using array representation. In particular, your program should: • Implement two methods of building a max-heap. o Using sequential insertions (its time complexity: ?(?????), by successively applying the regular add method). o Using the optimal method (its time complexity: ?(?), the “smart” way we learned in class). For both methods, your implementations need to keep track of how many swaps (swapping parent and child) are required...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT