DSA UNIT2
DSA UNIT2
QuickSort works on the principle of divide and conquer, breaking down the problem into smaller sub-
problems.
1. Choose a Pivot: Select an element from the array as the pivot. The choice of pivot can vary (e.g.,
first element, last element, random element, or median).
2. Partition the Array: Rearrange the array around the pivot. After partitioning, all elements
smaller than the pivot will be on its left, and all elements greater than the pivot will be on its
right. The pivot is then in its correct position, and we obtain the index of the pivot.
3. Recursively Call: Recursively apply the same process to the two partitioned sub-arrays (left and
right of the pivot).
4. Base Case: The recursion stops when there is only one element left in the sub-array, as a single
element is already sorted.
#include <stdio.h>
quickSort(arr, pi + 1, high);
int main()
int arr[] = { 4, 2, 5, 3, 1 };
quickSort(arr, 0, n - 1);
return 0;
}
Radix Sort:- Radix Sort is a linear sorting algorithm that sorts elements by processing them
digit by digit. It is an efficient sorting algorithm for integers or strings with fixed-size keys.
Step 1: Find the largest element in the array, which is 802. It has three digits, so we will iterate
three times, once for each significant place.
Step 2: Sort the elements based on the unit place digits (X=0). We use a stable sorting technique,
such as counting sort, to sort the digits at each significant place. It’s important to understand that
the default implementation of counting sort is unstable i.e. same keys can be in a different order
than the input array. To solve this problem, We can iterate the input array in reverse order to
build the output array. This strategy helps us to keep the same keys in the same order as they
appear in the input array.
#include <stdio.h>
int find_max(int arr[], int n) {
int max_element = arr[0];
for(int i = 1; i<n; i++) {
if(arr[i] > max_element)
max_element = arr[i];
}
return max_element;
}
void countingSort(int arr[], int n, int pos)
{
int result[n + 1];
int count[10] = {0};
for (int i = 0; i < n; i++)
count[(arr[i] / pos) % 10]++;
for (int i = 1; i < 10; i++)
count[i] += count[i - 1];
for (int i = n - 1; i >= 0; i--) {
result[count[(arr[i] / pos) % 10] - 1] = arr[i];
count[(arr[i] / pos) % 10]--;
}
for (int i = 0; i < n; i++)
arr[i] = result[i];
}
radixsort(arr, n);
printf("An array after applying the radix sort: \n");
for (int i = 0; i < n; ++i) {
printf("%d ", arr[i]);
}
printf("\n");
}
Bucket sort is a sorting technique that involves dividing elements into various groups, or
buckets. These buckets are formed by uniformly distributing the elements. Once the elements are
divided into buckets, they can be sorted using any other sorting algorithm. Finally, the sorted
elements are gathered together in an ordered fashion.
bucketSort(a[], n)
1. Create 'n' empty buckets
2. Do for each array element a[i]
2.1. Put array elements into buckets, i.e. insert a[i] into bucket[n*a[i]]
3. Sort the elements of individual buckets by using the insertion sort.
4. At last, gather or concatenate the sorted buckets.
End bucketSort
Bucket Sort(A[])
1. Let B[0....n-1] be a new array
2. n=length[A]
3. for i=0 to n-1
4. make B[i] an empty list
5. for i=1 to n
6. do insert A[i] into list B[n a[i]]
7. for i=0 to n-1
8. do sort list B[i] with insertion-sort
9. Concatenate lists B[0], B[1],........, B[n-1] together in order
End
#include <stdio.h>
int getMax(int a[], int n) // function to get maximum element from the given array
{
int max = a[0];
for (int i = 1; i < n; i++)
if (a[i] > max)
max = a[i];
return max;
}
void bucket(int a[], int n) // function to implement bucket sort
{
int max = getMax(a, n); //max is the maximum element of array
int bucket[max], i;
for (int i = 0; i <= max; i++)
{
bucket[i] = 0;
}
for (int i = 0; i < n; i++)
{
bucket[a[i]]++;
}
for (int i = 0, j = 0; i <= max; i++)
{
while (bucket[i] > 0)
{
a[j++] = i;
bucket[i]--;
}
}
}
void printArr(int a[], int n) // function to print array elements
{
for (int i = 0; i < n; ++i)
printf("%d ", a[i]);
}
int main()
{
int a[] = {54, 12, 84, 57, 69, 41, 9, 5};
int n = sizeof(a) / sizeof(a[0]); // n is the size of array
printf("Before sorting array elements are - \n");
printArr(a, n);
bucket(a, n);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
}