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

DSA UNIT2

Uploaded by

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

DSA UNIT2

Uploaded by

facapa9164
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

How does QuickSort Algorithm work?

QuickSort works on the principle of divide and conquer, breaking down the problem into smaller sub-
problems.

There are mainly three steps in the algorithm:

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.

// C program to implement Quick Sort Algorithm

#include <stdio.h>

void swap(int* a, int* b) {


int temp = *a;
*a = *b;
*b = temp;
}

int partition(int arr[], int low, int high) {


int p = arr[low];
int i = low;
int j = high;
while (i < j) {
while (arr[i] <= p && i <= high - 1) {
i++;
}
while (arr[j] > p && j >= low + 1) {
j--;
}
if (i < j) {
swap(&arr[i], &arr[j]);
}
}
swap(&arr[low], &arr[j]);
return j;
}

void quickSort(int arr[], int low, int high) {

if (low < high) {

int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

int main()

int arr[] = { 4, 2, 5, 3, 1 };

int n = sizeof(arr) / sizeof(arr[0]);

quickSort(arr, 0, n - 1);

for (int i = 0; i < n; i++)

printf("%d ", arr[i]);

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.

//radix sort program in c

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

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


int max_element = find_max(arr, n);
for (int pos = 1; max_element / pos > 0; pos *= 10)
countingSort(arr, n, pos);
}
int main() {
int arr[] = {312, 42, 635, 11, 8, 783, 954, 777};
int n = sizeof(arr) / sizeof(arr[0]);
printf("An array before applying the radix sort: \n");
for (int i = 0; i < n; ++i) {
printf("%d ", arr[i]);
}
printf("\n");

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

What is Counting Sort?


Counting Sort is a non-comparison-based sorting algorithm. It is particularly efficient when the range
of input values is small compared to the number of elements to be sorted. The basic idea behind
Counting Sort is to count the frequency of each distinct element in the input array and use that
information to place the elements in their correct sorted positions.

Step 1:countingSort(array, n) // 'n' is the size of array


Step 2:max = find maximum element in the given array
Step 3:create count array with size maximum + 1
Step4:find the count of every unique element and
store that count at ith position in the count array

// Counting sort in C programming


#include<stdio.h>
#define MAX 255
void countSort(int array[], int size)
{
int output[MAX];
int count[MAX];
int max = array[0];
for (int i = 1; i < size; i++) { if (array[i] > max)
max = array[i];
}
for (int i = 0; i <= max; ++i)
{
count[i] = 0;
}

for (int i = 0; i < size; i++)


{
count[array[i]]++;
}
for (int i = 1; i <= max; i++)
{
count[i] += count[i - 1];
}

for (int i = size - 1; i >= 0; i--)


{
output[count[array[i]] - 1] = array[i];
count[array[i]]--;
}
for (int i = 0; i < size; i++)
{
array[i] = output[i];
}
}
void display(int array[], int size)
{
for (int i = 0; i < size; i++)
printf("%d ",array[i]);
printf("\n");
}
int main() {
int array[] = {2, 5, 2, 8, 1, 4, 1};
int n = sizeof(array) / sizeof(array[0]);
countSort(array, n);
display(array, n);
return 0;
}

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.

Bucket Sort Algorithm:

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

You might also like