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.
Implement Radix Sort using PYTHON programming language. Use one of the two options for the algorithm...
Implement Radix Sort using PYTHON programming language. Use one of the two options for the algorithm to sort the digits: Use Counting Sort or Bucket Sort. • Assume the numbers are maximum 4-digit numbers. • If using Counting Sort, you can see that your digit range is between 0 and 9 ([0…9]). • If using Bucket Sort, you will have ten buckets labeled from 0 to 9. Please add comments and explain every step carefully.
Create a program called BubleSort.java that implements the Bubble Sort algorithm (The Art of Computer Programming...
Create a program called BubleSort.java that implements the Bubble Sort algorithm (The Art of Computer Programming - Donald Knuth). The algorithm is as follows: The program should be able to do the following: accepts one command line parameter. The parameter specifies the path to a text file containing the integers to be sorted. The structure of the file is as follows: There will be multiple lines in the file (number of lines unknown). Each line will contain multiple integers, separated...
Create a program called BubleSort.java that implements the Bubble Sort algorithm (The Art of Computer Programming...
Create a program called BubleSort.java that implements the Bubble Sort algorithm (The Art of Computer Programming - Donald Knuth). The algorithm is as follows: The program should be able to do the following: accepts one command line parameter. The parameter specifies the path to a text file containing the integers to be sorted. The structure of the file is as follows: There will be multiple lines in the file (number of lines unknown). Each line will contain multiple integers, separated...
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.
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
Using the idea of the Bubble Sort algorithm, design an algorithm in pseudocode that gets 3...
Using the idea of the Bubble Sort algorithm, design an algorithm in pseudocode that gets 3 numbers a, b, c, from the user, and prints the 3 numbers in ascending order
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT