0% found this document useful (0 votes)
25 views5 pages

Sorting Algorithms in C: Bubble, Insertion, Selection, Radix

The document contains implementations of four sorting algorithms: Bubble Sort, Insertion Sort, Selection Sort, and Radix Sort in C programming language. Each section includes code for reading an array of integers, sorting them using the respective algorithm, and printing the sorted result. Example inputs and outputs are provided for each sorting method.
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)
25 views5 pages

Sorting Algorithms in C: Bubble, Insertion, Selection, Radix

The document contains implementations of four sorting algorithms: Bubble Sort, Insertion Sort, Selection Sort, and Radix Sort in C programming language. Each section includes code for reading an array of integers, sorting them using the respective algorithm, and printing the sorted result. Example inputs and outputs are provided for each sorting method.
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

Bubble Sort

#include <stdio.h>
/*
* Main Function
*/
void main()
{
int array[20];
int n, j, i, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i= 0; i < n; i++)
{
scanf("%d", &array[i]);
}
for (i = 0 ; i < n - 1; i++)
{
for (j = 0 ; j < n - i- 1; j++)
{
if (array[j] > array[j+1])
{
swap = array[j];
array[j] = array[j+1];
array[j+1] = swap;
}
}
}

printf("Bubble Sorted list in ascending order:\n");

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


printf("%d\n", array[i]);

}
Enter number of elements
5
Enter 5 integers
6
2
9
3
5
Bubble Sorted list in ascending order:
2
3
5
6
9

Insertion Sort
#include<stdio.h>
int main()
{
int i, j, count, temp, number[25];

printf("How many numbers u are going to enter?: ");


scanf("%d",&count);

printf("Enter %d elements: ", count);


// This loop would store the input numbers in array
for(i=0;i<count;i++)
scanf("%d",&number[i]);

// Implementation of insertion sort algorithm


for(i=1;i<count;i++){
temp=number[i];
j=i-1;
while((temp<number[j])&&(j>=0)){
number[j+1]=number[j];
j=j-1;
}
number[j+1]=temp;
}

printf("Insertion Sorted elements: ");


for(i=0;i<count;i++)
printf(" %d",number[i]);

return 0;
}
How many numbers u are going to enter?: 5
Enter 5 elements: 7
4
9
6
12
Insertion Sorted elements: 4 6 7 9 12
Selection Sort
#include<stdio.h>
int main()
{
int size, arr[50], i, j, temp, small, count=0, index;
printf("Enter size for Array: ");
scanf("%d", &size);
printf("Enter %d array elements: ", size);
for(i=0; i<size; i++)
scanf("%d", &arr[i]);
for(i=0; i<(size-1); i++)
{
small = arr[i];
for(j=(i+1); j<size; j++)
{
if(small>arr[j])
{
small = arr[j];
count++;
index = j;
}
}
if(count!=0)
{
temp = arr[i];
arr[i] = small;
arr[index] = temp;
}
count=0;
}
printf("\nNow the Array after sorting is:\n");
for(i=0; i<size; i++)
printf("%d ", arr[i]);

return 0;
}

Enter size for Array: 5


Enter 5 array elements: 7
3
6
1
9

Now the Array after sorting is:


1 3 6 7 9
Radix Sort
#include<stdio.h>

// to get the element with maximum value


int getMax(int nums[], int n) {
int max = nums[0];
int i;
for (i = 1; i < n; i++)
if (nums[i] > max)
max = nums[i];
return max;
}
// Count Sort algorithm
void CountSort(int nums[], int n, int exp) {
// to store a sorted array
int output[n];

// to keep count of values


int i, cnt[10] = { 0 };

// count number of values


for (i = 0; i < n; i++)
cnt[(nums[i] / exp) % 10]++;

// count index for each


for (i = 1; i < 10; i++)
cnt[i] += cnt[i - 1];

// place the elements at a particular index to sort the array


for (i = n - 1; i >= 0; i--) {
output[cnt[(nums[i] / exp) % 10] - 1] = nums[i];
cnt[(nums[i] / exp) % 10]--;
}

// copy output in nums


for (i = 0; i < n; i++)
nums[i] = output[i];
}

// RadixSort algorithm
void RadixSort(int nums[], int n) {
int m = getMax(nums, n);
// call CountSort algorithm for each digit
for (int exp = 1; m / exp > 0; exp *= 10)
CountSort(nums, n, exp);
}

// print the array


void print(int nums[], int n) {
int i;
for (i = 0; i < n; i++)
printf("%d ", nums[i]);
}

// main function
int main() {
// user input for the array
printf("Enter number of elements: ");
int N= 0;
scanf("%d", &N);
int nums[N];
printf("Enter elements: ");
for(int i=0; i<N; i++){
scanf("%d", &nums[i]);
}

// calling RadixSort
RadixSort(nums, N);

// printing sorted array


printf("Sorted Array is: ");
print(nums, N);
return 0;
}

Enter number of elements: 5


Enter elements: 4

7
2
1
8
Sorted Array is: 1 2 4 7 8

You might also like