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...
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
Using C# programming language, Write a program that sort three numbers entered by the user using...
Using C# programming language, Write a program that sort three numbers entered by the user using only if and nested if statements. If for instance the user entered 5, 2, and 7; the program should display 2,5,7.
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...
It's time to write a sorting algorithm! In this lab, you'll be writing Bubble Sort. Much...
It's time to write a sorting algorithm! In this lab, you'll be writing Bubble Sort. Much like the previous lab, you will be tasked with prompting the user for a list of values until the user enters 0 (you may use the same initializeVector that you wrote in the last lab). You will then write bubblesort which sorts the vector from smallest to largest. You will then call a function displayVector which displays the vector to the screen. Keep everything...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT