Question

In: Computer Science

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:

  1. 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.
  2. Read the array elements from standard input.
  3. Print out the sorted array, and don’t forget to free the memory.

Debug your program using Eclipse C/C++ CDT.

Solutions

Expert Solution

Please look at my code and in case of indentation issues check the screenshots.

------------main.c----------------

#include <stdio.h>
#include <stdlib.h>

void bubbleSort(int *array, int size)               //this function performs bubble sort on the array
{
   int i, j, temp;
   for(i = 0; i < size-1; i++){                   //loop size-1 times
       for(j = 0; j < size-i-1; j++){               //at each iteration, index size-i-1 till end elements are sorted
           if(array[j] > array[j+1]){               //compare adjacent elements
               temp = array[j];                   //Swap the elements
               array[j] = array[j+1];
               array[j+1] = temp;
           }
       }
   }
}

void printArray(int *array, int size)               //this function prints the array
{
   int i;
   printf("\nThe array is: ");
   for (i = 0; i < size; i++){
       printf("%d ", array[i]);
   }
   printf("\n");
}

int main()
{
   int *array;                                       //create an integer pointer
   int size;              
   printf("Enter the size of the array: ");
   scanf("%d", &size);                               //read size of array

   array = malloc(size * sizeof(int));               //use malloc to allocate memory for array
   int i;
   for(i = 0; i < size; i++){                       //read array elements
       printf("Enter element %d: ", i+1);
       scanf("%d", &array[i]);
   }  

   printArray(array, size);                       //print array
   bubbleSort(array, size);                       //sort the array using bubble sort
   printArray(array, size);                       //print array
  
   free(array);                                   //free the space allocated to array
   return 0;
}

--------------Screenshots-------------------

----------------------Output------------------------------------

-----------------------------------------------------------------------------------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs down.
Please Do comment if you need any clarification.
I will surely help you.

Thankyou


Related Solutions

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 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...
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.
1.   Bubble Sort Implement a bubble sort program that will read from a file “pp2.txt” from...
1.   Bubble Sort Implement a bubble sort program that will read from a file “pp2.txt” from the current directory a list of intergers (10 numbers to be exact), and the sort them, and print them to the screen. You can use redirection to read data from a given file through standard input, as opposed to reading the data from the file with the read API (similar to Lab #1). You can assume the input data will only have 10 numbers...
Bubble Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user...
Bubble Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user and gets at least 5 whole numbers as user input from the keyboard and stores them in an array Displays the numbers from the array on the screen Sorts the numbers in the array using BUBBLE SORT Algorithm Displays the sorted numbers on the screen from the array Save your code file as "yourLastname_Firstname_BubbleSort.cpp" and submit your .cpp file. NOTE: This assignment needs only...
Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
C++ Bubble Sort Write a program that ask user to enter 7 numbers and store that...
C++ Bubble Sort Write a program that ask user to enter 7 numbers and store that in array. Display that all numbers before and after performing Bubble sort. You must have to create new function with required parameter to perform Bubble sort. Sample Run :- Enter 1 number :- 1 Enter 2 number :- 5 Enter 3 number :- 7 Enter 4 number :- 45 Enter 5 number :- 90 Enter 6 number :- 6 Enter 7 number :- 55...
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...
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...
java 2. Write a program to implement heapsort. Sort the following elements using your program. 6,...
java 2. Write a program to implement heapsort. Sort the following elements using your program. 6, 12, 34, 29, 28, 11, 23, 7, 0, 33, 30, 45
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT