Skip to content

Instantly share code, notes, and snippets.

@snadahalli
Last active May 16, 2018 14:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save snadahalli/7087434 to your computer and use it in GitHub Desktop.
Save snadahalli/7087434 to your computer and use it in GitHub Desktop.
C program to sort an integer array using bubble sort.
/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use
* the programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* Happy Coding
***********************************************************/
#include<stdio.h>
int main() {
int a[50], n, i, j, temp = 0;
printf("Enter how many numbers you want:\n");
scanf("%d", &n);
printf("Enter the %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("\n\t\tThe given array is:\n");
for (i = 0; i < n; i++) {
printf("\n\t\t%d", a[i]);
}
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("\n\n\n\t\tThe sorted array using Buble sort is:\n");
for (i = 0; i < n; i++) {
printf("\n\t\t%d", a[i]);
}
return 0;
}
@AnkanDas22
Copy link

That's definitely not bubble sort.
A bubble sort is called so because it forms a bubble, i.e., two consecutive numbers are only checked whenever it traverses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment