0% found this document useful (0 votes)
41 views

#Include #Include: Int Int

This document contains C code to implement bubble sort on an integer array. It includes functions to: 1) Get user input for the number of array elements and their values and print the unsorted array. 2) Implement the bubble sort algorithm by repeatedly swapping adjacent elements that are out of order until the array is fully sorted. 3) Print the sorted array.

Uploaded by

esmani84
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

#Include #Include: Int Int

This document contains C code to implement bubble sort on an integer array. It includes functions to: 1) Get user input for the number of array elements and their values and print the unsorted array. 2) Implement the bubble sort algorithm by repeatedly swapping adjacent elements that are out of order until the array is fully sorted. 3) Print the sorted array.

Uploaded by

esmani84
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<stdio.

h>
#include<conio.h>
void sort(int *a,int n);
void main()
{
int a[20];
int n,i;
clrscr();
printf("Program for BUBBLE SORT\n");
printf("Enter the Number of ELements you want in Array\n");
scanf("%d",&n);
printf("Enter the Elements in UNSOTED ARRAY\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("The Unsorted ARRAY is:\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\n");
sort(&a,n);
getch();
}
void sort(int *a,int n)
{
int i,temp,j;
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if((*a+j)==(*a+j+1))
{
temp=*a+j;
*a+j=*a+j+1;
*a+j+1=temp;
}
}
}
}

#include<stdio.h>
#include<conio.h>

void bubble(int *ptr,int s)


{
int i,j;
int temp;
for(i=1;i<s;i++)
{
for(j=0;j<s-i;j++)
{
if(*(ptr+j)>*(ptr+j+1))
{
temp=*(ptr+j);
*(ptr+j)=*(ptr+j+1);
*(ptr+j+1)=temp;
}
}
}
}
void main()
{
int arr[10];
int i;
int size;
clrscr();
printf("\nEnter the size of array :");
scanf("%d",&size);

printf("\nEnter the element\n");


for(i=0;i<size;i++)
scanf("%d",&arr[i]);
printf("\nBefore sorting\n");
for(i=0;i<size;i++)
printf("%d
",arr[i]);
bubble(arr,size);
printf("\nAfter sorting\n");
for(i=0;i<size;i++)
printf("%d
",arr[i]);
getch();
}

You might also like