Question

Please write a c++ code If your nameArray variable is sorted using Bubble Sort Algorithm, it...

Please write a c++ code

If your nameArray variable is sorted using Bubble Sort Algorithm, it will go through a several pass-throughs and characters will be swapped in each pass-through, until nameArray gets completely sorted (alphabetically, in descending order). Show all these stages (Passes and swaps within Passes) for your nameArray. Here is an example, with the name JONPDOE, of how I want you to write these steps:

Pass-1:

[J O] N P D O E ==> [O J] N P D O E Note: O and J were swapped

O [J N] P D O E ==> O [N J] P D O E Note: N and J were swapped

O N [J P] D O E ==> O N [P J] D O E Note: P and J were swapped

O N P [J D] O E ==> O N P [J D] O E Note: No Swaps

O N P J [D O] E ==> O N P J [O D] E Note: O and D were swapped

O N P J O [D E] ==> O N P J O [E D] Note: E and D were swapped

Pass-2:

[O N] P J O E D ==> [O N] P J O E D Note: No Swap

O [N P] J O E D ==> O [P N] J O E D Note: P and N were swapped

O P N [J O] E D ==> O P N [O J] E D Note: O and J were swapped

(so on, until the array is completely sorted)

a) How many passes were required? (2 passes shown above; but more required in our example)

b) How many swaps within each pass? (in our example: Pass1: 5 swaps, Pass2: 2…)

c) How many total swaps were performed? (in our incomplete example: 7 swaps so far…)

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

#include<iostream>
using namespace std;
int count=0;
void swap(char arr[],int i, int j)
{
char temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
count++;
}
void bubbleSort(char arr[],int n)
{
// One by one move boundary of unsorted subarray
for (int i = 1; i < n; i++)
{
cout<<"\nString is in pass "<<i<<endl;
for (int j = 0; j < n-i; j++)
if (arr[j] < arr[j+1])
{
cout<<endl<<arr<<"=>";
swap(arr,j, j+1);
cout<<arr<<":\t"<<arr[j+1]<<" and "<<arr[j]<<"are swaped";
}
else
{
cout<<endl<<arr<<"=>";
cout<<arr<<":\t There is no swap";
}
  
  

}
}
void selectionSort(char arr[],int n)
{
  

// One by one move boundary of unsorted subarray
for (int i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] > arr[min_idx])
min_idx = j;
if(min_idx != i)
{
swap(arr,min_idx , i);
cout<<endl<<arr<<"=>";
cout<<arr<<":\t"<<arr[min_idx]<<" and "<<arr[i]<<"are swaped";
}
else
{
cout<<endl<<arr<<"=>";
cout<<arr<<":\t There is no swap";
}
  

}
}
int main()
{
char str[30],str1[30];
int len=0;
cout<<"\n Enter the string:";
cin>>str;
while(str[len]!=0)
{
str1[len]=str[len];
len++;
}
str1[len]='\0';
bubbleSort(str,len);
cout<<" \n Number of Swap in bubble sort:"<<count;
count=0;
cout<<"\nSelection Sort Output:\n";
selectionSort(str1,len);
cout<<" \n Number of Swap in Selection sort:"<<count;
}

chandan e Computer Science quest (M Someone commented c/X GIArm Getting This Error w .wvector begin-C-Refe L × × Cwww.HomeworkLib.c

Add a comment
Know the answer?
Add Answer to:
Please write a c++ code If your nameArray variable is sorted using Bubble Sort Algorithm, it...
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 the following in c++ programming code and answer question 3. use question 2 below as...

    complete the following in c++ programming code and answer question 3. use question 2 below as reference 2. If your nameArray variable is sorted using Bubble Sort Algorithm, it will go through a several pass-throughs and characters will be swapped in each pass-through, until nameArray gets completed sorted (alphabetically ascending order). Show all these stages (Passes and iterations within Passes) for your nameArray. Based on our example, here is a sample of how I want you to write these steps:...

  • Write in C++ (Bubble Sort) implement the bubble sort algorithm - another simple yet inefficient s...

    Write in C++ (Bubble Sort) implement the bubble sort algorithm - another simple yet inefficient sorting technique. its called bubble sort or sinking sort because smaller values gradually "bubble" their way to the top of the array like air bubbles rising in water, while the larger values sink to the bottom of the array. the technique uses nested loops to make several passes through the array. each pass compares successive pairs of elements. if a pair is in increasing order,...

  • Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers

    Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers (words)that are read from the console. Be reminded that in a descending sort, the integers are sorted from the largest to the smallest. A “special value” 99999 will beused to signify the end of the input sequence. This value is not to be considered part of the input data set. However, any value greater than 99999 that...

  • An array of 100 elements is to be sorted using the bubble sort in the modules (the one that tests...

    An array of 100 elements is to be sorted using the bubble sort in the modules (the one that tests the return value of floatLargestToTop0 to see if it can return "early".) Check all the true statements about the sort algorithm, i.e., the sort method and its support methods. (Check all that apply.) It will sometimes return (completely sorted) after only 99 data comparisons. It will always require at least one swap. It will always require at least 99 comparisons...

  • MIPS MIPS MIPS PLEASE INCLUDE COMMENTS AND OUTPUT Sort array using Bubble sort algorithm. 1) First...

    MIPS MIPS MIPS PLEASE INCLUDE COMMENTS AND OUTPUT Sort array using Bubble sort algorithm. 1) First ask the user how many elements of his/her array. 2) Then, read the integer array elements as input from the User. 3) Then, print out the array before the sorting 4) Apply Bubble sort algorithm on your array 5) Print out the array after the sorting 6) Print some welcome text to th user 7) Add comments to your code to describe how is...

  • I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that...

    I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists; it is also often useful for canonical zing data and for producing human-readable output. More formally, the output must satisfy...

  • //bubble sort implementation// Fig 6.15 with simplified convention for the for loop. #define _CRT...

    //bubble sort implementation// Fig 6.15 with simplified convention for the for loop. #define _CRT_SECURE_NO_WARNINGS //for windows os only #include <stdio.h> #include <stdlib.h> // Fig. 6.15: fig06_15.c // Sorting an array's values into ascending order. #include <stdio.h> #define SIZE 10 // function main begins program execution int main(void) {    // initialize a    int a[SIZE] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };    printf("Data items in original order");    // output original array    for (int i = 0;...

  • Exercise 7.3.5: Worst-case time complexity - mystery algorithm. The algorithm below makes some changes to an...

    Exercise 7.3.5: Worst-case time complexity - mystery algorithm. The algorithm below makes some changes to an input sequence of numbers. MysteryAlgorithm Input: a1, a2....,an n, the length of the sequence. p, a number Output: ?? i != 1 j:=n While (i < j) While (i <j and a < p) i:= i + 1 End-while While (i <j and a 2 p) j:=j-1 End-while If (i < j), swap a, and a End-while Return( aj, a2,...,an) (a) Describe in English...

  • Date: December 12th, 2019 4. Describe why you selected the worstcase runtime above. Recall what we...

    Date: December 12th, 2019 4. Describe why you selected the worstcase runtime above. Recall what we said about inversions and how the number of inversions is equivalent to the number of swaps for insertion sort. Also, recall the visualization from the notes on insertion First Name: Last Name: Hint: Given a list L = {4, 15, 104, Z. 11.2.14) where we are determining the number of inversions for 10, the inversions are underlined in red. I. INSERTION SORT 1. Given...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

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