0% found this document useful (0 votes)
36 views24 pages

Assignment No1 DSA

Uploaded by

maryamsajjad781
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)
36 views24 pages

Assignment No1 DSA

Uploaded by

maryamsajjad781
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/ 24

Islamiya University Bahawalpur

Assignment no:-“1”
Submitted by:- “Maryam Sajjad”
Submitted to :- “Danial Hanif”
Roll no:- “S23RINFT1M01008”
Subject:- “DSA”

Date – 28-May-2024 Department - BS-IT


Bucket Sort
Define:
“Bucket Sort is a sorting algorithm that distributes elements into
several buckets and then sorts each bucket individually.”

Bucket-Sort Algorithm:
1. Let B [0 . . n-1] be a new array
2. n=A.length
3. for i = 0 to n-1
4. make B[i] an empty list
5. for i = 1 to n
6. insert A[i] into list B [[nA[i]]]
7. for i=0 to n-1
8. sort list B[i] with insertion sort
9. concatenate the lists together

B[0] , B[1] , …. B[n-1] ( in order)

Example:
In cpp:
#include <iostream>
#include <vector>
#include <algorithm>

// Function to sort a single bucket using insertion


sort
void insertionSort(std::vector<float>& bucket) {
for (size_t i = 1; i < bucket.size(); ++i) {
float key = bucket[i];
int j = i - 1;
while (j >= 0 && bucket[j] > key) {
bucket[j + 1] = bucket[j];
--j;
}
bucket[j + 1] = key;
}
}
// Function to perform Bucket Sort
void bucketSort(std::vector<float>& arr) {
if (arr.empty()) return;

int n = arr.size();
std::vector<std::vector<float>> buckets(n);

float max_value = *max_element(arr.begin(),


arr.end());

for (float num : arr) {


int index = static_cast<int>(num * n /
(max_value + 1));
buckets[index].push_back(num);
}

int index = 0;
for (auto& bucket : buckets) {
insertionSort(bucket);
for (float num : bucket) {
arr[index++] = num;
}
}
}

// Function to print an array


void printArray(const std::vector<float>& arr) {
for (float num : arr) {
std::cout << num << " ";
}
std::cout << std::endl;
}

// Main function to test the bucket sort


implementation
int main() {
std::vector<float> arr = {0.78, 0.17, 0.39, 0.26,
0.72, 0.94, 0.21, 0.12, 0.23, 0.68};
std::cout << "Original array: ";
printArray(arr);
bucketSort(arr);

std::cout << "Sorted array: ";


printArray(arr);

return 0;
}

Radix Sort
Define:
“Radix Sort is a non-comparative sorting algorithm that
sorts numbers by processing individual digits. It works
by sorting the input numbers based on each digit,
starting from the least significant digit to the most
significant digit.”
Radix-Sort Algorithm:
1. Determine the Maximum Number:
 Find the maximum number in the array to
know the number of digits.
2. Iterate Over Each Digit:
 Use counting sort (or another stable sort) for
each digit from least significant to most
significant.

Example:
In cpp:
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

// Function to get the maximum value in the array


int getMax(const std::vector<int>& arr) {
return *max_element(arr.begin(), arr.end());
}

// Function to do counting sort of arr[] according


to the digit represented by exp
void countingSort(std::vector<int>& arr, int exp) {
int n = arr.size();
std::vector<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] 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];
}
}

// The main function to that sorts arr[] of size n


using Radix Sort
void radixSort(std::vector<int>& arr) {
// Find the maximum number to know number
of digits
int m = getMax(arr);

// Do counting sort for every digit. Note that


instead of passing digit number, exp is passed.
// exp is 10^i where i is current digit number
for (int exp = 1; m / exp > 0; exp *= 10) {
countingSort(arr, exp);
}
}

// Function to print an array


void printArray(const std::vector<int>& arr) {
for (int num : arr) {
std::cout << num << " ";
}
std::cout << std::endl;
}

// Main function to test the radix sort


implementation
int main() {
std::vector<int> arr = {170, 45, 75, 90, 802, 24, 2,
66};
std::cout << "Original array: ";
printArray(arr);

radixSort(arr);

std::cout << "Sorted array: ";


printArray(arr);

return 0;
}
Merge Sort
Define:
“Merge Sort is a divide-and-conquer algorithm that
divides the input array into two halves, recursively sorts
each half, and then merges the two sorted halves.”

