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

SORTING

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)
25 views

SORTING

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/ 11

Q:INSERTION SORT

#include<iostream>

using namespace std;

void display(int *array, int size) {

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

cout<< array[i] << " ";

cout<<endl;

void insertionSort(int *array, int size) {

int key, j;

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

key = array[i];//take value

j = i;

while(j > 0 && array[j-1]>key) {

array[j] = array[j-1];

j--;

array[j] = key; //insert in right place

int main() {

int n;

cout<< "Enter the number of elements: ";

cin>> n;

int arr[n]; //create an array with given number of elements

cout<< "Enter elements:" <<endl;

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

cin>>arr[i];

cout<< "Array before Sorting: ";

display(arr, n);
insertionSort(arr, n);

cout<< "Array after Sorting: ";

display(arr, n);

ANS:

Q:SELECTION SORT
#include <iostream>

using namespace std;

void selectionSort(int arr[], int n) {

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;

if (minIndex != i) {

std::swap(arr[i], arr[minIndex]);

}
int main() {

int n;

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

std::cin>> n;

int arr[n];

std::cout<< "Enter the elements:\n";

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

std::cin>>arr[i];

selectionSort(arr, n);

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

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

std::cout<<arr[i] << " ";

std::cout<< "\n";

return 0;

ANS:

Q:BUBBLE SORT
#include <iostream>
using namespace std;

void bubbleSort(int arr[], int size) {

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

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

if (arr[j] >arr[j + 1]) {

// Swap arr[j] and arr[j+1]

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

int main() {

int size;

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

std::cin>> size;

int arr[size];

std::cout<< "Enter the elements:" << std::endl;

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

std::cin>>arr[i];

bubbleSort(arr, size);

std::cout<< "Sorted array:" << std::endl;

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

std::cout<<arr[i] << " ";

}
return 0;

ANS:

Quick Sort

#include<iostream>
using namespace std;

void swap(int arr[] , int pos1, int pos2){


int temp;
temp = arr[pos1];
arr[pos1] = arr[pos2];
arr[pos2] = temp;
}

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


int i = low;
int j = low;
while( i <= high){
if(arr[i] > pivot){
i++;
}
else{
swap(arr,i,j);
i++;
j++;
}
}
return j-1;
}

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


if(low < high){
int pivot = arr[high];
int pos = partition(arr, low, high, pivot);

quickSort(arr, low, pos-1);


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

int main()
{
int n ;
cout << " Enter the size of array:";
cin>>n;
int arr[n];
for( int i = 0 ; i < n; i++){
cin>> arr[i];
}
quickSort(arr, 0 , n-1);
cout<<"The sorted array is: ";
for( int i = 0 ; i < n; i++){
cout<< arr[i]<<"\t";
}
}

Output

MERGE SORT
#include <iostream>
using namespace std;

void merge(int arr[], int l, int mid, int r) {


int n1 = mid - l + 1;
int n2 = r - mid;

int L[n1], M[n2];

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


L[i] = arr[l + i];
for (int j = 0; j < n2; j++)
M[j] = arr[mid + 1 + j];

// Maintain current index of sub-arrays and main array


int i, j, k;
i = 0;
j = 0;
k = l;

while (i < n1 && j < n2) {


if (L[i] <= M[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = M[j];
j++;
}
k++;
}

while (i < n1) {


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

while (j < n2) {


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

// Divide the array into two subarrays, sort them and merge them
void mergeSort(int arr[], int l, int r) {
if (l < r) {
// mid is the point where the array is divided into two subarrays
int mid = l + (r - l) / 2;

mergeSort(arr, l, mid);
mergeSort(arr, mid + 1, r);
// Merge the sorted subarrays
merge(arr, l, mid, r);
}
}

// Driver program
int main() {

int n ;
cout << " Enter the size of array:";
cin>>n;
int arr[n];
for( int i = 0 ; i < n; i++){
cin>> arr[i];
}
mergeSort(arr, 0 , n-1);
cout<<"The sorted array is: ";
for( int i = 0 ; i < n; i++){
cout<< arr[i]<<"\t";
}

return 0;
}
Output

You might also like