sorting algorithms c++
sorting algorithms c++
3 2024510066
Aim: To implement and analyze searching algorithms, Linear Search and Binary Search
Objectives:
1. To implement the Bubble Sort algorithm.
2. To implement the Insertion Sort algorithm.
3. To implement the Selection Sort algorithm.
4. To implement the Quick Sort algorithm.
5. To implement the Radix Sort algorithm.
6. To implement the Merge Sort algorithm.
7. To compare the time complexity of all the algorithms.
8. To understand when each algorithm is best suited for use.
Concept:
1. Start
2. Input the array to be sorted
3. Repeat the following steps until no swaps are made in a complete pass through
the array:
● Loop through the array from the first element to the (n-1)th element:
○ If the current element is greater than the next element:
■ Swap the elements
● Print the array status after every pass
4. End
1. Start
2. Input the array to be sorted
3. Loop through the array from the first element to the (n-1)th element:
● Find the smallest element from the unsorted part of the array
● Swap the smallest element with the first element of the unsorted array
● Print the array status after every pass
4. End
Prabhat Anand Tiwari Practical no. 3 2024510066
1. Start
2. Input the array to be sorted
3. Loop through the array starting from the second element:
● Set the current element as the key
● Insert the key in sorted array by:
○ Compare the key with elements in the sorted array and shift them
to the right if they are larger than the key
○ Insert the key when the element to left is smaller than the key
● Print the array status after every pass
4. End
Problem Statement:
Implement Bubble, Selection, Insertion, Quick, Radix and Merge sort techniques
in c++.
Solution:
Bubble Sort:
#include <iostream>
using namespace std;
class BubbleSort
{
public:
int *arr;
int size;
int *getInput()
{
cout << "Enter size of array: ";
cin >> size;
int *arr = new int[size];
for (int i = 0; i < size; i++)
{
cout << "Enter value of element at position " << i << ": ";
cin >> arr[i];
}
return arr;
}
};
int main()
{
BubbleSort bs1;
int *arr = bs1.getInput();
bs1.bubbleSort(arr);
return 0;
}
Output:
Prabhat Anand Tiwari Practical no. 3 2024510066
Selection Sort:
#include <iostream>
using namespace std;
class SelectionSort
{
public:
int *arr;
int size;
int *getInput()
Prabhat Anand Tiwari Practical no. 3 2024510066
{
cout << "Enter size of array: ";
cin >> size;
int *arr = new int[size];
for (int i = 0; i < size; i++)
{
cout << "Enter value of element at position " << i << ": ";
cin >> arr[i];
}
return arr;
}
};
int main()
{
SelectionSort is1;
int *arr = is1.getInput();
is1.selectionSort(arr);
return 0;
}
Output:
Prabhat Anand Tiwari Practical no. 3 2024510066
Insertion Sort:
#include <iostream>
using namespace std;
class InsertionSort
{
public:
int *arr;
int size;
{
cout << arr[j] << ' ';
}
cout << endl;
}
}
int *getInput()
{
cout << "Enter size of array: ";
cin >> size;
int *arr = new int[size];
for (int i = 0; i < size; i++)
{
cout << "Enter value of element at position " << i << ": ";
cin >> arr[i];
}
return arr;
}
};
int main()
{
int size = 0;
InsertionSort is1;
int *arr = is1.getInput();
is1.insertionSort(arr);
return 0;
}
Prabhat Anand Tiwari Practical no. 3 2024510066
Output:
Prabhat Anand Tiwari Practical no. 3 2024510066
Quick Sort:
#include <iostream>
using namespace std;
class QuickSort {
public:
int size;
int* getInput() {
cout << "Enter size of array: ";
cin >> size;
int* arr = new int[size];
int main() {
QuickSort quickSortObj;
int* array = quickSortObj.getInput();
quickSortObj.quickSort(array, 0, quickSortObj.size - 1);
return 0;
}
Output:
Prabhat Anand Tiwari Practical no. 3 2024510066
Radix Sort:
#include <iostream>
#include <vector>
using namespace std;
class RadixSort
{
public:
int *array;
int max = 0;
int size = 0;
int digits = 0;
int *getInput()
{
cout << "Enter size of array: ";
cin >> size;
int *arr = new int[size];
for (int i = 0; i < size; i++)
{
cout << "Enter value of element at position " << i << ": ";
cin >> arr[i];
}
return arr;
}
};
int main()
{
RadixSort rs;
int *arrayLoc = rs.getInput();
rs.findMaxDigit(arrayLoc);
Prabhat Anand Tiwari Practical no. 3 2024510066
rs.radixSort(arrayLoc);
return 0;
}
Output:
Prabhat Anand Tiwari Practical no. 3 2024510066
Merge Sort:
#include <iostream>
using namespace std;
class MergeSort
{
public:
int size;
int *getInput()
{
cout << "Enter size of array: ";
cin >> size;
int *arr = new int[size];
for (int i = 0; i < size; i++)
{
cout << "Enter value of element at position " << i << ": ";
cin >> arr[i];
}
return arr;
}
int i = 0;
int j = 0;
int k = left;
while (i < lSize && j < rSize)
Prabhat Anand Tiwari Practical no. 3 2024510066
{
if (leftArr[i] <= rightArr[j])
{
arr[k] = leftArr[i];
i++;
}
else
{
arr[k] = rightArr[j];
j++;
}
k++;
}
int main()
{
MergeSort ms;
int *array = ms.getInput();
ms.mergeSort(array, 0, ms.size - 1);
return 0;
}
Output:
Prabhat Anand Tiwari Practical no. 3 2024510066
Observation:
Bubble Sort is an easy to implement algorithm, but it is not very efficient. It swaps
adjacent elements if they are in the wrong order, making it easy to understand and
implement. But has a time complexity of O(n²). It can be applied to both positive and
negative numbers but is inefficient for large datasets because of its quadratic time
complexity. It performs many unnecessary comparisons and swaps even if the array is
partially sorted.
Selection Sort is easy to understand and implement but is also not efficient for large
datasets. It works by repeatedly finding the minimum element from the unsorted portion
of the array and swapping it with the first unsorted element. Like Bubble Sort, it has a
time complexity of O(n²), which makes it impractical for large datasets. Selection Sort
performs a fixed number of comparisons regardless of the initial order of the array.
However, it minimizes the number of swaps.
Insertion Sort is relatively efficient for small or nearly sorted datasets but can be slow in
the worst case and with larger datasets. It works by gradually building a sorted array by
picking each element and inserting it into its correct position. It performs much better
when the input is already partially sorted, with a best-case time complexity of O(n).
Quick Sort is an efficient algorithm that performs well on average, with a time complexity
of O(n log n). It works by selecting a pivot element and partitioning the array such that
elements smaller than the pivot are placed on its left, and larger elements on its right.
However, in the worst case (when the array is already sorted or nearly sorted), its time
complexity becomes O(n²).
Radix Sort is a sorting algorithm where we don’t compare elements. It processes the
numbers digit by digit, starting from the least significant digit to the most significant digit.
Its time complexity is O(d * n), where d is the number of digits, and n is the number of
elements in the array, making it more efficient than previous sorting algorithms.
However, it cannot be used for sorting negative integers.
Merge Sort is an efficient sorting algorithm that has a constant time complexity of O(n
log n), regardless of the input. It divides the array into two halves, recursively sorts each
half, and then merges the sorted halves together. Merge Sort is particularly useful for
large datasets because of its performance. However, it requires additional memory
proportional to the size of the array. Merge Sort can handle both positive and negative
numbers.