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

Bubble Sort ADA AYUSH

The document discusses three sorting algorithms - bubble sort, merge sort, and quick sort. It provides the program code to implement each algorithm in C language. For each algorithm, it shows the program code to sort an array of integers and output the array before and after sorting.

Uploaded by

Ayush Patel
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)
60 views6 pages

Bubble Sort ADA AYUSH

The document discusses three sorting algorithms - bubble sort, merge sort, and quick sort. It provides the program code to implement each algorithm in C language. For each algorithm, it shows the program code to sort an array of integers and output the array before and after sorting.

Uploaded by

Ayush Patel
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

TY CE 2 Ayush Patel 200410107100

 Bubble sort
PROGRAM CODE:
#include
<stdio.h> int
main()
{
int array[45], n, c, d, swap;
printf("Enter number of
elements\n"); scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d",
&array[c]);

for (c = 0 ; c < n - 1; c++)


{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1])
{
swap = array[d];
array[d] =
array[d+1];
array[d+1] = swap;
}
}
}
printf("Array after sorting
is:\n"); for (c = 0; c < n; c++)
printf("%d\n", array[c]);
TY CE 2 Ayush Patel 200410107100

return 0;
}
OUTPUT :

 Merge sort

PROGRAM CODE:
#include <stdio.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],
RightArray[n2]; for (int i = 0; i
< n1; i++) LeftArray[i] = a[beg
+ i];
for (int j = 0; j < n2; j++)
RightArray[j] = a[mid + 1 +
j]; i = 0;
j = 0;
k = beg;
TY CE 2 Ayush Patel 200410107100

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;
TY CE 2 Ayush Patel 200410107100

mergeSort(a, beg, mid);


mergeSort(a, mid + 1,
end); merge(a, beg, mid,
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, 42 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are -
\n"); printArray(a, n);
mergeSort(a, 0, n - 1);
printf("After sorting array elements are -
\n"); printArray(a, n);
return 0;
}
Output:

 Quick sort
PROGRAM CODE:
#include <stdio.h>
TY CE 2 Ayush Patel 200410107100

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


{
int pivot = a[end]; // pivot
element int i = (start - 1);

for (int j = start; j <= end - 1; j++)


{
if (a[j] < pivot)
{
i++;
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
int t = a[i+1];
a[i+1] =
a[end]; a[end]
= t; return (i +
1);
}

void quick(int a[], int start, int end)


{
if (start < end)
{
int p = partition(a, start, end); //p is the partitioning
index quick(a, start, p - 1);
quick(a, p + 1, end);
}
}
TY CE 2 Ayush Patel 200410107100

void printArr(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]);
printf("Before sorting array elements are -
\n"); printArr(a, n);
quick(a, 0, n - 1);
printf("\nAfter sorting array elements are -
\n"); printArr(a, n);
return 0;
}
OUTPUT:

You might also like