0% found this document useful (0 votes)
8 views14 pages

DSA Sorting Techniques

Consists of sorting techniques bubble sort,merge sort and quick sort with theory and code

Uploaded by

vedant.spamz65
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)
8 views14 pages

DSA Sorting Techniques

Consists of sorting techniques bubble sort,merge sort and quick sort with theory and code

Uploaded by

vedant.spamz65
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/ 14

Vedant Patil_S21_100

Code:
#include <stdio.h>
#define MAX_SIZE 100
//For BubbleSortAlgorithm
void bubbleSort(int a[], int n)
{
for(int i=0; i<n-1; i++)
{
for(int j=0; j<n-1; j++)
{
if(a[j] > a[j+1])
{
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
//For MergeSortAlgorithm
void merge(int arr[], int left, int mid, int right)
{
int n1 = mid - left + 1;
int n2 = right - mid;

int L[MAX_SIZE];
int R[MAX_SIZE];

for (int i = 0; i < n1; i++)


Vedant Patil_S21_100

{
L[i] = arr[left + i];
}
for (int j = 0; j < n2; j++)
{
R[j] = arr[mid + 1 + j];
}

int i = 0;
int j = 0;
int k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
Vedant Patil_S21_100

k++;
}
}
void mergeSort(int arr[], int left, int right)
{
if (left < right) {
int mid = left + (right - left) / 2;

mergeSort(arr, left, mid);


mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}

//For QuickSortAlgorithm

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


{
int pivot = array[end];
int j = beg-1;
for(int i=beg; i<=end-1; i++)
{
if(array[i] <= pivot)
{
j++;
int temp = array[j];
array[j] = array[i];
array[i] = temp;
Vedant Patil_S21_100

}
}
j++;
int temp = array[j];
array[j] =array[end];
array[end] = temp;
return j;
}
int quickSort(int a[], int beg, int end)
{
if(beg<end)
{
int pivot = partition(a, beg, end);
quickSort(a, beg, pivot-1);
quickSort(a, pivot+1, end);
}
}

void main()
{
int n,choice;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
for(int i=0; i<n; i++)
{
printf("Enter the element: ");
scanf("%d", &arr[i]);
}
Vedant Patil_S21_100

printf("\nEnter Sorting method to be applied:\n1.Bubble Sort\n2.Merge Sort\n3.Quick


Sort\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1://Bubble Sort
printf("Given array is:\n");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");

bubbleSort(arr, n);

printf("\nSorted array is:\n");


for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
break;
case 2://MergeSort
printf("Given array is:\n");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
Vedant Patil_S21_100

mergeSort(arr,0, n-1);

printf("\nSorted array is:\n");


for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
break;

case 3://Quicksort
printf("Given array is:\n");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");

quickSort(arr,0, n-1);

printf("\nSorted array is:\n");


for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
break;
}
}
Vedant Patil_S21_100

Output:
Vedant Patil_S21_100

You might also like