Question
You will create a dimple Bubble Sort program to sort a string of random integers or text.
Please read instructions and examples
Please show screenshot of proof that the code works in the C program

ECE 216 Programming Project 2 The Bubble Sort You will create a simple Bubble Sort program to sort a string of random integers or text or whatever you can punch in from the keyboard. For the input data, you will input a string of single digit ASCII characters, at least 20 of them, in some sort of random order. You arent limited to 20. Put in 100 if you like. For the output data, you will simply print this string after it is sorted For the process, you will sort the data in either descending order (largest value first smallest value last) or ascending order, but you can allow for both possibilities if you wish In order to sort the data, you will need to develop the algorithm for a bubble sort, thern create a flow chart or pseudocode so you will understand the process, and finally code it. The bubble sort is perhaps the easiest of the sorting algorithms to write. The basic principle is to simply compare two adjacent numbers. If they are in order, then leave them alone and move to the next pair. If they are out of order, swap their positions in memory, and then move onto the next pair Here is a simple example using 5 numbers. Start: 18496 81496 84196 84916 8496 1 compare 8 to 4in order, no swap 89461 89641 compare 1 to 8 compare 1 to 4 compare 1 to 9 compare 1 to 6 out of order, swap them out of order, swap them out of order, swap them out of order, swap them 1st pass: now have: now have: now have: now have: 2nd pass: now have: now have: compare 4 to 9 compare 4 to 6 compare 4 to 1 compare 8 to 9 out of order, swap them out of order, swap them in order, no swap out of order, swap them 3rd pass: now have: 98641 compare 8 to 6 compare 6 to 4 compare 4 to 1 in order, no swap in order, no swap in order, no swap
media%2Fa3a%2Fa3a07506-2ad1-4356-ac7e-dc
media%2F18a%2F18a3271d-7e81-4bb8-b809-a7
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <malloc.h>

int main()

{

int n=0 ;

printf("\nEnter number of characters you want to enter: ");

scanf("%d", &n);

char *str= NULL;

if ( n > 0 )

{

str = (char*)malloc(n*sizeof(char));

printf("\nEnter characters :");

fgets(str, 100, stdin);

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

{

scanf("%s", &str[i]);

}

//Bubble sort

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

{

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

{

if (str[j] > str[j+1]) // For decreasing order use <

{

char swap = str[j];

str[j] = str[j+1];

str[j+1] = swap;

}

}

}

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

{

printf("%c", str[i]);

if ( (i+1) % 10 == 0 )

printf("\n");

else

printf(" ");

}

}

return 0;

}

Add a comment
Know the answer?
Add Answer to:
You will create a dimple Bubble Sort program to sort a string of random integers or...
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
  • Bubble Sort Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent...

    Bubble Sort Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. Example: First Pass: ( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1. ( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4 ( 1 4 5 2 8...

  • Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays....

    Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...

  • C++ Program (Linux): Create a bubble sort that can read any list of number from a...

    C++ Program (Linux): Create a bubble sort that can read any list of number from a text file and print them out. Also print the number of comparison (how many time is had to compare number in order to sort).

  • //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;...

  • Another simple sort is the odd-even sort. The idea is to repeatedly make two passes through the a...

    Another simple sort is the odd-even sort. The idea is to repeatedly make two passes through the array. On the first pass you look at all the pairs of items, a[j] and a[j+1], where j is odd (j = 1, 3, 5, …). If their key values are out of order, you swap them. On the second pass you do the same for all the even values (j = 2, 4, 6, …). You do these two passes repeatedly until...

  • Introduction BUBBLE SORT: Step-by-step example Let us take the array of numbers "5 1 4 2...

    Introduction BUBBLE SORT: Step-by-step example Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort. In each step, elements written in bold are being compared. Three passes will be required First Pass: ( 5 1 4 2 8 ) → ( 1 5 4 2 8 ). Here, algorithm compares the first two elements, swaps since 5 (15428)→(14528). Swap since 5 > 4 (14528)→(14258). Swap...

  • 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,...

  • 11:10 PM No SIM mycourselink.lakeheadu.ca Modify the following bubble sort program to improve its performance I...

    11:10 PM No SIM mycourselink.lakeheadu.ca Modify the following bubble sort program to improve its performance I Fig. 6.15: fig06 15.c 2 Sorting an array's values into ascending order. Finclude <stdio.h> 4 #define SIZE 10 6 function main begins program execution 7 int main(void) 9 initialize a lo int a CSIZEJ 12, 6, 4, 8, 10, 12, 89, 68, 45, 37) 12 putsc Data items in original order output original array IS for (size t i 0; i SIZE ++i) a[i])...

  • Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and...

    Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and Insertion Sort. The numbers to be sorted will be obtained using a library function which generates pseudo-random numbers. TO Do 1. Fill an array with 140 real numbers between 0.00 and 945.00. Generate the numbers using the subroutine that generates random numbers. Make a spare copy of the array; you will need it later. 2. Call a subroutine to print the contents of the...

  • Design a program that allows you to experiment with different sort algorithms in Java. This program should allow you to...

    Design a program that allows you to experiment with different sort algorithms in Java. This program should allow you to easily plug-in new sort algorithms and compare them. Assume that input data is generated randomly and stored in a text file (have no less than 2000 items to sort). Do not restrict your program to only one data type, or to one ordering relationship. The data type, ordering relationship, and the sorting method must be input parameters for your program....

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