Merge pull request #149 from Aruj-Sharma/master

Added Stooge Sort and Radix Sort in sorting algorithms
This commit is contained in:
Ashwek Swamy 2019-10-30 14:24:53 +05:30 committed by GitHub
commit c8bb8fd081
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 113 additions and 0 deletions

View File

@ -66,6 +66,8 @@ C
- SelectionSort
- ShakerSort
- HeapSort
- StoogeSort
## Hashing
- sdbm

37
sorting/StoogeSort.c Normal file
View File

@ -0,0 +1,37 @@
#include <stdio.h>
void stoogesort(int [], int, int);
void main()
{
int arr[100], i, n;
printf("How many elements do you want to sort: ");
scanf("%d", &n);
for (i = 0;i < n; i++)
scanf(" %d", &arr[i]);
stoogesort(arr, 0, n - 1);
printf("Sorted array : \n");
for (i = 0;i < n;i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
void stoogesort(int arr[], int i, int j)
{
int temp, k;
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
if ((i + 1) >= j)
return;
k = (int)((j - i + 1) / 3);
stoogesort(arr, i, j - k);
stoogesort(arr, i + k, j);
stoogesort(arr, i, j - k);
}

74
sorting/radix_sort.c Normal file
View File

@ -0,0 +1,74 @@
#include<stdio.h>
int largest(int a[], int n)
{
int large = a[0], i;
for(i = 1; i < n; i++)
{
if(large < a[i])
large = a[i];
}
return large;
}
void RadixSort(int a[], int n)
{
int bucket[10][10], bucket_count[10];
int i, j, k, remainder, NOP=0, divisor=1, large, pass;
large = largest(a, n);
printf("The large element %d\n",large);
while(large > 0)
{
NOP++;
large/=10;
}
for(pass = 0; pass < NOP; pass++)
{
for(i = 0; i < 10; i++)
{
bucket_count[i] = 0;
}
for(i = 0; i < n; i++)
{
remainder = (a[i] / divisor) % 10;
bucket[remainder][bucket_count[remainder]] = a[i];
bucket_count[remainder] += 1;
}
i = 0;
for(k = 0; k < 10; k++)
{
for(j = 0; j < bucket_count[k]; j++)
{
a[i] = bucket[k][j];
i++;
}
}
divisor *= 10;
for(i = 0; i < n; i++)
printf("%d ",a[i]);
printf("\n");
}
}
int main()
{
int i, n, a[10];
printf("Enter the number of elements :: ");
scanf("%d",&n);
printf("Enter the elements :: ");
for(i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
RadixSort(a,n);
printf("The sorted elements are :: ");
for(i = 0; i < n; i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}