Assignment No1 DSA
Assignment No1 DSA
Assignment no:-“1”
Submitted by:- “Maryam Sajjad”
Submitted to :- “Danial Hanif”
Roll no:- “S23RINFT1M01008”
Subject:- “DSA”
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
Example:
In cpp:
#include <iostream>
#include <vector>
#include <algorithm>
int n = arr.size();
std::vector<std::vector<float>> buckets(n);
int index = 0;
for (auto& bucket : buckets) {
insertionSort(bucket);
for (float num : bucket) {
arr[index++] = num;
}
}
}
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>
radixSort(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;
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>
selectionSort(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:
Example:
In cpp:
#include <iostream>
#include <vector>
insertionSort(arr);