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

Daa 2

Daa 3

Uploaded by

shivadubey2002
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)
11 views6 pages

Daa 2

Daa 3

Uploaded by

shivadubey2002
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/ 6

PRACTICAL 6

AIM:Write a program to sort an array using Merge Sort

#include <iostream>
using namespace std;

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


int n1 = mid - left + 1;
int n2 = right - mid;

int L[n1], R[n2];

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


L[i] = arr[left + i];
for (int i = 0; i < n2; i++)
R[i] = arr[mid + 1 + i];

int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}

while (i < n1) {


arr[k] = L[i];
i++;
k++;
}

while (j < n2) {


arr[k] = R[j];
j++;
k++;
}
}

void mergeSort(int arr[], int left, int right) {


if (left < right) {
int mid = left + (right - left) / 2;

mergeSort(arr, left, mid);


mergeSort(arr, mid + 1, right);

merge(arr, left, mid, right);


}
}

int main() {
int n;
cout << "Enter the number of elements in the array: ";
cin >> n;

int arr[n];
cout << "Enter the elements : ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

mergeSort(arr, 0, n - 1);

cout << "Sorted array: ";


for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}

return 0;
}

OUTPUT:
PRACTICAL 4
AIM:Write a program to sort an array using Selection Sort

#include <iostream>
#include <vector>

void selectionSort(std::vector<int>& arr) {


int n = arr.size();
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
std::swap(arr[i], arr[minIndex]);
}
}

int main() {
int n;

std::cout << "Enter the number of elements in the array: ";


std::cin >> n;

std::vector<int> arr(n);

std::cout << "Enter " << n << " numbers:\n";


for (int i = 0; i < n; i++) {
std::cin >> arr[i];
}

selectionSort(arr);

std::cout << "Sorted array:\n";


for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;

return 0;
}
OUTPUT:
PRACTICAL 5
AIM:Write a program to sort an array using Quick Sort

#include <iostream>
#include <vector>

void quickSort(std::vector<int>& arr, int low, int high);


int partition(std::vector<int>& arr, int low, int high);

void quickSort(std::vector<int>& arr, int low, int high) {


if (low < high) {

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

quickSort(arr, low, pivotIndex - 1);


quickSort(arr, pivotIndex + 1, high);
}
}

int partition(std::vector<int>& arr, int low, int high) {


int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
std::swap(arr[i], arr[j]); }
}
std::swap(arr[i + 1], arr[high]);
return i + 1;
}

int main() {
int n;

std::cout << "Enter the number of elements in the array: ";


std::cin >> n;

std::vector<int> arr(n);

std::cout << "Enter " << n << " numbers:\n";


for (int i = 0; i < n; i++) {
std::cin >> arr[i];
}

quickSort(arr, 0, n - 1);
std::cout << "Sorted array:\n";
for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;

return 0;
}

OUTPUT:

You might also like