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

DSA LAB 104

The document contains implementations of three sorting algorithms: Bubble Sort, Selection Sort, and Radix Sort in C++. Each algorithm is explained with its time complexity, and the code includes input handling and output display for sorted arrays.

Uploaded by

patwadivya787
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)
6 views

DSA LAB 104

The document contains implementations of three sorting algorithms: Bubble Sort, Selection Sort, and Radix Sort in C++. Each algorithm is explained with its time complexity, and the code includes input handling and output display for sorted arrays.

Uploaded by

patwadivya787
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

MRIDUL MAURYA

24U030104
IT 2
DSA LAB
BUBBLE SORT

#include <iostream>

using namespace std;

// bubble sort |best case O(n),worst case and average case O(n2)

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

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

bool bh = true;

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

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

swap(arr[j],arr[j+1]);

bh = false;

if(bh){

break;

int main(){

int n;

cin>>n;

int arr[n];

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

cin>>arr[i];

bubble (arr,n);

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

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

}
}

SELECTION SORT

#include <iostream>

using namespace std;

// selection sort |best case,worst case and average case O(n2)

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

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

int minindex = i;

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

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

minindex = j;

swap(arr[i],arr[minindex]);

int main(){

int n;

cin>>n;

int arr[n];

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

cin>>arr[i];

selection(arr,n);

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

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

REDIX SORT
#include <iostream>

using namespace std;

// Function to get the maximum value in an array

int getMax(int arr[], int n) {

int maxVal = arr[0];

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

if (arr[i] > maxVal) {

maxVal = arr[i];

return maxVal;

// Counting Sort for Radix Sort (Sorting based on a specific digit place)

void countingSort(int arr[], int n, int exp) {

int output[n]; // Output array to store sorted values

int count[10] = {0}; // Count array for digits (0-9)

// Count occurrences of each digit at the current place value

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

int digit = (arr[i] / exp) % 10;

count[digit]++;

// Convert count array to prefix sum to determine actual positions

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

count[i] += count[i - 1];

// Build the output array in sorted order

for (int i = n - 1; i >= 0; i--) {

int digit = (arr[i] / exp) % 10;

output[count[digit] - 1] = arr[i];
count[digit]--;

// Copy the sorted elements back into the original array

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

arr[i] = output[i];

// Radix Sort function

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

int maxVal = getMax(arr, n); // Find the maximum number to determine


the number of digits

// Apply counting sort for each digit (units, tens, hundreds, etc.)

for (int exp = 1; maxVal / exp > 0; exp *= 10) {

countingSort(arr, n, exp);

// Function to print an array

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

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

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

cout << endl;

// Main function

int main() {

int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};

int n = sizeof(arr) / sizeof(arr[0]);


cout << "Original array: ";

printArray(arr, n);

radixSort(arr, n);

cout << "Sorted array: ";

printArray(arr, n);

return 0;

OUTPUT

You might also like