NAME:Menta Jaya Sai Saketh
REGNO:19BBS0194
Lab slot:L3+L4
Faculty name:PROF. Boominathan P
QUICK SORT :
1)Illustrating the Quick Sort algorithm by using unordered 8 integers number as
input.
Pseudo code :
Algorithm quicksort(int number[25],int first,int last)
Decleration int I,j,pivot,temp
If first<last
{
Pivot=first
I=first
J=last
Until i<j
{
Until number[i]<=number[pivot] and i<last
I++
Until number[j]<number[pivot]
j—
if i<j
{
Temp=number[i]
Number[i]=number[j]
Number[j]=temp
}
}
Temp=number[pivot]
Number[pivot]=number[j]
Number[j]=temp
Quicksort(number,first,j-1)
Quicksort(number,j+1,last)
}
}
Algorithm main()
{
Declaration I,count,number[25]
Print “Enter number of elements “
Read count
Print “Enter count elements “
For i=0 to count i++
Read number[i]
Quicksort(number,0,count-1)
Print “The sorted order is :”
For i=0 to count i++
Print number[i]
Return 0
}
Programme code :
#include<stdio.h>
void quicksort(int number[25],int first,int last){
int i, j, pivot, temp;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
int main(){
int i, count, number[25];
printf("Enter number of elements : ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);
printf("The Sorted Order is: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
Output :
MERGE SORT :
2)Implement the Merge Sort Algorithm and show each pass output of the
Splitting/Merging array, until the array is sorted by using the following input
set.
14,23,45,7,13,53,3,26,48,31,12,54,5,18,33,15,4
Pseudo code :
Global variable declaration
A[11]={14,233,45,7,13,53,3,26,48,31,12,54,5,18,33,15,4}
B[10]
Algorithm merging(int low,int mid,int height)
{
Declaration l1,l2,i
For l1=low to mid l2=mid+1 to high i++
{
If a[l1]<=a[l2]
B[i]=a[l1++]
Else
B[i]=a[l2++]
}
Until l1<=mid
B[i++]=a[l1++]
Until l2<=high
B[i++]==a[l2++]
For i=low to high i++
A[i]=b[i]
}
Algorithm sort(int low ,int high)
{
Declaration int mid
If low<high
{
Mid=(low+high)/2
Sort(low,mid)
Sort(mid+1,high)
Merging(low,mid,high)
}
Else return
}
}
Algorithm main()
{
Declaration int i
Print “list before sorting :”
For i=0 to max i++
Print a[i]
Sort(0,max)
Print “list after sorting :”
For i=0 to max i++
Print a[i]
}
Programme code :
#include <stdio.h>
#define max 10
int a[11] = { 14,23,45,7,13,53,3,26,48,31,12,54,5,18,33,15,4 };
int b[10];
void merging(int low, int mid, int high) {
int l1, l2, i
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
if(a[l1] <= a[l2])
b[i] = a[l1++];
else
b[i] = a[l2++];
}
while(l1 <= mid)
b[i++] = a[l1++];
while(l2 <= high)
b[i++] = a[l2++];
for(i = low; i <= high; i++)
a[i] = b[i];
}
void sort(int low, int high) {
int mid;
if(low < high) {
mid = (low + high) / 2;
sort(low, mid);
sort(mid+1, high);
merging(low, mid, high);
} else {
return;
}
}
int main() {
int i;
printf("List before sorting\n");
for(i = 0; i <= max; i++)
printf("%d ", a[i]);
sort(0, max);
printf("\nList after sorting\n");
for(i = 0; i <= max; i++)
printf("%d ", a[i]);
}
Out put :
3)Demonstrate the Shell Sort algorithm and display each pass output by using
unordered 8 integers numbers as input.
Pseudo code :
Programme code :
Output :