0% found this document useful (0 votes)
20 views9 pages

Selection and Radix Sort Implementation

Uploaded by

omp830089
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views9 pages

Selection and Radix Sort Implementation

Uploaded by

omp830089
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Data Structure Laboratory (RCP232APC202L)

Laboratory Report
PRACTICAL NO. 12

Batch: S3 Roll No.: 48 Name:- Nikita Patil


Date of Practical: Date of Submission:

Title:- Implementations of Selection and Radix sort.

Evaluation

1) Attendance [5] ----------------


2) Lab Performance [5] ----------------
3) Oral [5] ----------------

Overall Marks [15] ----------------

Subject Incharge

R. C. Patel Institute of Technology, Shirpur


Data Structure Laboratory (RCP232APC202L)

PRACTICAL NO. 12
TITLE: Implementations of Selection and Radix sort.

PREREQUISITE: Basic understanding of C programming, arrays, and sorting algorithms.

OUTCOME: After successful completion of this practical, students will be able to:
• Understand and implement Selection Sort and Radix Sort algorithms.
• Compare performance and working of simple and non-comparison-based sorting techniques.
• Analyze time complexity and stability of both algorithms.

THEORY:

Sorting is the process of arranging data in a particular order (ascending or descending). In this
experiment, two different sorting techniques are implemented:

1. Selection Sort:
• A simple comparison-based algorithm.
• Repeatedly finds the minimum element from the unsorted portion and places it at the
beginning.
• Time Complexity: O(n²)
• Space Complexity: O(1)

2. Radix Sort:
• A non-comparison-based sorting algorithm.
• Sorts elements digit by digit, starting from least significant digit (LSD) to most significant
digit (MSD).
• Uses a stable intermediate sorting method such as counting sort for each digit.
• Time Complexity: O(nk) where k = number of digits.
• Space Complexity: O(n + k)

Applications:
• Data preprocessing and ranking systems.
• Used in databases, integer-based datasets, and large data processing tasks.

Algorithm for Sorting Methods:

R. C. Patel Institute of Technology, Shirpur


Data Structure Laboratory (RCP232APC202L)

1. Selection Sort:
a) Repeat from i = 0 to n-2:
• Find the index of the minimum element in the unsorted part.
• Swap it with the element at index i.

2. Radix Sort:
a) Find the maximum number to determine the number of digits.
b) For each digit position (unit, tens, hundreds, ...):
• Perform a stable Counting Sort based on that digit.
c) After processing all digits, the array will be sorted.

Sample Input/Output:

Input Array: [64, 25, 12, 22, 11]

Operation Output
Selection Sort [11, 12, 22, 25, 64]
Radix Sort [11, 12, 22, 25, 64]

Conclusion:

Thus, the implementation of Selection and Radix Sort algorithms was successfully completed. The
experiment highlights the difference between comparison-based and non-comparison-based
sorting methods, demonstrating efficiency and use-cases of each.

R. C. Patel Institute of Technology, Shirpur


Data Structure Laboratory (RCP232APC202L)

#include <stdio.h>

#include <stdlib.h>

#define MAX_SIZE 100

// Utility function to find maximum element

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 do counting sort based on digit represented by exp

void countingSort(int arr[], int n, int exp) {

int output[n];

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] to contain actual position of digit

for (int i = 1; i < 10; i++) {

R. C. Patel Institute of Technology, Shirpur


Data Structure Laboratory (RCP232APC202L)

count[i] += count[i - 1];

// Build 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 output array to arr[]

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

arr[i] = output[i];

// Radix Sort implementation

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

int max = getMax(arr, n);

// Do counting sort for every digit

for (int exp = 1; max / exp > 0; exp *= 10) {

countingSort(arr, n, exp);

// Selection Sort implementation

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

R. C. Patel Institute of Technology, Shirpur


Data Structure Laboratory (RCP232APC202L)

for (int i = 0; i < n - 1; i++) {

// Find minimum element in unsorted array

int min_idx = i;

for (int j = i + 1; j < n; j++) {

if (arr[j] < arr[min_idx]) {

min_idx = j;

// Swap found minimum element with first element

if (min_idx != i) {

int temp = arr[i];

arr[i] = arr[min_idx];

arr[min_idx] = temp;

// Function to print array

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

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

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

printf("\n");

int main() {

R. C. Patel Institute of Technology, Shirpur


Data Structure Laboratory (RCP232APC202L)

int arr[MAX_SIZE];

int n = 0;

int choice;

while (1) {

printf("\nSorting Menu:\n");

printf("1. Enter array\n");

printf("2. Selection Sort\n");

printf("3. Radix Sort\n");

printf("4. Display array\n");

printf("5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter number of elements (max %d): ", MAX_SIZE);

scanf("%d", &n);

if (n > MAX_SIZE || n < 1) {

printf("Invalid size! Please enter size between 1 and %d\n", MAX_SIZE);

break;

printf("Enter %d integers: ", n);

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

scanf("%d", &arr[i]);

R. C. Patel Institute of Technology, Shirpur


Data Structure Laboratory (RCP232APC202L)

printf("Original array: ");

printArray(arr, n);

break;

case 2:

if (n == 0) {

printf("Please enter array first!\n");

break;

printf("Before Selection Sort: ");

printArray(arr, n);

selectionSort(arr, n);

printf("After Selection Sort: ");

printArray(arr, n);

break;

case 3:

if (n == 0) {

printf("Please enter array first!\n");

break;

printf("Before Radix Sort: ");

printArray(arr, n);

radixSort(arr, n);

printf("After Radix Sort: ");

R. C. Patel Institute of Technology, Shirpur


Data Structure Laboratory (RCP232APC202L)

printArray(arr, n);

break;

case 4:

if (n == 0) {

printf("Please enter array first!\n");

break;

printf("Current array: ");

printArray(arr, n);

break;

case 5:

printf("Exiting program.\n");

exit(0);

default:

printf("Invalid choice! Please try again.\n");

return 0;

R. C. Patel Institute of Technology, Shirpur

You might also like