1.int main() {
2. int a[] = {181, 289, 390, 121, 145, 736, 514, 888, 122};
3. int n = sizeof(a) / sizeof(a[0]);
4. printf("Before sorting array elements are - n");
5. printArray(a,n);
6. radixsort(a, n);
7. printf("After applying Radix sort, the array elements are -
n");
8. printArray(a, n);
9.}
1.#include <stdio.h>
2.
3.int getMax(int a[], int n) {
4. int max = a[0];
5. for(int i = 1; i<n; i++) {
6. if(a[i] > max)
7. max = a[i];
8. }
}
return max; //maximum element from the array
}
1.void countingSort(int a[], int n, int place) // function t implement counting sor
t
2.{
3. int output[n + 1];
4. int count[10] = {0};
5.
6. // Calculate count of elements
7. for (int i = 0; i < n; i++)
8. count[(a[i] / place) % 10]++;
9.
10. // Calculate cumulative frequency
11. for (int i = 1; i < 10; i++)
12. count[i] += count[i - 1];
13.
14. // Place the elements in sorted order
15. for (int i = n - 1; i >= 0; i--) {
16. output[count[(a[i] / place) % 10] - 1] = a[i];
17. count[(a[i] / place) % 10]--;
18. }
19.
20. for (int i = 0; i < n; i++)
21. a[i] = output[i]; }
1.// function to implement radix sort
2.void radixsort(int a[], int n) {
3.
4. // get maximum element from array
5. int max = getMax(a, n);
6.
7. // Apply counting sort to sort eleme
nts based on place value
8. for (int place = 1; max / place > 0;
place *= 10)
9. countingSort(a, n, place); }
1.// function to print array elements
2.void printArray(int a[], int n) {
3. for (int i = 0; i < n; ++i) {
4. printf("%d ", a[i]);
5. }
6. printf("n");
7.}
Data structure.pptx
Data structure.pptx
Data structure.pptx
Data structure.pptx
Data structure.pptx
Data structure.pptx

Data structure.pptx

  • 44.
    1.int main() { 2.int a[] = {181, 289, 390, 121, 145, 736, 514, 888, 122}; 3. int n = sizeof(a) / sizeof(a[0]); 4. printf("Before sorting array elements are - n"); 5. printArray(a,n); 6. radixsort(a, n); 7. printf("After applying Radix sort, the array elements are - n"); 8. printArray(a, n); 9.}
  • 45.
    1.#include <stdio.h> 2. 3.int getMax(inta[], int n) { 4. int max = a[0]; 5. for(int i = 1; i<n; i++) { 6. if(a[i] > max) 7. max = a[i]; 8. } } return max; //maximum element from the array }
  • 46.
    1.void countingSort(int a[],int n, int place) // function t implement counting sor t 2.{ 3. int output[n + 1]; 4. int count[10] = {0}; 5. 6. // Calculate count of elements 7. for (int i = 0; i < n; i++) 8. count[(a[i] / place) % 10]++; 9. 10. // Calculate cumulative frequency 11. for (int i = 1; i < 10; i++) 12. count[i] += count[i - 1]; 13. 14. // Place the elements in sorted order 15. for (int i = n - 1; i >= 0; i--) { 16. output[count[(a[i] / place) % 10] - 1] = a[i]; 17. count[(a[i] / place) % 10]--; 18. } 19. 20. for (int i = 0; i < n; i++) 21. a[i] = output[i]; }
  • 47.
    1.// function toimplement radix sort 2.void radixsort(int a[], int n) { 3. 4. // get maximum element from array 5. int max = getMax(a, n); 6. 7. // Apply counting sort to sort eleme nts based on place value 8. for (int place = 1; max / place > 0; place *= 10) 9. countingSort(a, n, place); }
  • 48.
    1.// function toprint array elements 2.void printArray(int a[], int n) { 3. for (int i = 0; i < n; ++i) { 4. printf("%d ", a[i]); 5. } 6. printf("n"); 7.}