Question

In: Computer Science

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

Solutions

Expert Solution

Solution:

  1. Prompt the user for number of data in array.
  2. check the entered number of data is up to 10. if not display the message and return.
  3. prompt the user to enter the data in array.
  4. perform the bubble sort
  5. display the sorted array

Code (PLEASE REFER THE SCREENSHOT FOR CODE INDENTATION):

#include <stdio.h>

int main()
{
int array[10] = {0};
int numOfData;
int i = 0, j = 0;
int temp = 0;
  
/* prompt the user to enter number of elements in the array */
printf("Enter the number of elements in array (up to 10): ");
scanf("%d", &numOfData);
  
if(numOfData > 10)
{
printf("Entered value is greater than 10. Please try again with value up to 10.");
return 0;
}
  
/* prompt the user to enter data */
for(i = 0; i < numOfData; i++)
{
printf("Enter data #%d: ", i);
scanf("%d", &array[i]);
}
  
/* Bubble sort the array
After the first iteration the last element will be at sorted position
After second iteration the second last element will be at sorted position
and so on.
*/
/* loop for numOfData - 1 (one less than the total number of elements) in the array */
for(i = 0; i < numOfData -1; i++)
{
/* After every iteration one element is at sorted position,
so loop till the number of unsorted items */
for(j = 0; j < numOfData - i - 1; j++)
{
/* swap the element in the array */
if(array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
  
/* diaplay the sorted array */
printf("Sorted array: ");
for(i = 0; i < numOfData; i++)
{
printf("%d ", array[i]);
}

return 0;
}

Output:


Related Solutions

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...
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers...
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers by repeatedly calling a “swap” subroutine. The original unsorted list of integers should be received from the keyboard input. Your program should first prompt the user “Please input an integer for the number of elements:”. After the user enters a number and return, your program outputs message “Now input each element and then a return:”. For example, if the user enters 5 as the...
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that...
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that sorts the array below in ascending order.  LISP is a recursive language so the program will use recursion to sort. Since there will be no loops, you will not need the variables i, j, and temp, but still use the variable name array for the array to be sorted.             Array to be sorted is 34, 56, 4, 10, 77, 51, 93, 30, 5, 52 The...
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
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.
(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 Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for...
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for Bubble Sort b. Use a dynamic array of integers in a variable size of n. c. Display the following information: 1) Total counts of comparisons 2) Total counts of shifts / moves / swaps, whichever applies d. Write a main() function to test a best, and an average cases in terms of time efficiency i. Fill out the array with random numbers for an...
Write and test a C program to implement Bubble Sort. . In your C program, you...
Write and test a C program to implement Bubble Sort. . In your C program, you should do: Implement the array use an integer pointer, get the size of the array from standard input and use the malloc function to allocate the required memory for it. Read the array elements from standard input. Print out the sorted array, and don’t forget to free the memory. Debug your program using Eclipse C/C++ CDT.
in MARIE simulator, write assembly language to BUBBLE SORT 30 hexadecimals store in two array.
in MARIE simulator, write assembly language to BUBBLE SORT 30 hexadecimals store in two array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT