Question

In: Computer Science

How can i bubble sort a sentence in a char array in c++ This is the...

How can i bubble sort a sentence in a char array in c++

This is the prototype of the function:

char* sort(char string[], int numOfWords, int lengthOfWord);

This is the testing code in the main file:

char words[] = "CAT FAT BAT HAT RAT";
printf("Before sort: t%s\n";words);
char result = sort(words; 5; 3);
printf("After sort : t%s\n"; result);

Expected output:

Before sort: CAT FAT BAT HAT RAT
After sort: BAT CAT FAT HAT RAT

Solutions

Expert Solution

#include<bits/stdc++.h>

using namespace std;

#define MAX 100

void sort(char arr[][MAX], int n)

{

    char temp[MAX];

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

    {

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

        {

            if (strcmp(arr[j], arr[i]) > 0)

            {

                strcpy(temp, arr[j]);

                strcpy(arr[j], arr[i]);

                strcpy(arr[i], temp);

            }

        }

    }

}

int main()

{

    char arr[][MAX] = {"CAT","FAT","BAT","HAT","RAT"};

    int n = sizeof(arr)/sizeof(arr[0]);

    sortStrings(arr, n);

    printf("Strings in sorted order are : ");

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

        printf(arr[i],",");

    return 0;

}


Related Solutions

How would I make a bubble sort and an optimized bubble sort with the code given?...
How would I make a bubble sort and an optimized bubble sort with the code given? I also need to implement a timer into each sort and display runtime with the sorts. NODE.H _______________________________________________________________________________________________________ /* node.h */ /* two classes 1: node.h 2. singlylinkedlist.h nod1 (value + pointer) ---> node2 ---> node3 ---> |||| <--- node.h ^ | singlylinkedlist ----------------*node head; */ #ifndef NODE_H #define NODE_H #include <iostream> using namespace std; class Node {    friend class singlyLinkedList; public:   ...
C++ Program (Using 2D array and bubble sort to sort data) A company pays its salespeople...
C++ Program (Using 2D array and bubble sort to sort data) A company pays its salespeople on a commission basis.  The salespeople each receive $250 per week plus 11 percent of their gross sales for the sales period.  For example, a salesperson who grosses $5000 in sales in the period receives $250 plus 11 percent of $5000, or a total of $812.21.  Write a program (using an array of counters) determines for each salesperson their total sales, their salary and additional data points.  There...
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers....
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers. Record the time. Run Bubble Check time (compute the processing time) do it 100 times (random numbers) Take the average Insertion: Compare] (some explanations please)
Write a Y86 program in C language that sorts an array of data using Bubble Sort....
Write a Y86 program in C language that sorts an array of data using Bubble Sort. Allow the user to input up to 10 numbers from the keyboard. Sort the array in place (i.e., no need to allocate additional memory for the sorted array). Your program should be a complete one
i have an array of strings, how do i sort them into a binary tree? C...
i have an array of strings, how do i sort them into a binary tree? C Program
(In C language) Given a two-dimensional char array, how do I encode it into a one...
(In C language) Given a two-dimensional char array, how do I encode it into a one dimensional integer array? For example: char arr [8][8] = {{1,1,1,1,1,1,1,1}, {1,0,0,0,1,0,0,1}, {1,0,1,0,1,1,0,1}, {1,0,1,0,0,0,0,1}, {1,0,1,1,1,1,0,1}, {1,0,0,0,0,0,0,1}, {1,0,1,0,1,0,1,1}, {1,1,1,1,1,1,1,1}} into int arr2 [8] I know this problem requires bit-shifting but I am unsure how. It also needs to be as efficient as possible. Thanks!
ASSEMBLY PROGRAM!!! QtSpim Sorting Data Add the Bubble Sort to minMaxArray.asm to sort the array into...
ASSEMBLY PROGRAM!!! QtSpim Sorting Data Add the Bubble Sort to minMaxArray.asm to sort the array into ascending order. Use the Bubble Sort algorithm from the lecture. You can use either Base Addressing or Indexed Addressing for the arrays. For this assignment, make sure you prompt the user for the numbers. Do not hard-code them in the data section. NOTE: Declare the array last in the Data section.
// This program uses a bubble sort to arrange an array of integers in // ascending...
// This program uses a bubble sort to arrange an array of integers in // ascending order (smallest to largest). It then display the array // before the sorting and after the sorting. Modify the program so it orders // integers in descending order (largest to smallest). Then add some code // to display the array at each step of the algorithm. You don't have to // modify anything in the main() function. All modification are inside // the bubbleSortArray()...
I would like to integrate a bubble sort into this binary search in c ++ Thank...
I would like to integrate a bubble sort into this binary search in c ++ Thank you! #include <iostream> using namespace std; // Binary search algorith // f is the first , l is the last , t is the target int binarySearch(int stgrade[], int f, int l, int t) {         while (f <= l)         {             int m = f + (l - l) / 2;                 // Check if...
The bubble sort described in AS 7e is inefficient when perform on a larger array. To...
The bubble sort described in AS 7e is inefficient when perform on a larger array. To improve the efficiency, we have to observe the basic mechanism of Bubble Sort. Each inner loop of the Bubble sort always move the largest item on the unsorted left side to the sorted right side. When a list is sorted, we say the list is in ascending order by convention. We can enhance the efficiency of traditional Bubble sort by making only one swapping...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT