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

DAA Code

Uploaded by

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

DAA Code

Uploaded by

sameen butt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Bubble Sort

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);
}

static void BubbleSort(int[] arr)


{
int n = arr.Length;
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
// swap temp and arr[i]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

static void PrintArray(int[] arr)


{
int n = arr.Length;
for (int i = 0; i < n; ++i)
Console.Write(arr[i] + " ");
Console.WriteLine();
}
}

This C# code defines a BubbleSort function that sorts an array of


integers using the bubble sort algorithm. It also includes a PrintArray
function to print the sorted array. Finally, in the Main method, an example
array is sorted using BubbleSort and then printed using PrintArray.

C++

#include <iostream>
using namespace std;

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


for (int i = 0; i < n - 1; ++i) {
// Last i elements are already in place, so we don't need to check them again
for (int j = 0; j < n - i - 1; ++j) {
// Swap adjacent elements if they are in the wrong order
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

// Function to print an array


void printArray(int arr[], int size) {
for (int i = 0; i < size; ++i)
cout << arr[i] << " ";
cout << endl;
}

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;

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


int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
/* Move elements of arr[0..i-1], that are greater than key,
to one position ahead of their current position */
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

// Function to print an array


void printArray(int arr[], int n) {
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
cout << endl;
}

int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);

cout << "Original array: ";


printArray(arr, n);

insertionSort(arr, n);

cout << "Sorted array: ";


printArray(arr, n);
return 0;
}

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;

// Move elements of arr[0..i-1], that are greater than key,


// to one position ahead of their current position
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

static void PrintArray(int[] arr)


{
int n = arr.Length;
for (int i = 0; i < n; ++i)
Console.Write(arr[i] + " ");
Console.WriteLine();
}

static void Main(string[] args)


{
int[] arr = { 12, 11, 13, 5, 6 };
Console.WriteLine("Original array:");
PrintArray(arr);
InsertionSort(arr);
Console.WriteLine("Sorted array:");
PrintArray(arr);
}
This C# code defines a function InsertionSort that takes an array arr 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 method, an example
array is sorted using InsertionSort and then printed.

Selection Sort

using System;

class Program
{
static void SelectionSort(int[] arr)
{
int n = arr.Length;

// One by one move boundary of unsorted subarray


for (int i = 0; i < n - 1; i++)
{
// Find the minimum element in unsorted array
int minIndex = i;
for (int j = i + 1; j < n; j++)
{
if (arr[j] < arr[minIndex])
{
minIndex = j;
}
}

// Swap the found minimum element with the first element


int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}

static void PrintArray(int[] arr)


{
int n = arr.Length;
for (int i = 0; i < n; ++i)
{
Console.Write(arr[i] + " ");
}
Console.WriteLine();
}

static void Main(string[] args)


{
int[] arr = { 64, 25, 12, 22, 11 };
Console.WriteLine("Original array:");
PrintArray(arr);
SelectionSort(arr);
Console.WriteLine("Sorted array:");
PrintArray(arr);
}
}

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;

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


int i, j, minIndex, temp;
for (i = 0; i < n - 1; i++) {
minIndex = i;
// Find the minimum element in unsorted array
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the first element
temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}

// Function to print an array


void printArray(int arr[], int size) {
for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";
}
cout << endl;
}

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;

// Function to get the maximum element from an array


int getMax(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}

// Function to perform counting sort based on significant digit at exp


void countSort(int arr[], int n, int exp) {
int output[n]; // output array
int count[10] = {0};
// Store count of occurrences in count[]
for (int i = 0; i < n; i++) {
count[(arr[i] / exp) % 10]++;
}

// 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];
}

// Build the output array


for (int i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}

// 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];
}
}

// Radix Sort function


void radixSort(int arr[], int n) {
// Find the maximum number to know the number of digits
int max = getMax(arr, n);

// 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);
}
}

// Function to print an array


void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}

// Driver program to test above functions


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;
}

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.

You might also like