Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
2 views4 pages

DS LAB Exp-3

The document outlines the implementation of two recursive sorting algorithms in C: Quick Sort and Merge Sort. Quick Sort uses a divide and conquer approach by selecting a pivot and partitioning the array, while Merge Sort recursively splits the array and merges sorted halves. The document includes code examples and discusses the time complexities of both algorithms, with Quick Sort having O(n^2) and Merge Sort having O(n log n).

Uploaded by

zeeshan ahmad
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)
2 views4 pages

DS LAB Exp-3

The document outlines the implementation of two recursive sorting algorithms in C: Quick Sort and Merge Sort. Quick Sort uses a divide and conquer approach by selecting a pivot and partitioning the array, while Merge Sort recursively splits the array and merges sorted halves. The document includes code examples and discusses the time complexities of both algorithms, with Quick Sort having O(n^2) and Merge Sort having O(n log n).

Uploaded by

zeeshan ahmad
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/ 4

PROGRAM -03

3.1 Objective: Write C programs for the following sorting algorithms (Recursive).
a) Quick Sort
b) Merge Sort
3.2 Quick sort
Theory
This algorithm follows the divide and conquer approach. It picks an element as pivot and then
partitions the given array around the picked pivot element. In quick sort, a large array is
divided into two arrays in which one holds values that are smaller than the specified value
(Pivot), and another array holds the values that are greater than the pivot. After that, the left
and right sub-arrays are also partitioned using the same approach. It will continue until the
single element remains in the sub-array.
Quick sort is the widely used sorting algorithm as it is a faster and highly efficient sorting
algorithm.
Code
#include <stdio.h>
/* function that consider last element as pivot,
place the pivot at its exact position, and place
smaller elements to left of pivot and greater
elements to right of pivot. */

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 current element is smaller than the pivot
if (a[j] < pivot)
{
i++; // increment index of smaller element
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);
}

10
/* function to implement quick sort */
void quick(int a[], int start, int end) /* a[] = array to be sorted, start = Starting index, end = En
ding index */
{
if (start < end)
{
int p = partition(a, start, end); //p is the partitioning index
quick(a, start, p - 1);
quick(a, p + 1, end);
}
}

/* function to print an array */


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

Complexity: O(n2)
3.3 Merge sort
Theory
We can think of it as a recursive algorithm that continuously splits an array in half until it
cannot be further sub divided. This means that if the array becomes empty or has only one
element left, the dividing will stop (base case to stop the recursion). Finally, when both the

11
halves are sorted, the merge operation is applied. Merge operation is the process of taking
two smaller sorted arrays and combining them to eventually make a larger sorted one.
Code
#include <stdio.h>
/* Function to merge the subarrays of a[] */
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]; //temporary arrays

/* copy data to temp arrays */


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; /* initial index of first sub-array */


j = 0; /* initial index of second sub-array */
k = beg; /* initial index of merged sub-array */

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)

12
{
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);
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

Complexity: O(n log n)

13

You might also like