Question

In: Computer Science

The programming language is C. In its simplest algorithm, the bubble sort technique compares each two...

The programming language is C.

In its simplest algorithm, the bubble sort technique compares each two adjacent elements of an array of size “N” and exchanges their values if they are out of order. The process of comparing and exchanging is repeated for N array passes. For sorting array elements in ascending order, the smaller values “bubble” to the top of the array (toward element 0), while the larger values sink to the bottom of the array. Assuming that: double ArrayA[ ] = {100.1, 99.2, -8.7, 23.3, -19, 11.2, 20.6, 27.7, -1.1, 13.1, 12.6, 15.9, 55.1, -40.2, 0 } is stored in memory. Write a complete C language program that sorts ArrayA in ascending order using bubble sort technique. Your program should display the array before and after sorting. Your answer should include screenshots of your code and program outputs. Your programs should be properly commented.

Solutions

Expert Solution

Source Code:

#include <stdio.h>
void bubbleSort(double arr[],int n)
{
   double temp=0.0;
   for(int i=0;i<n-1;i++)
   {
       for(int j=0;j<n-i-1;j++)
       {
           if(arr[j]>arr[j+1])
           {
               temp=arr[j];
               arr[j]=arr[j+1];
               arr[j+1]=temp;
           }
       }
   }
}
void printArray(double arr[],int n)
{
   for(int i=0;i<n;i++){
       printf("%.2lf ",arr[i]);
   }
   printf("\n");
}
int main()
{
   double ArrayA[ ] = {100.1, 99.2, -8.7, 23.3, -19, 11.2, 20.6, 27.7, -1.1, 13.1, 12.6, 15.9, 55.1, -40.2, 0 };
   int len=sizeof(ArrayA)/sizeof(ArrayA[0]);
   printf("Before sorting:\n");
   printArray(ArrayA,len);
   bubbleSort(ArrayA,len);
   printf("After sorting:\n");
   printArray(ArrayA,len);
}

Sample input and output:


Related Solutions

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...
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 that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude <iostream>)
(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)
What is the number of comparisons in the bubble sort algorithm, if it is used to...
What is the number of comparisons in the bubble sort algorithm, if it is used to sort a list of n-entries? Justify your formula.
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 program and test a program that translates the following Bubble Sort algorithm to a...
Write a program and test a program that translates the following Bubble Sort algorithm to a bubblesort function. The function's prototype is, void bubblesort(int a[], int size); Bubble Sort The inner loop moves the largest element in the unsorted part of the array to the last position of the unsorted part of the array; the outer loop moves the last position of the unsorted part of the array. The Bubble sort exchanges elements with adjacent elements as it moves the...
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.
Import a data set (txt file) then do the sorting algorithm using bubble sort, radix sort,...
Import a data set (txt file) then do the sorting algorithm using bubble sort, radix sort, insertion sort, and merge sort, It must show how long it took and how many movements occurred. Please write codes in C++ Here's data set (should be stored in txt file) 7426 4524 4737 9436 3997 2757 6288 5414 9590 5968 6638 3199 9514 1541 9866 2144 6731 911 2171 6135 6437 912 9417 2662 6606 6349 707 2890 5386 9718 3492 5068 9674...
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes...
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes the final score of a baseball game. Use a loop to read the number of runs scored by both teams during each of nine innings. Display the final score afterward. Submit your design, code, and execution result via file, if possible
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT