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

Aps Lab

The document describes the merge sort algorithm. It has two procedures - MergeSort and Merge. MergeSort recursively splits the array into halves, and sorts each half. Merge then merges the two sorted halves into a single sorted array.

Uploaded by

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

Aps Lab

The document describes the merge sort algorithm. It has two procedures - MergeSort and Merge. MergeSort recursively splits the array into halves, and sorts each half. Merge then merges the two sorted halves into a single sorted array.

Uploaded by

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

LAB 1

QUE. 1 ) Procedure Merge sort (A, start, finish)


The above Procedure Subalgorithm recursively sorts given list of elements between
position “start’ and “finish” (inclusive). Array ‘A’ contains ‘n’ number of elements.
Variables ‘length’ and ‘mid’ refer to the number of elements in the current sublist and
position of the middle element of the sublist, respectively.

Step 1 Computation, size of current sublist


set length = finish – start + 1
Step 2 Condition checking, if length is one
if (length = 1) then
return
Step 3 calculating, middle point
set mid= start + (length/2) – 1
Step 4 solving I sublist, recursively
call Mergesort (A, start, mid)
Step 5 Solving II sublist, recursively
call Mergesort (A, mid + 1, finish)
Step 6 Merging two sublists, sorted
call Merge (A, start, mid + 1, finish)
Step 7 Finish
return

Procedure Merge (A, first, second, third)


The above procedure subalgorithm merges the two lists and produces a new sorted list.
Temporary array 'temp' is used for holding sorted values.
Step 1 initialization
set n = 0, f=first, S= second
ANS /BUBBLE SORT/

#include<stdio.h>
#include<stdlib.h>
#include<alloc.h>
#include<dos.h>
#include<time.h>
void main()
{
clock_t start,end;

int i,n,j,temp,k;
printf("\nEnter No of elements in the array:");
scanf("%d",&n);
start = clock();
int *p=(int*) malloc(n*sizeof(int));
printf("Enter the elements of array to be sorted:");
for(i=0;i<n;i++)
{
*(p+i)=rand();
}
printf("\nThe Elements of the unsorted array are:\n");
for(i=0;i<n;i++)
{
printf("%d\t",*(p+i));
}
printf("\n");

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(*(p+j)>*(p+j+1))
{
temp=*(p+j);
*(p+j)=*(p+j+1);
*(p+j+1)=temp;
}

}
printf("\nArray after pass no %d:",i);
for(k=0;k<n;k++)
printf(" %d ",*(p+k));
}
printf("\n\nThe Elements of the sorted array are(BY BUBBLE SORT):\n");
for(i=0;i<n;i++)
{
printf("%d\t",*(p+i));
}
end = clock();
printf("\nThe time for the event was: %f",(end-start)/CLK_TCK);
}

You might also like