DAA Code
DAA Code
Code:
def bubble_sort(arr):
n = len(arr)
# Traverse through all array elements
for i in range(n):
# Last i elements are already in place
for j in range(0, n-i-1):
# Traverse the array from 0 to n-i-1
# Swap if the element found is smaller
# than the next element
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
# Example usage:
arr = [64, 34, 25, 12, 22, 11, 90]
bubble_sort(arr)
print("Sorted array is:", arr)
C#
using System;
class Program
{
static void Main(string[] args)
{
int[] arr = { 64, 34, 25, 12, 22, 11, 90 };
BubbleSort(arr);
Console.WriteLine("Sorted array:");
PrintArray(arr);
}
C++
#include <iostream>
using namespace std;
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Original array: ";
printArray(arr, n);
bubbleSort(arr, n);
cout << "Sorted array: ";
printArray(arr, n);
return 0;
}
This C++ code defines a function bubbleSort that takes an array arr and its
size n as input and sorts it using the bubble sort algorithm. It also includes a
printArray function to print the elements of an array. In the main function, an
example array is sorted using bubbleSort and then printed.
Insertion Sort:
C++
#include <iostream>
using namespace std;
int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
This C++ code defines a function insertionSort that takes an array arr and its
size n as input and sorts it using the insertion sort algorithm. It also includes a
printArray function to print the elements of an array. In the main function, an
example array is sorted using insertionSort and then printed.
C#
using System;
class Program
{
static void InsertionSort(int[] arr)
{
int n = arr.Length;
for (int i = 1; i < n; i++)
{
int key = arr[i];
int j = i - 1;
Selection Sort
using System;
class Program
{
static void SelectionSort(int[] arr)
{
int n = arr.Length;
This C# code defines a function SelectionSort that takes an array arr as input and
sorts it using the selection sort algorithm. It also includes a PrintArray function to
print the elements of an array. In the Main method, an example array is sorted using
SelectionSort and then printed.
C++
#include <iostream>
using namespace std;
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Original array: ";
printArray(arr, n);
selectionSort(arr, n);
cout << "Sorted array: ";
printArray(arr, n);
return 0;
}
This C++ code defines a function selectionSort that takes an array arr and its
size n as input and sorts it using the selection sort algorithm. It also includes a
printArray function to print the elements of an array. In the main function, an
example array is sorted using selectionSort and then printed.
Radix Sort:
#include <iostream>
using namespace std;
// Change count[i] so that count[i] now contains actual position of this digit in
output[]
for (int i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
// Copy the output array to arr[], so that arr[] now contains sorted numbers
according to current digit
for (int i = 0; i < n; i++) {
arr[i] = output[i];
}
}
// Perform counting sort for every digit. Note that instead of passing digit
number, exp is passed.
// exp is 10^i where i is the current digit number
for (int exp = 1; max / exp > 0; exp *= 10) {
countSort(arr, n, exp);
}
}
radixSort(arr, n);
Radix Sort is a non-comparative sorting algorithm that sorts data with integer
keys by grouping keys by the individual digits which share the same significant
position and value. Here's an implementation of Radix Sort in C++:
This C++ code defines functions for Radix Sort. getMax function is used to get
the maximum element from the array, countSort function performs counting
sort based on the significant digit at a given exponent, and radixSort function
sorts the array using Radix Sort algorithm. Finally, the main function
demonstrates the usage of the Radix Sort algorithm by sorting an example
array.