Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Bubble_sort Algo #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Bubble_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* Bubble sort code */

#include <stdio.h> // Header File in included in this line

int main() // The main function Begain here
{
int array[100], n, c, d, swap; /*Defineing the variable used in the program*/

printf("Enter number of elements\n"); // total number of integers in the sorting list that can be added into the array
scanf("%d", &n);

printf("Enter %d integers\n", n); //add different integers dont use ,(comma) hit enter after every new integer value

for (c = 0; c < n; c++)
scanf("%d", &array[c]); // adding values into the array

for (c = 0 ; c < n - 1; c++) //checking for correct/sorted order by loopuing and comparing each values in array and then swapping them in correct position
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d]; //method for swappin is define in line no 23-25
array[d] = array[d+1];
array[d+1] = swap;
}
}
} // list is sorted till here now we will be printing the sorted list

printf("Sorted list in ascending order:\n");

for (c = 0; c < n; c++)
printf("%d\n", array[c]);

return 0;

/* Time Complexity for Bubble Sort is O(n^2)*/
}