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

Merge Quick Program

The document contains two sorting algorithm implementations in C: Merge Sort and Quick Sort. Each algorithm includes a function to sort an array and a main function to input the array elements and display the sorted result. Both algorithms utilize different strategies for sorting, with Merge Sort using a divide-and-conquer approach and Quick Sort using a pivot-based partitioning method.

Uploaded by

mejafav142
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)
9 views4 pages

Merge Quick Program

The document contains two sorting algorithm implementations in C: Merge Sort and Quick Sort. Each algorithm includes a function to sort an array and a main function to input the array elements and display the sorted result. Both algorithms utilize different strategies for sorting, with Merge Sort using a divide-and-conquer approach and Quick Sort using a pivot-based partitioning method.

Uploaded by

mejafav142
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
You are on page 1/ 4

Code: Merge Sort

#include <stdio.h>
#include <stdlib.h>
void Merge(int arr[], int left, int mid, int right) {
int i, j, k;
int size1 = mid - left + 1; int size2 = right - mid;
int Left[size1], Right[size2];
for (i = 0; i < size1; i++) Left[i] = arr[left + i];
for (j = 0; j < size2; j++) Right[j] = arr[mid + 1 + j];
i = 0; j=0; k=left;
while (i < size1 && j < size2) {
if (Left[i] <= Right[j]) {
arr[k] = Left[i]; i++;
}
else {
arr[k] = Right[j]; j++; }
k++;
}
while (i < size1) {
arr[k] = Left[i]; i++;
k++; }
while (j < size2){
arr[k] = Right[j]; j++;
k++; } }
void Merge_Sort(int arr[], int left, int right)
{
if (left < right) {
int mid = left + (right - left) / 2;
Merge_Sort(arr, left, mid);
Merge_Sort(arr, mid + 1, right);
Merge(arr, left, mid, right); } }
int main() {
int size;
printf("Enter the size: "); scanf("%d", &size);
int arr[size];
printf("Enter the elements of array: ");
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
Merge_Sort(arr, 0, size - 1);
printf("The sorted array is: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

Output:
Code: Quick Sort
#include <stdio.h>
#include <stdlib.h>
int quickSort(int *arr, int low, int high) {
int i = low, j = high;
int pivot = arr[(low + high) / 2];
while (i <= j) {
while (arr[i] < pivot) i++;
while (arr[j] > pivot) j--;
if (i <= j) {
int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++;
j--; } }
if (low < j)
quickSort(arr, low, j);
if (i < high)
quickSort(arr, i, high); return 0;
}
int main(void) {
puts("Enter the number of elements in the array: "); int n;
scanf("%d", &n); int arr[n];
puts("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int low = 0;
int high = n - 1;
int pivot = arr[high]; int k = low - 1;
for (int j = low; j < high; j++)
{
if (arr[j] <= pivot)
{
k++;
int temp = arr[k]; arr[k] = arr[j]; arr[j] = temp; } }
int temp = arr[k + 1];
arr[k + 1] = arr[high];
arr[high] = temp;
int pi = k + 1;
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
printf("\nThe sorted array is: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}

Output:

You might also like