0% found this document useful (0 votes)
15 views5 pages

Quick Sort and Merge Sort in C

The document contains implementations of two sorting algorithms: Quick Sort and Merge Sort in C. Each algorithm includes a main function that prompts the user to enter an array of integers, sorts the array, and then displays the sorted result. Sample outputs demonstrate the functionality of both sorting methods.

Uploaded by

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

Quick Sort and Merge Sort in C

The document contains implementations of two sorting algorithms: Quick Sort and Merge Sort in C. Each algorithm includes a main function that prompts the user to enter an array of integers, sorts the array, and then displays the sorted result. Sample outputs demonstrate the functionality of both sorting methods.

Uploaded by

vajjhasaikrishna
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

/*--------------------------------------------------QUICK SORT---------------------------------------------------

*/

#include <stdio.h>

void quicksort(int arr[], int first, int last) {

if (first < last) {

int pivot = arr[last];

int i = first - 1;

for (int k = first; k <= last - 1; k++) {

if (arr[k] <= pivot) {

i++;

int temp = arr[i];

arr[i] = arr[k];

arr[k] = temp;

int temp = arr[i + 1];

arr[i + 1] = arr[last];

arr[last] = temp;

int pivotIndex = i + 1;

quicksort(arr, first, pivotIndex - 1);

quicksort(arr, pivotIndex + 1, last);

}
int main() {

int arr[100], n;

printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter elements: ");

for (int k = 1; k <= n; k++)

scanf("%d", &arr[k - 1]);

quicksort(arr, 0, n - 1);

printf("Sorted array: ");

for (int k = 1; k <= n; k++)

printf("%d ", arr[k - 1]);

return 0;

Sample Output :-

Enter number of elements: 5

Enter elements: 1 9 3 8 5

Sorted array: 1 3 5 8 9
/*--------------------------------------------------MERGE SORT-------------------------------------------------*/

#include <stdio.h>

void merge(int arr[], int left, int mid, int right) {

int i = left;

int j = mid + 1;

int k = 0;

int temp[1000];

while (i <= mid && j <= right) {

if (arr[i] <= arr[j]) {

temp[k++] = arr[i++];

} else {

temp[k++] = arr[j++];

while (i <= mid)

temp[k++] = arr[i++];

while (j <= right)

temp[k++] = arr[j++];

// Copy back to original array

for (i = left, k = 0; i <= right; i++, k++)

arr[i] = temp[k];

}
void mergesort(int arr[], int left, int right) {

if (left < right) {

int mid = (left + right) / 2;

mergesort(arr, left, mid);

mergesort(arr, mid + 1, right);

merge(arr, left, mid, right);

int main() {

int arr[100], n;

printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter elements: ");

for (int k = 1; k <= n; k++)

scanf("%d", &arr[k - 1]);

mergesort(arr, 0, n - 1);

printf("Sorted array: ");

for (int k = 1; k <= n; k++)

printf("%d ", arr[k - 1]);

return 0;

Sample Output :-

Enter number of elements: 5

Enter elements: 1 9 3 8 5

Sorted array: 1 3 5 8 9

You might also like