?C program? Recall that bubble sort is a technique for sorting an array. A bubbl
ID: 3684542 • Letter: #
Question
?C program?
Recall that bubble sort is a technique for sorting an array. A bubble sort compares adjacent array elements and exchanges their values if they are out of order. In this way, the smaller values "bubble" to the top of the array (toward element 0), while the larger values sink to the bottom of the array. After the first pass of a bubble sort, the last array element is in the correct position; after the second pass the last two elements are correct, and so on. Thus, after each pass, the unsorted portion of the array contains one less element. Write and test a function that implements this sorting method on strings! You MAY use <string.h> library functions to help with sorting the strings. NOTE: you will need to run your sort on an array of strings. Any array of strings is viewed as a 2-dimensional array of characters in which each row is terminated by the null character. Thus, each row represents one string in the array of strings. Consider the below information.
char array_strings[10][50]; /* Declare an array of strings which contains at most 10 strings (the number of rows) which have a length of at most 50 characters (the number of columns) */
/* To copy a string into one row of the 2-D array use string copy as follows. */
strcpy (array_strings[0], "a string"); /* Note: supplying one dimension as an index into a 2-D array supplies a starting address for a row! */
strcpy (array_strings[1], "cat");
/* Logical view */
4 01 9Explanation / Answer
#include <stdio.h>
#include <string.h>
int main(void)
{
char array_strings[10][50],temp[50];
strcpy (array_strings[0], "a string");
strcpy (array_strings[1], "cat");
strcpy (array_strings[2], "dog bite");
strcpy (array_strings[3], "pinky");
strcpy (array_strings[4], "unicorn");
strcpy (array_strings[5], "zumba");
strcpy (array_strings[6], "dabba");
strcpy (array_strings[7], "south park");
strcpy (array_strings[8], "cartman");
strcpy (array_strings[9], "you guys!!");
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<9;j++)
{
if(strcmp(array_strings[j],array_strings[j+1])>0)
{
strcpy(temp,array_strings[j]);
strcpy(array_strings[j],array_strings[j+1]);
strcpy(array_strings[j+1],temp);
}
}
}
for(i=0;i<10;i++)
{
printf("%s ",array_strings[i]);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.