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

Module 4

The document contains C++ implementations of two sorting algorithms: Merge Sort and Quick Sort. It includes functions for merging and partitioning arrays, as well as main functions to input an array, display it before and after sorting. The output demonstrates the sorting of an array of 15 integers using Quick Sort.
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)
2 views6 pages

Module 4

The document contains C++ implementations of two sorting algorithms: Merge Sort and Quick Sort. It includes functions for merging and partitioning arrays, as well as main functions to input an array, display it before and after sorting. The output demonstrates the sorting of an array of 15 integers using Quick Sort.
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

Module 4

#include<iostream>

using namespace std;

void merge(int arr[],int low,int mid,int high){

int i=low;

int j=mid+1;

int k=low;

int size=(high-1)+1;

int temp[size];

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

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

temp[k]=arr[i];

i++;

k++;

}else{

temp[k]=arr[j];

j++;

k++;

while(i<=mid){

temp[k]=arr[i];

i++;

k++;

while(j<=high){

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

k++;

for(int s=low;s<=high;s++){

arr[s]=temp[s];

void mergesort(int arr[],int low,int high){

if(low<high){

int mid=(low+high)/2;

mergesort(arr,low,mid);

mergesort(arr,mid+1,high);

merge(arr,low,mid,high);

int main(){

int size,i;

cout<<"Enter size of the array:";

cin>>size;

cout<<"Enter "<<size<<" [Link] elements of the array:"<<endl;

int myarr[size];

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

cin>>myarr[i];

cout<<"Before merge sort:";

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

cout<<myarr[i]<<" ";
}

mergesort(myarr,0,(size-1));

cout<<endl<<"After merge sort:"<<endl;

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

cout<<myarr[i]<<" ";

return 0;

Quick Sort:

//quick sort source code

#include <iostream>

using namespace std;

int partition(int arr[], int low, int high)

int pivot = arr[low];

int i = low;

int j = high;

while (i < j)

while (arr[i] <= pivot && i < high)

i++;

while (arr[j] > pivot)

j--;

if (i < j)

int temp = arr[i];

arr[i] = arr[j];
arr[j] = temp;

int temp = arr[low];

arr[low] = arr[j];

arr[j] = temp;

return j;

void quickSort(int arr[], int low, int high)

if (low < high)

int p = partition(arr, low, high);

quickSort(arr, low, p - 1);

quickSort(arr, p + 1, high);

int main()

int size;

cout << "Enter size of the array: ";

cin >> size;

cout << "Enter " << size << " no of elements of the array" << endl;

int myarr[50];

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

cin >> myarr[i];


}

cout << "Before quick sort:" << endl;

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

cout << myarr[i] << " ";

quickSort(myarr, 0, size - 1);

cout << endl << "After quick sort:" << endl;

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

cout << myarr[i] << " ";

return 0;

output:

Enter size of the array: 15

Enter 15 no of elements of the array

67

87

98

45

23

67
9

15

59

37

Before quick sort:

1 67 2 87 4 98 45 23 8 67 9 0 15 59 37

After quick sort:

0 1 2 4 8 9 15 23 37 45 59 67 67 87 98

You might also like