0% found this document useful (0 votes)
14 views6 pages

Merge, Quick

Concept of Merge

Uploaded by

mohankavin9414
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

Merge, Quick

Concept of Merge

Uploaded by

mohankavin9414
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Ex.

No: 06
Date: a) MERGE SORT

AIM:
To write a C program to sort a given array of numbers using
Merge Sort.

ALGORITHM:

• Split the array into two halves.


• Recursively sort both halves.
• Merge the sorted halves back together.

CODING:
#include<stdio.h>
#include<conio.h>
void merge(int a[],int beg,int mid,int end){
int i,j,k;
int n1 = mid-beg+1;
int n2 = end-mid;
int LeftArray[n1];
int RightArray[n2];
for(i=0;i<n1;i++){
LeftArray[i] = a[beg+i];
}
for(j=0;j<n2;j++){
RightArray[j] = a[mid+1+j];
}
i=0;
j=0;
k=beg;
while(i<n1 && j<n2){
if(LeftArray[i] <= RightArray[j]){
a[k] = LeftArray[i];
i++;
}
else{
a[k] = RightArray[j];
j++;
}
k++;
}
while(i<n1){
a[k] = LeftArray[i];
i++;
k++;
}
while(j<n2){
a[k] = RightArray[j];
j++;
k++;
}
}
void mergeSort(int a[],int beg,int end){
if(beg<end){
int mid = (beg+end)/2;
mergeSort(a,beg,mid);
mergeSort(a,mid+1,end);
mergeSort(a,mid-1,end);
}
}
void printArray(int a[],int n){
int i;
for(i=0;i<n;i++){
printf("%d",a[i]);
}
printf("\n");
}
int main(){
int a[]={12,31,25,8,32,17,40};
int n = sizeof(a)/sizeof(a[0]);
clrscr();
printf("Before sorting: ");
printArray(a,n);
mergeSort(a,0,n-1);
printf("After sorting: ");
printArray(a,n);
getch();
return 0;
}

OUTPUT:
Before sorting:
12 31 25 8 32 17 40
After sorting:
8 12 17 25 31 32 40

RESULT:
Thus, the sorting of numbers using merge sort was
executed successfully.

Signature of the Faculty


Ex.No: 06
Date: b) QUICK SORT

AIM:
To write a C program to sort a given array of numbers using
Quick Sort.

ALGORITHM:

• Select an element from the array as a pivot.


• Rearrange the array so that elements less than the pivot
come before it, and elements greater come after it.
• Apply the same logic to the left and right subarrays.

CODING:
#include<stdio.h>
#include<conio.h>
int partition(int a[],int start,int end){
int pivot = a[end];
int i = start-1;
int j;
int temp;
for(j=start;j<end-1;j++){
if(a[j]<pivot){
i++;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
temp = a[i+1];
a[i+1] = a[end];
a[end] = temp;
return(i+1);
}
void quick(int a[],int start,int end){
if(start<end){
int p = partition(a,start,end);
quick(a,start,p-1);
quick(a,p+1,end);
}
}
void arr(int a[],int n){
int i;
for(i=0;i<n;i++){
printf("%d",a[i]);
}
}
int main(){
int a[]={24,9,29,14,19,27};
int n=sizeof(a)/sizeof(a[0]);
clrscr();
printf("Before Sorting: \n");
arr(a,n);
quick(a,0,n-1);
printf("After Sorting: \n");
arr(a,n);
getch();
return 0;
}
OUTPUT:
Before sorting:
24 9 29 14 19 27
After sorting:
9 14 19 24 27 29

RESULT:
Thus, the sorting of numbers using quick sort was
executed successfully.

Signature of the Faculty

You might also like