#include <stdio.h>
void bubble_sort(int a[], int k);
int main()
{
int arr[100], n, i;
printf("\n= = = = = = = = = = Bubble Sort in C = = = = = = = = = =\n");
printf("How many integers you wanna store..? : ");
scanf("%d", &n);
printf("Enter %d integers into the array:\n", n);
for(i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
printf("Before sorting the array elements are:\n");
for(i=0; i<n; i++)
{
printf("%d\t",arr[i]);
}
bubble_sort(arr,n);
printf("\n\nAfter sorting the array elements are:\n");
for(i=0; i<n; i++)
{
printf("%d\t",arr[i]);
}
printf("\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
return 0;
}
void bubble_sort(int arr[], int k)
{
int i, j, temp;
for(i=0; i<k-1; i++ )
{
for(j=0; j<k-i-1; j++ )
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
OutPut:
manoj@ubuntu:~/cp$ gcc bubblesort.c -o bubblesort
manoj@ubuntu:~/cp$ ./bubblesort
= = = = = = = = = = Bubble Sort in C = = = = = = = = = =
How many integers you wanna store..? : 8
Enter 8 integers into the array:
4
7
6
3
5
2
8
1
Before sorting the array elements are:
4 7 6 3 5 2 8 1
After sorting the array elements are:
1 2 3 4 5 6 7 8
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/cp$
void bubble_sort(int a[], int k);
int main()
{
int arr[100], n, i;
printf("\n= = = = = = = = = = Bubble Sort in C = = = = = = = = = =\n");
printf("How many integers you wanna store..? : ");
scanf("%d", &n);
printf("Enter %d integers into the array:\n", n);
for(i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
printf("Before sorting the array elements are:\n");
for(i=0; i<n; i++)
{
printf("%d\t",arr[i]);
}
bubble_sort(arr,n);
printf("\n\nAfter sorting the array elements are:\n");
for(i=0; i<n; i++)
{
printf("%d\t",arr[i]);
}
printf("\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
return 0;
}
void bubble_sort(int arr[], int k)
{
int i, j, temp;
for(i=0; i<k-1; i++ )
{
for(j=0; j<k-i-1; j++ )
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
OutPut:
manoj@ubuntu:~/cp$ gcc bubblesort.c -o bubblesort
manoj@ubuntu:~/cp$ ./bubblesort
= = = = = = = = = = Bubble Sort in C = = = = = = = = = =
How many integers you wanna store..? : 8
Enter 8 integers into the array:
4
7
6
3
5
2
8
1
Before sorting the array elements are:
4 7 6 3 5 2 8 1
After sorting the array elements are:
1 2 3 4 5 6 7 8
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/cp$
No comments:
Post a Comment