Experiment 12: Program To Implement Bubble Sorting: Algorithm

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Experiment 12: Program to implement bubble

sorting
Algorithm
1. Input array
2. BubbleSort()
2.1 for i=0 till i<size, i++
for j=0 till j<size-1, j++
if AR[j]>AR[j+1]
tmp=AR[j]
AR[j]=AR[j+1]
AR[j+1]=tmp;
print array

Program
#include<stdio.h>
#include<conio.h>
void BubbleSort(int[],int);
void main()
{
int AR[50],ITEM,N,index,i;
clrscr();
printf("\n\n How many elements do you want to add ");
scanf("%d",&N);
printf("\n Enter Array elements ");
for(i=0;i<N;i++)
{
scanf("%d",&AR[i]);
}
BubbleSort(AR,N);
printf("\n\n The sorted array is ");
for(i=0;i<N;i++)
{
printf("\t%d",AR[i]);
}
getch();
}

void BubbleSort(int AR[],int size)


{
int tmp,i,j,k;
for(i=0;i<size;i++)
{
for(j=0;j<(size-1)-i;j++)
{
if(AR[j]>AR[j+1])
{
tmp=AR[j];
AR[j]=AR[j+1];
AR[j+1]=tmp;
}
}
printf("\n Array after iteration %d is : ",i+1);
for(k=0;k<size;k++)
{
printf("\t%d",AR[k]);
}
}
}
Experiment 13: Program to perform linear
search
Algorithm
1. Input array
2. Lsearch()
2.1 compare array elements with item
2.2 if found the return i else -1
3. if index ==-1 not found
else found
Program

#include<stdio.h>
#include<conio.h>
int Lsearch(int[],int,int);
void main()
{
int AR[50],ITEM,N,index,i;
clrscr();
printf("\n\n Enter no of elements ");
scanf("%d",&N);
printf("\n Enter Array Elements ");
for(i=0;i<N;i++)
{
scanf("%d",&AR[i]);
}
printf("\n Enter element to be searched ");
scanf("%d",&ITEM);
index=Lsearch(AR,N,ITEM);
if(index==-1)
printf("\n Element not found ");
else
printf("\n\n Element found at Position: %d ",index+1);
getch();
}

int Lsearch(int AR[],int size, int item)


{
int i;
for(i=0;i<size;i++)
{
if(AR[i]==item)
return i;
}
return -1;
}
Experiment 16: Program to perform merge sort
Algorithm
1. input array
2. merge_sort()
2.1 if l<r
mid =(l+r)/2
merge_sort(arr,beg,mid)
merge_sort(arr,mid+1,end)
merge_sort(arr,beg,mid,end)
3. display sorted array
Program
#include<conio.h>
#include<stdio.h>
void merge(int a[],int, int, int);
void merge_sort(int a[],int, int);
void main()
{
int arr[10], i, n;
clrscr();
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
printf("\n Enter the elements of the array: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
merge_sort(arr, 0, n-1);
printf("\n The sorted array is: \n");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
getch();
}

void merge(int arr[],int beg, int mid, int end)


{
int i=beg, j=mid+1, index=beg, temp[10], k;
while((i<=mid) && (j<=end))
{
if(arr[i] < arr[j])
{
temp[index] = arr[i];
i++;
}
else
{
temp[index] = arr[j];
j++;
}
index++;
}
if(i>mid)
{
while(j<=end)
{
temp[index] = arr[j];
j++;
index++;
}
}
else
{
while(i<=mid)
{
temp[index] = arr[i];
i++;
index++;
}
}
for(k=beg;k<index;k++)
arr[k] = temp[k];
}

void merge_sort(int arr[], int beg, int end)


{
int mid,j;
if(beg<end)
{
mid = (beg+end)/2;
merge_sort(arr, beg, mid);
merge_sort(arr, mid+1,end);
merge(arr, beg, mid,end);
}
}
Experiment 15: Program to perform quick sort
Algorithm
1. input array
2. quick_sort()
if beg<end
loc=partion(a,beg,end)
quick_sort(a,beg,loc-1)
quick_sort(a,loc+1,end)
3. partition()
3.1 loc=left=beg
right=end
flag=0
while flag!=1
while a[loc] <= a[right] && loc!=right
right—
if loc==right
flag =1
else if a[loc]>a[right]
temp = a[loc]
a[loc] = a[right]
a[right] = temp
loc = right
if flag!=1
while a[loc] >= a[left] && loc!=left
left++
if loc==left
flag =1
else if a[loc] <a[left]
temp = a[loc]
a[loc] = a[left]
a[left] = temp
loc = left
3.2 return loc

Program
#include <stdio.h>
#include <conio.h>
#define size 100
int partition(int a[], int beg, int end);
void quick_sort(int a[], int beg, int end);
void main()
{
int arr[size], i, n;
printf("\n Enter number of elements ");
scanf("%d", &n);
printf("\n Enter the elements of the array ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
quick_sort(arr, 0, n-1);
printf("\n The sorted array is: ");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
getch();
}

int partition(int a[], int beg, int end)


{
int left, right, temp, loc, flag;
loc = left = beg;
right = end;
flag = 0;
while(flag != 1)
{
while((a[loc] <= a[right]) && (loc!=right))
right--;
if(loc==right)
flag =1;
else if(a[loc]>a[right])
{
temp = a[loc];
a[loc] = a[right];
a[right] = temp;
loc = right;
}
if(flag!=1)
{
while((a[loc] >= a[left]) && (loc!=left))
left++;
if(loc==left)
flag =1;
else if(a[loc] <a[left])
{
temp = a[loc];
a[loc] = a[left];
a[left] = temp;
loc = left;
}
}
}
return loc;
}

void quick_sort(int a[], int beg, int end)


{
int loc;
if(beg<end)
{
loc = partition(a, beg, end);
quick_sort(a, beg, loc-1);
quick_sort(a, loc+1, end);
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy