mirror of
https://github.com/TheAlgorithms/C
synced 2025-02-12 03:24:30 +03:00
code clean + added missing function + syntax corrections
This commit is contained in:
parent
dc5d25690b
commit
f23ec833b9
@ -1,42 +1,44 @@
|
||||
//sorting of array list using Radix sort
|
||||
#include <stdio.h>
|
||||
|
||||
#define range 10 // Range for integers is 10 as digits range from 0-9
|
||||
#define range 10 // Range for integers is 10 as digits range from 0-9
|
||||
|
||||
// Utility function to get the maximum value in ar[]
|
||||
int MAX(int ar[], int size){
|
||||
int i, max = ar[0];
|
||||
for(i = 0; i<size; i++){
|
||||
if(ar[i]>max)
|
||||
max = ar[i];
|
||||
}
|
||||
return max;
|
||||
int MAX(int ar[], int size)
|
||||
{
|
||||
int i, max = ar[0];
|
||||
for (i = 0; i < size; i++)
|
||||
{
|
||||
if (ar[i] > max)
|
||||
max = ar[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
// Counting sort according to the digit represented by place
|
||||
void countSort(int arr[],int n,int place)
|
||||
void countSort(int arr[], int n, int place)
|
||||
{
|
||||
int i,freq[range]={0};
|
||||
int i, freq[range] = {0};
|
||||
int output[n];
|
||||
|
||||
|
||||
// Store count of occurences in freq[]
|
||||
for(i=0;i<n;i++)
|
||||
freq[(arr[i]/place)%range]++;
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
freq[(arr[i] / place) % range]++;
|
||||
|
||||
// Change freq[i] so that it contains the actual position of the digit in output[]
|
||||
for(i=1;i<range;i++)
|
||||
freq[i]+=freq[i-1];
|
||||
|
||||
for (i = 1; i < range; i++)
|
||||
freq[i] += freq[i - 1];
|
||||
|
||||
// Build the output array
|
||||
for(i=n-1;i>=0;i--)
|
||||
for (i = n - 1; i >= 0; i--)
|
||||
{
|
||||
output[freq[(arr[i]/place)%range]-1]=arr[i];
|
||||
freq[(arr[i]/place)%range]--;
|
||||
output[freq[(arr[i] / place) % range] - 1] = arr[i];
|
||||
freq[(arr[i] / place) % range]--;
|
||||
}
|
||||
|
||||
|
||||
// Copy the output array to arr[], so it contains numbers according to the current digit
|
||||
for(i=0;i<n;i++)
|
||||
arr[i]=output[i];
|
||||
for (i = 0; i < n; i++)
|
||||
arr[i] = output[i];
|
||||
}
|
||||
|
||||
/*This is where the sorting of the array takes place
|
||||
@ -44,41 +46,48 @@ void countSort(int arr[],int n,int place)
|
||||
n --- Array Size
|
||||
max --- Maximum element in Array
|
||||
*/
|
||||
void radixsort(int arr[],int n,int max) //max is the maximum element in the array
|
||||
void radixsort(int arr[], int n, int max) //max is the maximum element in the array
|
||||
{
|
||||
int mul=1;
|
||||
while(max)
|
||||
int mul = 1;
|
||||
while (max)
|
||||
{
|
||||
countsort(arr,n,mul);
|
||||
mul*=10;
|
||||
max/=10;
|
||||
countSort(arr, n, mul);
|
||||
mul *= 10;
|
||||
max /= 10;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, const char * argv[]){
|
||||
int n;
|
||||
printf("Enter size of array:\n");
|
||||
scanf("%d", &n); // E.g. 8
|
||||
|
||||
printf("Enter the elements of the array\n");
|
||||
int i;
|
||||
int arr[n];
|
||||
for(i = 0; i < n; i++){
|
||||
scanf("%d", &arr[i] );
|
||||
}
|
||||
|
||||
printf("Original array: ");
|
||||
display(arr, n); // Original array : 10 11 9 8 4 7 3 8
|
||||
|
||||
int max;
|
||||
max = MAX(arr,n);
|
||||
|
||||
radixsort(arr, n, max);
|
||||
|
||||
printf("Sorted array: ");
|
||||
display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11
|
||||
|
||||
return 0;
|
||||
|
||||
|
||||
void display(int *arr, int N)
|
||||
{
|
||||
for (int i = 0; i < N; i++)
|
||||
printf("%d, ", arr[i]);
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
int main(int argc, const char *argv[])
|
||||
{
|
||||
int n;
|
||||
printf("Enter size of array:\n");
|
||||
scanf("%d", &n); // E.g. 8
|
||||
|
||||
printf("Enter the elements of the array\n");
|
||||
int i;
|
||||
int arr[n];
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
scanf("%d", &arr[i]);
|
||||
}
|
||||
|
||||
printf("Original array: ");
|
||||
display(arr, n); // Original array : 10 11 9 8 4 7 3 8
|
||||
|
||||
int max;
|
||||
max = MAX(arr, n);
|
||||
|
||||
radixsort(arr, n, max);
|
||||
|
||||
printf("Sorted array: ");
|
||||
display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user