In: Computer Science
Write and test a C program to implement Bubble Sort. .
In your C program, you should do:
Debug your program using Eclipse C/C++ CDT.
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