0% found this document useful (0 votes)
15 views

MERGE Shell Quick

The document describes three sorting algorithms: merge sort, quicksort, and shell sort. Merge sort works by dividing the array, sorting each subarray, and then merging the sorted subarrays. Quicksort chooses a pivot element and partitions the array into two halves based on element values relative to the pivot. Shell sort improves on insertion sort by allowing longer gaps between element comparisons.

Uploaded by

jashujee
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

MERGE Shell Quick

The document describes three sorting algorithms: merge sort, quicksort, and shell sort. Merge sort works by dividing the array, sorting each subarray, and then merging the sorted subarrays. Quicksort chooses a pivot element and partitions the array into two halves based on element values relative to the pivot. Shell sort improves on insertion sort by allowing longer gaps between element comparisons.

Uploaded by

jashujee
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

MERGE SORT

#include <stdio.h>

void merge(int originalArray[], int lowerIndex, int middleIndex, int higherIndex) {

int i, j, k;

int tempArray[higherIndex + 1];

i = lowerIndex;

j = middleIndex + 1;

k = lowerIndex;

while (i <= middleIndex && j <= higherIndex) {

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

tempArray[k] = originalArray[i];

i++;

} else {

tempArray[k] = originalArray[j];

j++;

k++;

while (i <= middleIndex) {

tempArray[k] = originalArray[i];

i++;

k++;

while (j <= higherIndex) {

tempArray[k] = originalArray[j];

j++;

k++;
}

for (k = lowerIndex; k <= higherIndex; k++) {

originalArray[k] = tempArray[k];

void mergeSort(int originalArray[], int lowerIndex, int higherIndex) {

int middleIndex;

if (lowerIndex < higherIndex) {

middleIndex = (lowerIndex + higherIndex) / 2;

mergeSort(originalArray, lowerIndex, middleIndex);

mergeSort(originalArray, middleIndex + 1, higherIndex);

merge(originalArray, lowerIndex, middleIndex, higherIndex);

// Print the current state of the array after each merge operation

for (int i = lowerIndex; i <= higherIndex; i++) {

printf("%d ", originalArray[i]);

printf("\n");

int main() {

int size, i;

scanf("%d", &size);

int array[size];

for (i = 0; i < size; i++) {

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

mergeSort(array, 0, size - 1);


return 0;

QUICK SORT:

#include <stdio.h>

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

if (first < last) {

int pivot = a[first];

int i = first;

int j = last;

while (i < j) {

while (a[i] <= pivot && i < last) {

i++;

while (a[j] > pivot) {

j--;

if (i < j) {

int temp = a[i];

a[i] = a[j];

a[j] = temp;

int temp = a[first];

a[first] = a[j];

a[j] = temp;

quicksort(a, first, j - 1);

quicksort(a, j + 1, last);

}
}

int main() {

int n, i;

scanf("%d", &n);

int a[n];

for (i = 0; i < n; i++) {

scanf("%d", &a[i]);

quicksort(a, 0, n - 1);

for (i = 0; i < n; i++) {

printf("%d ", a[i]);

return 0;

SHELL SORT:

#include <stdio.h>

int main() {

int i,j,temp,n;

printf("ENTER SIZE OF ARRAY:");

scanf("%d",&n);

int a[n];

printf("\n\n\n\n");

printf("ENTER ARRAY ELEMENTS: ");

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

scanf("%d",&a[i]);
}

int gap;

for(gap=n/2;gap>=1;gap=gap/2)

for(j=gap;j<n;j++)

for(i=j-gap;i>=0;i=i-gap)

if(a[i+gap]>a[i])

break;

else

temp=a[i+gap];

a[i+gap]=a[i];

a[i]=temp;

printf("SORTED ARRAY: \n");

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

printf("%d ",a[i]);

return 0;

You might also like