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

Sorting

Uploaded by

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

Sorting

Uploaded by

078bei022
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Name: NITESH PAUDEL

Roll No: PUR078BEI022


Lab Sheet No: 6
Title: Implementation of Different Sorting Algorithms in C++

#include <iostream>

#include <chrono>

using namespace std;

void swap(int *x, int *y)

int temp;

temp = *x;

*x = *y;

*y = temp;

void display(int A[], int n)

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

cout << A[i] << ", ";

cout << endl;

// Bubble sort logic goes here


void bubbleSort(int A[], int n)

int i, j;

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

for (j = 0; j < n - i - 1; j++)

if (A[j] > A[j + 1])

swap(&A[j], &A[j + 1]);

// Selection sort logic goes here

void selectionSort(int A[], int n)

int i, j, smallest, position;

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

smallest = A[i];

position = i;

for (j = i + 1; j < n; j++)

if (A[i] < smallest)

smallest = A[j];

position = j;

}
if (i != position)

swap(&A[i], &A[position]);

// Insertion sort logic goes here

void insertionSort(int A[], int n)

int i;

int j;

int temp;

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

j = i - 1;

temp = A[i];

while (j >= 0 && temp < A[j])

A[j + 1] = A[j];

j = j - 1;

A[j + 1] = temp;

// Function to merge two subarrays of arr[]

// First subarray is arr[left..mid]


// Second subarray is arr[mid+1..right]

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

int n1 = mid - left + 1; // Size of left subarray

int n2 = right - mid; // Size of right subarray

// Create temporary arrays

int L[n1], R[n2];

// Copy data to temporary arrays L[] and R[]

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

L[i] = arr[left + i];

for (int j = 0; j < n2; j++)

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

// Merge the temporary arrays back into arr[left..right]

int i = 0, j = 0, k = left; // Initial indexes of subarrays

while (i < n1 && j < n2)

if (L[i] <= R[j])

arr[k] = L[i];

i++;

else

arr[k] = R[j];

j++;

}
k++;

// Copy the remaining elements of L[], if any

while (i < n1)

arr[k] = L[i];

i++;

k++;

// Copy the remaining elements of R[], if any

while (j < n2)

arr[k] = R[j];

j++;

k++;

// Main function of mergeSort

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

if (left < right)

int mid =

left + (right - left) / 2; // Avoids overflow for large left and right

mergeSort(arr, left, mid); // Sort left half

mergeSort(arr, mid + 1, right); // Sort right half


merge(arr, left, mid, right); // Merge sorted halves

// For quick sort partition

int partition(int A[], int l, int r)

int x = l;

int y = r;

int pivot = A[l];

while (x < y)

while (A[x] <= pivot)

x++;

while (A[y] > pivot)

y--;

if (x < y)

swap(&A[x], &A[y]);

A[l] = A[y];

A[y] = pivot;

return y;

// Quick sort logic goes here

void quickSort(int A[], int l, int r)

if (l < r)

{
int p = partition(A, l, r);

quickSort(A, l, p - 1);

quickSort(A, p + 1, r);

// Main Function

int main()

int A[] = {12, 32, 34, 11, 1, 23, 69, 23, 43, 21};

int n;

n = 10;

cout << "Before Sorting: " << endl;

display(A, n);

// Bubble Sorting

auto t1 = chrono::high_resolution_clock::now();

bubbleSort(A, n);

auto t2 = chrono::high_resolution_clock::now();

cout << "\nAfter Sorting: " << endl;

display(A, n);

auto bubble_duration =

chrono::duration_cast<chrono::nanoseconds>(t2 - t1);

cout << "Bubble Sort Duration: " << bubble_duration.count() << endl;

// Selection Sorting

auto t3 = chrono::high_resolution_clock::now();

selectionSort(A, n);

auto t4 = chrono::high_resolution_clock::now();
auto selection_duration =

chrono::duration_cast<chrono::nanoseconds>(t4 - t3);

cout << "\nAfter Sorting: " << endl;

display(A, n);

cout << "Selection Sort Duration: " << selection_duration.count()

<< endl;

// Merge Sorting

auto t5 = chrono::high_resolution_clock::now();

mergeSort(A, 0, n - 1);

auto t6 = chrono::high_resolution_clock::now();

auto merge_duration =

chrono::duration_cast<chrono::nanoseconds>(t6 - t5);

cout << "\nAfter Sorting: " << endl;

display(A, n);

cout << "Merge Sort Duration: " << merge_duration.count() << endl;

// Insertion Sorting

auto t7 = chrono::high_resolution_clock::now();

insertionSort(A, n);

auto t8 = chrono::high_resolution_clock::now();

auto insertion_duration =

chrono::duration_cast<chrono::nanoseconds>(t8 - t7);

cout << "\nAfter Sorting: " << endl;

display(A, n);

cout << "Insertion Sort Duration: " << insertion_duration.count()

<< endl;

// Quick Sorting
auto t9 = chrono::high_resolution_clock::now();

quickSort(A, 0, n - 1);

auto t10 = chrono::high_resolution_clock::now();

auto quick_duration =

chrono::duration_cast<chrono::nanoseconds>(t10 - t9);

cout << "\nAfter Sorting: " << endl;

display(A, n);

cout << "Quick Sort Duration: " << quick_duration.count() << endl;

return 0;

You might also like