Merge-Sort Algoritm:
1. Divide: Divide the array into two
halves.
2. Conquer: Recursively sort each
half.
3. Combine: Merge the two
sorted halves to produce the
sorted array.

Example:
In cpp:
#include <iostream>
#include <vector>
// Function to merge two subarrays of arr[]
void merge(std::vector<int>& arr, int left, int mid,
int right) {
int n1 = mid - left + 1;
int n2 = right - mid;

// Create temp arrays


std::vector<int> L(n1);
std::vector<int> R(n2);

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


for (int i = 0; i < n1; i++)
L[i] = arr[left + i];
for (int i = 0; i < n2; i++)
R[i] = arr[mid + 1 + i];

// Merge the temp arrays back into


arr[left...right]
int i = 0, j = 0, k = left;
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++;
}
}

// Function to perform merge sort on arr[]


void mergeSort(std::vector<int>& arr, int left, int
right) {
if (left < right) {
int mid = left + (right - left) / 2;

// Sort first and second halves


mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);

// Merge the sorted halves


merge(arr, left, mid, right);
}
}

// Function to print an array


void printArray(const std::vector<int>& arr) {
for (int num : arr) {
std::cout << num << " ";
}
std::cout << std::endl;
}

// Main function to test the merge sort


implementation
int main() {
std::vector<int> arr = {12, 11, 13, 5, 6, 7};
std::cout << "Original array: ";
printArray(arr);
mergeSort(arr, 0, arr.size() - 1);

std::cout << "Sorted array: ";


printArray(arr);

return 0;
}

Custom Sorting
Algorithm (Sdatun
Sort)

Define:
“Selection Sort is a simple comparison-based sorting
algorithm. It divides the input list into two parts: the
sub-list of items already sorted, which is built up from
left to right at the front (left) of the list, and the sub-list
of items remaining to be sorted that occupy the rest of
the list.”

Sdatun-Sort Algorithm:
1. Find the Minimum Element: Iterate through the
array to find the smallest element.
2. Swap the Minimum Element: Swap it with the first
unsorted element.
3. Repeat: Continue this process for the next
positions in the array until the entire array is
sorted.
Example:
In cpp:
#include <iostream>
#include <vector>

// Function to perform Selection Sort on arr[]


void selectionSort(std::vector<int>& arr) {
int n = arr.size();
for (int i = 0; i < n - 1; ++i) {
// Find the minimum element in the unsorted
part of the array
int minIdx = i;
for (int j = i + 1; j < n; ++j) {
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
}
// Swap the found minimum element with the
first element of the unsorted part
std::swap(arr[i], arr[minIdx]);
}
}

// Function to print an array


void printArray(const std::vector<int>& arr) {
for (int num : arr) {
std::cout << num << " ";
}
std::cout << std::endl;
}

// Main function to test the selection sort


implementation
int main() {
std::vector<int> arr = {64, 25, 12, 22, 11};
std::cout << "Original array: ";
printArray(arr);

selectionSort(arr);

std::cout << "Sorted array: ";


printArray(arr);

return 0;
}
Insertion Sort
Define: “Insertion Sort is a simple and intuitive
sorting algorithm that builds the sorted array one item
at a time.”

Insertion-Sort Algorithm:

1. Iterate Over Array: Start from the second element


(index 1) and iterate through the array.
2. Insert Element: For each element, compare it with
the elements before it and insert it into its correct
position in the sorted part of the array.

Example:
In cpp:
#include <iostream>
#include <vector>

// Function to perform Insertion Sort on arr[]


void insertionSort(std::vector<int>& arr) {
int n = arr.size();
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;
}
arr[j + 1] = key;
}
}

// Function to print an array


void printArray(const std::vector<int>& arr) {
for (int num : arr) {
std::cout << num << " ";
}
std::cout << std::endl;
}

// Main function to test the insertion sort


implementation
int main() {
std::vector<int> arr = {12, 11, 13, 5, 6};
std::cout << "Original array: ";
printArray(arr);

insertionSort(arr);

std::cout << "Sorted array: ";


printArray(arr);
return 0;
}

You might also like